Update a variable on click

2019-09-15 16:38发布

basically I've written the following but I'm wondering what the best way to get $message_id = to the $row['id'] you click on to view an inbox message. If I just statically set $message_id = to say 39 and click on the row that's $row['id'] is 39 it'll open the message.

Basically what it does is list inbox messages I have, and anchor tag them with its row id to be used in the header to show the full contents of that message.

<p>
    <?php
    $message_id = "39";

    if (isset($_GET[$message_id]) && empty($_GET[$message_id])) {
        $username = $user_data['username'];

        $get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
        $get_message_result = mysql_fetch_array($get_message);

        echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
        echo $get_message_result['sent_from'] . '<br />';
        echo $get_message_result['body'] . '<br />';
        echo '<a href="inbox.php">Back</a>';
    } 

    $username = $user_data['username'];
    $result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'"); 
    ?>

    <table border="1">
        <tr>
            <td>From</td>
            <td>Subject</td>
            <td>Date</td>
        </tr>
            <?php
            while($row = mysql_fetch_array($result))
            {
                echo
                '<tr>' .
                '<td><a href="#">' . $row['sent_from'] . '</a></td>' .
                '<td><a href="?' . $row['id'] . '">' . $row['subject'] . '</a></td>' .
                '<td>' . $row['post_date'] . '</td>' .
                '</tr>';
            }
            ?>
    </table>
</p>

Anyone know if this is even possible, as I know code is read from top to bottom and as far as I'm aware you cant go back upwards at all.

1条回答
乱世女痞
2楼-- · 2019-09-15 17:08

I assume that the $user_data['username'] is set or available,if yes then you could pass a variable name of you choice which points the message id instead of sending only the id (eg: i am setting it as msg_id ), Now you can access the message id via $_GET['msg_id']

<p>
<?php
$username = $user_data['username'];
if ( isset( $_GET['msg_id'] ) ) {     

    $get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
    $get_message_result = mysql_fetch_array($get_message);

    echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
    echo $get_message_result['sent_from'] . '<br />';
    echo $get_message_result['body'] . '<br />';
    echo '<a href="inbox.php">Back</a>';
}else{
  $result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'"); 

?>


<table border="1">
        <tr>
            <td>From</td>
            <td>Subject</td>
            <td>Date</td>
        </tr>
            <?php
            while($row = mysql_fetch_array($result))
            {
                echo
                '<tr>' .
                '<td><a href="#">' . $row['sent_from'] . '</a></td>' .
                '<td><a href="inbox.php?msg_id=' . $row['id'] . '">' . $row['subject'] . '</a></td>' .
                '<td>' . $row['post_date'] . '</td>' .
                '</tr>';
            }
            ?>
    </table>
</p>
查看更多
登录 后发表回答