Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL riformatta il risultato mysql per soddisfare json hansontable

Per creare un JSON corretto, prova :`

$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);
            }
`

e sul lato resever fai questo :

`
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>');

    `