duanjie9630 2017-11-25 14:20
浏览 39
已采纳

动态PHP响应

I have an html page with a button on it that triggers an ajax request to a php page. So far so good.

I have a database messages which has the columns "id", "receiver", "sender", "message".

Say I have 2 entrys there both the same receiver and sender. The messages are different.

Now upon button click the messages shall be displayed in seperate paragraphs <p>Message 1</p> and <p>Message 2</p>.

My problem is that I don't know how many messages there will be and the button click should display them all. I have tried it with a while loop but can't get it to work properly. Also how would JQuery handle something like this?

My php so far:

$thuser = $_SESSION['username']; //this user
$sql = "SELECT * FROM messages WHERE receiver = '$thuser'";

if ($result = mysqli_query($link, $sql)) {

  while ($row = $result->fetch_row()) {
  ...
  });

}

If possible I would like an array as the response containg all messages {'message1' = > 'Hello', 'message2' => 'What is up?', message n => 'blah'}

Thanks for any help!

  • 写回答

1条回答 默认 最新

  • dqfaom241360 2017-11-25 15:06
    关注

    Use Array in your case :

    $message=array();
    
    $thuser = $_SESSION['username']; //this user
    $sql = "SELECT * FROM messages WHERE receiver = '$thuser'";
    
    if ($result = mysqli_query($link, $sql)) {
     $i=1;
      while ($row = $result->fetch_row()) {
       $st='Message'.$i;
      $message[$st]=$row['message'];
       $i++;
      });
    
    }
    print_r($message);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 类图中关联与聚合的区别
  • ¥15 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系