Search code examples
phpjqueryajaxjquery-easyui

show data from json


I'm trying to show table from json but failed, what's wrong with this:

$nip=$_POST['nip']; 
$sql = "select satker,shift_description,nip  FROM jamkerja 
    inner join master_shift on master_shift.shiftno=jamkerja.shiftno 
    inner join tr_jamkerjahdr on jamkerja.id_jamkerja=tr_jamkerjahdr.id_jamkerja 
    inner join tr_jamkerjamember on tr_jamkerjamember.trno=tr_jamkerjahdr.trno 
    where nip='$nip' ";

$result = $con->query($sql);

$data = array();
while($row = mysqli_fetch_assoc($result)){
    $data[] = $row;
}
print $data;

And this is the table:

$json = $data;
$json_decoded = json_decode($json);
foreach($json_decoded as $data12){
    echo '<tr>';
    echo '<td>'.$data12[satker].'</td>';
    echo '<td>'.$data12[shift_description].'</td>';
    echo '<td>'.$data12[nip].'</td>';
    echo '</tr>';

}

Solution

  • You don't need the $json_decode.Just loop through your array $data.

    remove the :

    $json = $data;
    $json_decoded = json_decode($json);
    

    and modify the foreach loop, adding the table tag.Also add single qoutes to your array indexes:

       echo '<table>';
        foreach($data as $data12){
                      echo '<tr>';
                    echo '<td>'.$data12['satker'].'</td>';
                    echo '<td>'.$data12['shift_description'].'</td>';
                    echo '<td>'.$data12['nip'].'</td>';
                  echo '</tr>';
    
            }
    
    echo '</table>';