dongyumiao5210 2016-06-15 20:54
浏览 56

将`mysqli_fetch_array`作为json对象中的一个字符串发送

I have the follwing code which was written to generate an array and send it back to my main page and append it to an existing table.

I am just woundering if there is any possible way to send the data generated from the below code as a single element within a JSON object.

For example:

{"arraydata":"Data Generated From The below Code","another variable":"some other data"}

An so on...

Any Suggestions??

     $result = mysqli_query($con," SELECT * FROM `BIMTECH_academy_2016_classes` 
               WHERE  `Serial`='$serial' ORDER BY `Serial` ");

     while($row = mysqli_fetch_array($result))
     {
           echo "<tr id='" . $row['Serial'] . "'>";
               echo "<td><img src='images/delete-icon.svg' id='classDeleteIcon'/></td>";
               echo "<td class='datepicker' id='Date'>" . $row['Date'] . "</td>";
               echo "<td class='timepicker' id='From'>" . $row['From'] . "</td>";
               echo "<td class='timepicker' id='To'>" . $row['To'] . "</td>";
           echo "</tr>";
     }
  • 写回答

1条回答 默认 最新

  • dow98764 2016-06-15 21:27
    关注

    Declare a variable ($html) before the while loop. Append/concat all data (html and $rows values) to $html variable. Create a array with different keys and values, one key contains $html variable as value.

    $html = ''; // a variable
    while($row = mysqli_fetch_array($result))
    {  // concat all data to $html
       $html.="<tr id='" . $row['Serial'] . "'>";
       $html.="<td><img src='images/delete-icon.svg' id='classDeleteIcon'/></td>";
       $html.="<td class='datepicker' id='Date'>" . $row['Date'] . "</td>";
       $html.="<td class='timepicker' id='From'>" . $row['From'] . "</td>";
       $html.="<td class='timepicker' id='To'>" . $row['To'] . "</td>";
       $html.="</tr>";
    }
    $data_array = array(); // declare array
    $data_array['arraydata'] = $html; // assign to array key
    $data_array['otherdata'] = 'otherdata'; // other data to the array
    $json_data = json_encode($data_array, JSON_HEX_QUOT | JSON_HEX_TAG); // encode array with html tags
    echo $json_data;
    

    Output:

    {"arraydata":" your html data","otherdata":"otherdata"}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器