MySQL重新格式化mysql结果以满足json handsontable

时间:2012-11-05 17:48:03

标签: mysql json format

根据我之前解决的问题:Re-arrange mysql result in an expected format for hansontable。我要从

重新格式化mysql结果
  

[“Superior”],[“Deluxe - City View”],[“Deluxe - Balcony”],[“Junior”   套房“],[”安达曼工作室“]

["Superior","Deluxe - City View","Deluxe - Balcony","Junior Suite","Andaman Studio"]

从这些代码:

$sql_rName="select title from room_db where hotel='1' order by id asc";
$result_rName=mysql_db_query($dbname,$sql_rName);
while($rec_rName=mysql_fetch_array($result_rName)){
    $_rName=$rec_rName['title'];
    $_array[]=$_rName;
}
echo "{\"data\": ".json_encode($_array)."}";

mysql表:room_db room_db

请建议。

PS。感谢Olaf Dietsche所有这些帮助。

1 个答案:

答案 0 :(得分:2)

要制作正确的JSON,请尝试: `

$result="select title from room_db where hotel='1' order by id asc";    
$messages = array();
            while($message_data = mysql_fetch_assoc($result)) {
                $message = array(
                'id' => $message_data['userid'],
                'title' => $message_data['title']
                );
                $messages[] = $message;
                }
                echo json_encode($messages);
            }
`

并在接收方这样做:

`
data1=$.parseJSON(data);

            if(data1.length===0){

                $('#table > #table_body').append('<tr><td colspan="4" align="center" style="color:red">NO matching data </td></tr>');
                }
        else{
            for(var i=0;i<data1.length;i++)
            {
                $('#table > #table_body').append('<tr id="' + data1[i]['id'] +'"> <td id="' + data1[i]['id'] +'" align="center" <td>'+data1[i]['title']+'</td> </tr>');
            }
            }
            $('#table').append('</tbody>');

    `