2014-01-29 51 views
0

基本上我寫了以下內容,但我想知道什麼是最好的方式來獲得$ message_id =您點擊查看收件箱消息的$ row ['id']。如果我只是靜態設置$ message_id =說39,然後點擊$ row ['id']是39的行,它會打開消息。點擊更新變量

基本上它所做的就是列出收件箱中的消息,並用它的行標識來標記它們的標識以顯示該消息的全部內容。

<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> 

任何人都知道,如果這甚至有可能,因爲我知道代碼讀取從上到下,據我所知,你不能回去向上的。

+1

我真的不明白你的問題,但判斷你的問題的標題訪問的消息ID'更新通過JavaScript上click'一個變量是有可能 – FreshPro

+0

您可以使用Ajax做到這一點。 –

+0

根據你的判斷:在上面的「程序」代碼中更新一個變量......只需查看一下引用:http://www.php.net/manual/en/language.references.php。 – loveNoHate

回答

0

我假設$user_data['username']已設置或可用,如果是的話,那麼你可以傳遞一個你選擇的變量名稱,它指向消息ID而不是隻發送ID(例如:我將它設置爲msg_id),現在你可以通過$_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>