[英]Select all from table and display result mysql
<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if(mysql_num_rows($query_announcement) > 0){
$row = mysql_num_rows($query_announcement);
for($x = 1; $x<= $row; $x++){
$html = '<tr>';
$html .= '<td>'.$get_announcement['id'].'</td>';
$html .= '<td>'.$get_announcement['announce_name'].'</td>';
$html .= '<td>'.$get_announcement['announce_description'].'</td>';
$html .= '<td>'.$get_announcement['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
?>
/this is my code but when i run it i only get the announcement with the same id, not all announcement that i created. /这是我的代码,但是当我运行它时,我只会得到具有相同ID的公告,而不是我创建的所有公告。 please help thanks 请帮助谢谢
You are looping the announcement but are not then pulling the next announcement. 您正在循环播放公告,但没有拉出下一个公告。
for($x = 1; $x<= $row; $x++){
//Pull next record
$row = mysql_num_rows($query_announcement);
If you change it to the code above the for loop will start. 如果将其更改为for循环上方的代码,则会启动。 The data will be pulled, displayed and then it will loop back around again. 数据将被拉出,显示,然后将再次循环。
your r fetching data only one time 您一次只能提取数据
for($x = 1; $x<= $row; $x++){
$get_announcement= mysql_fetch_assoc($query_announcement);
Try this: You need to loop through the rows to and use the each row to display them. 尝试以下操作:您需要遍历各行,并使用每一行来显示它们。
<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if($get_announcement){
while($row = mysql_fetch_array($get_announcement)) {
$html = '<tr>';
$html .= '<td>'.$row['id'].'</td>';
$html .= '<td>'.$row['announce_name'].'</td>';
$html .= '<td>'.$row['announce_description'].'</td>';
$html .= '<td>'.$row['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.