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 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'