简体   繁体   English

在while循环php上设置链接

[英]setting a link on a while loop php

$query = mysql_query("SELECT * FROM mailtbl WHERE fromuser = '$adminsess' AND sent = '1'");

echo "To Subject Date View message";

while($fetch = mysql_fetch_assoc($query)) {

    $mid = $fetch['mid'];
    $to = $fetch['touser'];
    $subject2 = $fetch['subject'];
    $message2 = $fetch['message'];
    $date2 = $fetch['datesent'];
    $rand = $fetch['rand'];
    $view = "<a href = 'messages.php?mode=".$sent."&view=".$rand."'>View message</a>";

    echo "$to$subject2$date2$view";

}

echo "</table>";

if (isset($view)) {

    $viewget = $_GET['view'];

    if ($viewget== $rand) {

        echo "hi";
        echo "$message2";

    } 
}

The $view there suppose to open the content of each message. 那里的$view假定打开每个消息的内容。 The main problem is, if I have multiple values in the table, after clicking the link in each row in the table, the only link that is functioning is in the last row. 主要问题是,如果表中有多个值,则在单击表中每一行中的链接后,唯一起作用的链接是在最后一行中。 The previous rows in the table with links doesn't show the content of the message (which is echo "hi" & echo "$message2";). 带有链接的表中的前几行未显示消息的内容(即echo“ hi”和echo“ $ message2”;)。 What exactly is wrong with my code? 我的代码到底有什么问题? thanks. 谢谢。

Your code to display the message is outside the while loop. 您显示消息的代码在while循环之外。 If you want it to run for every message, then put it in the loop that is iterating over every message. 如果希望它针对每条消息运行,则将其置于遍历每条消息的循环中。

The problem is that $rand is not set inside the isset($view) check. 问题在于isset($view)检查中未设置$rand $rand still contains the last value from the while loop above, so only the last $viewget / $rand will work. $rand仍然包含上述while循环中的最后一个值,因此只有最后一个$viewget / $rand可以工作。

My guess is that messages.php shows all available messages (the message list) and if clicked on one message, the message list and that selected message. 我的猜测是messages.php显示所有可用的消息(消息列表),如果单击一条消息,则显示消息列表和所选消息。

So first, before the while loop, you fetch the message identifier of the message to be shown: 因此,首先,在while循环之前,获取要显示的消息的消息标识符:

$ViewRequested = isset($_GET['view']) ? $_GET['view'] : -1;
$ViewRequestedData = NULL;

Next, inside the while loop, you check if the user is allowed to view the selected message, and remember the record: 接下来,在while循环中,检查是否允许用户查看所选消息并记住记录:

if ($rand == $ViewRequested) {
    $ViewRequestedData = $fetch;
}

Finally, after the while loop, you check whether there is a valid message requested: 最后,在while循环之后,您检查是否请求了有效消息:

if (is_array($ViewRequestedData) {
    echo $ViewRequestedData['message2'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM