How can I send looped data via json?

peace_love

I send data via json. It is working well:

    $.ajax({
        url: "json.php",
        type: "POST",
        dataType: "json",
        encode: true,
        data: {},
        success: function (data) {
            $(".blue").html(data.blue);
            $(".red").html(data.red);
        }
    });

json.php

$array['blue'] = "blue array";
$array['red'] = "red content";
echo json_encode($array);

My problem is now, that instead of..

blue array

...I want to send:

$pdo = $db->query('SELECT * FROM data;');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
    echo  "<li>".$row['name']."</li>";
}

is this possible?

MonkeyZeus

Here's an extremely basic example:

<?php
$json = array(
    'blue' => '',
    'red' => 'Empty red content or whatever'
);

$pdo = $db->query('SELECT * FROM data;');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
    $json['blue'].= "<li>".$row['name']."</li>";
}

echo json_encode($json);

Not sure what logic you would use to actually populate red but you can work it into the while loop or whatever.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I send data via express next() function?

From Dev

How can I send data to an iBeacon ?

From Dev

How can I send multipart/form-data that contains json object and image file via curl? And how can I treat it with Django Rest Framework?

From Dev

Oracle SQL - How can I write an insert statement that is conditional and looped?

From Dev

How can i send the json array to server with json?

From Dev

How can I send an Excel file via email?

From Dev

How can I send data to PHP script from SharedPreferences and display JSON in listview?

From Dev

How can I send a text string to another device via NFC?

From Dev

How can I send data to WebServer in Android?

From Dev

How many bytes can I send via POST / GET?

From Dev

How can I send the equivalent of this curl request via AJAX?

From Dev

How to make can.Model send JSON data when update?

From Dev

How can I merge two associative arrays that are being looped?

From Dev

How to send JSON data via checkbox value?

From Dev

How can i send data from json?

From Dev

How to scroll Looped Data?

From Dev

Oracle SQL - How can I write an insert statement that is conditional and looped?

From Dev

How can I send data to an API via the internet securely?

From Dev

How can we send json encoded data through ajax via form text input

From Dev

How can I send a php "echo" via Ajax?

From Dev

how can i send a dict in data of ajax

From Dev

Could I send JSON data with LoRa and how?

From Dev

How can I send fetching data from database via mail

From Dev

How can I send HTML data to PHP using JSON?

From Dev

TCPDF How can I create tables with looped content?

From Dev

how can I Send File via jQuery?

From Dev

How can I send data to aws lambda via aws gateway?

From Dev

How can I send data as a POST with a JSON body, and navigate to the response?

From Dev

How to send data as json format via textinput array

Related Related

  1. 1

    Can I send data via express next() function?

  2. 2

    How can I send data to an iBeacon ?

  3. 3

    How can I send multipart/form-data that contains json object and image file via curl? And how can I treat it with Django Rest Framework?

  4. 4

    Oracle SQL - How can I write an insert statement that is conditional and looped?

  5. 5

    How can i send the json array to server with json?

  6. 6

    How can I send an Excel file via email?

  7. 7

    How can I send data to PHP script from SharedPreferences and display JSON in listview?

  8. 8

    How can I send a text string to another device via NFC?

  9. 9

    How can I send data to WebServer in Android?

  10. 10

    How many bytes can I send via POST / GET?

  11. 11

    How can I send the equivalent of this curl request via AJAX?

  12. 12

    How to make can.Model send JSON data when update?

  13. 13

    How can I merge two associative arrays that are being looped?

  14. 14

    How to send JSON data via checkbox value?

  15. 15

    How can i send data from json?

  16. 16

    How to scroll Looped Data?

  17. 17

    Oracle SQL - How can I write an insert statement that is conditional and looped?

  18. 18

    How can I send data to an API via the internet securely?

  19. 19

    How can we send json encoded data through ajax via form text input

  20. 20

    How can I send a php "echo" via Ajax?

  21. 21

    how can i send a dict in data of ajax

  22. 22

    Could I send JSON data with LoRa and how?

  23. 23

    How can I send fetching data from database via mail

  24. 24

    How can I send HTML data to PHP using JSON?

  25. 25

    TCPDF How can I create tables with looped content?

  26. 26

    how can I Send File via jQuery?

  27. 27

    How can I send data to aws lambda via aws gateway?

  28. 28

    How can I send data as a POST with a JSON body, and navigate to the response?

  29. 29

    How to send data as json format via textinput array

HotTag

Archive