0

我正在尝试获取数据库中的所有记录并且它是成功的,但是我只能获得 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 。对此我很抱歉。

4

1 回答 1

0
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);
?> 
于 2013-05-23T07:01:33.033 回答