有人可以帮我解决我在我的个人资料中有用户消息,它只显示最后一条消息,我将如何显示该用户的所有消息。
以下是我已经拥有的代码:
<?php
$message_list = mysql_query("SELECT * FROM `messages`");
while($get_message = mysql_fetch_array($message_list)) {
$message_subject = $get_message['subject'];
$message_body = $get_message['body'];
$message_from = $get_message['from_user'];
$message_date = $get_message['date'];
$message_to = $get_message['to_user'];
}
if($message_to === $user_data['username']) {
echo
'
<center>
<table border="2" bordercolor="" style="background-color:" width="85%" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>From</center></td>
<td width="20%"><center>Date / Time</center></td>
<td><center>Message</center></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center>Delete</center></td>
</tr>
</table>
</center>';
echo
'
<center>
<table border="1" bordercolor="" style="background-color:" width="85%" height="35px" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>'. $message_from .'</center></td>
<td width="20%"><center>'. $message_date .'</center></td>
<td><center><br>'. $message_body .'<br></center><br></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center><form><input type="checkbox" name="delete"></form></center></td>
</tr>
</table>
</center>';
} else {
echo '<center>you have no messages</center>';
}
?>
所有显示的内容都是最后输入的消息。
答案 0 :(得分:1)
问题是你早点结束循环,试试这个代码而不是你的代码
<?php
$message_list = mysql_query("SELECT * FROM `messages`");
while($get_message = mysql_fetch_array($message_list)) {
$message_subject = $get_message['subject'];
$message_body = $get_message['body'];
$message_from = $get_message['from_user'];
$message_date = $get_message['date'];
$message_to = $get_message['to_user'];
if($message_to === $user_data['username']) {
echo
'
<center>
<table border="2" bordercolor="" style="background-color:" width="85%" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>From</center></td>
<td width="20%"><center>Date / Time</center></td>
<td><center>Message</center></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center>Delete</center></td>
</tr>
</table>
</center>';
echo
'
<center>
<table border="1" bordercolor="" style="background-color:" width="85%" height="35px" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>'. $message_from .'</center></td>
<td width="20%"><center>'. $message_date .'</center></td>
<td><center><br>'. $message_body .'<br></center><br></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center><form><input type="checkbox" name="delete"></form></center></td>
</tr>
</table>
</center>';
} else {
echo '<center>you have no messages</center>';
}
}
?>
答案 1 :(得分:1)
只需要移动一个支架就可以了!
<?php
$message_list = mysql_query("SELECT * FROM `messages`");
while($get_message = mysql_fetch_array($message_list)) {
$message_subject = $get_message['subject'];
$message_body = $get_message['body'];
$message_from = $get_message['from_user'];
$message_date = $get_message['date'];
$message_to = $get_message['to_user'];
//DELETED BRACKET HERE
if($message_to === $user_data['username']) {
echo
'
<center>
<table border="2" bordercolor="" style="background-color:" width="85%" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>From</center></td>
<td width="20%"><center>Date / Time</center></td>
<td><center>Message</center></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center>Delete</center></td>
</tr>
</table>
</center>';
echo
'
<center>
<table border="1" bordercolor="" style="background-color:" width="85%" height="35px" cellpadding="3" cellspacing="3">
<tr>
<td width="15%"><center>'. $message_from .'</center></td>
<td width="20%"><center>'. $message_date .'</center></td>
<td><center><br>'. $message_body .'<br></center><br></td>
<td width="15%"><center>Reply</center></td>
<td width="7%"><center><form><input type="checkbox" name="delete"></form></center></td>
</tr>
</table>
</center>';
} else {
echo '<center>you have no messages</center>';
}
} //PUT IT HERE
?>
但正如警告一样,这将输出&#34;你没有消息&#34;每次系统中都有消息,即使它不是用户!我只是将您的查询更改为包含基于用户数据的WHERE
子句
$username = $user_data['username'];
$message_list = mysql_query("SELECT * FROM `messages` WHERE `to_user` = '$username'");