2

i had this php code :

<?php
    include "../mainmenu/koneksi.php";
    // Start with the list of animals
    $sql = "SELECT * FROM data_binatang";
    $rows = array();
    $res = mysql_query($sql);
    for($i=0; $i<mysql_num_rows($res); ++$i){
        $row1 = mysql_fetch_assoc($res);
        $id_binatang = $row1['id_binatang'];
        $sql = "SELECT * FROM data_waktu_vaksinasi WHERE id_binatang = $id_binatang AND (status_vaksin = 'belum' OR status_vaksin IS NULL) ORDER BY tanggal_vaksin ASC LIMIT 1";
        $res2 = mysql_query($sql);
        $row2 = mysql_fetch_assoc($res2);
        $arr[$id_binatang] = array();
        array_push($arr[$id_binatang], $row1['nama_binatang'], $row1['id_user'], $row1['jenis_binatang'], $row1['ras_binatang'], $row1['foto_binatang'], $row2['nama_vaksin'], $row2['id_data_waktu_vaksinasi'], $row2['status_vaksin'], $row2['tanggal_vaksin'], $row2['tanggal_datang']);
    }

    echo "RESULT:";
    echo "<table border=1><tr><th>id binatang</th><th>nama binatang</th><th>id user</th><th>jenis binatang</th><th>ras binatang</th><th>foto binatang</th><th>nama vaksin</th><th>id data waktu vaksin</th><th>status vaksin</th><th>tanggal vaksin</th><th>tanggal datang</th></tr>";
    foreach($arr as $key => $val){
        echo "<tr><td>$key</td><td>".implode("</td><td>", $val)."</td></tr><br>";
    }

?>

and here's the result

now i want to generate the table into json, but i don't know what to put inside the json encode, i tried:

echo '{"data_vaksinasi_menu":'.json_encode($arr[$id_binatang]).'}';

but instead it gave me null

1 Answer 1

3

Try this:

echo json_encode(array('data_vaksinasi_menu' => $arr));
Sign up to request clarification or add additional context in comments.

3 Comments

nice! thank you, now i want to put the name for every value. Do you know how to do it?
the json only generate the value, but i want to generate the key too
Hope you need this If I understands you. update the push in for loop like below. array_push($arr[$id_binatang]['data_vaksinasi_menu'], $row1['nama_binatang'], $row1['id_user'], $row1['jenis_binatang'], $row1['ras_binatang'], $row1['foto_binatang'], $row2['nama_vaksin'], $row2['id_data_waktu_vaksinasi'], $row2['status_vaksin'], $row2['tanggal_vaksin'], $row2['tanggal_datang']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.