我正在尝试获取数据库中的所有记录并且它是成功的,但是我只能获得 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'] 然后是一个循环显示 Read More 的按钮。我想要发生的是在单击“阅读更多”后调用一个函数,具体取决于 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 。对此我很抱歉。