gpt4 book ai didi

php - 只能获取 $row ['id' 的一个值]

转载 作者:行者123 更新时间:2023-11-30 01:37:16 29 4
gpt4 key购买 nike

我正在尝试获取数据库中的所有记录并且成功,但是我只能获取 1 个 id 值。我需要获取数据库中的所有 id 并调用 JavaScript 函数。在我的代码中,我只能获取最后一条记录的 id。我怎样才能得到它们?

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */
echo "<form action='' method='post' onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr; //This should change and increment the value of id, isn't it?

echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

这里发生的事情是,它将显示数据库中的记录:$row['header']、$row['date']、$row['desc'],然后是一个循环显示“阅读更多”的按钮。我想要发生的是在单击“阅读更多”后根据 id 调用一个函数。

例如,

___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 3
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 2
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 1
___________________________

这可能吗?或者我错过了什么。我的代码仅显示所有按钮的 $id=1 。对此我深表歉意。

最佳答案

you may try like this
make the id field name as array like more[] and bring the form out side loop as shown
no need of for loop there to get the ids u already using while loop
bring submit button also outside loop

如果你想显示该帖子的详细信息,那么为什么你再次使用表单你可以简单地将其链接到详细信息页面 n 可以传递 ID,无需将其作为表单提交。

检查下面的代码

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";

$result = mysql_query($query);

//echo "<form action='' method='post' onSubmit='ReadMore()'>";
echo "<table>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */

//for($ctr=1;$ctr<=3;$ctr++){
//$id=$ctr; //This should change and increment the value of id, isn't it?

//echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
//}
echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";

</td></tr>";
echo "</tr>";
}

echo "</table>";
//echo "</form>";
mysql_close($con);
?>

关于php - 只能获取 $row ['id' 的一个值],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707434/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com