Jump to content

fetch and display json array data stored in database


michelle1404

Recommended Posts

I am storing a json data which come from server as a message for every 10 seconds. While displaying the data i have to separate it by its type. For single message i have done it but for multiple messages i am not getting how to separate each json message.

i have attached the sql dump 

messages.txt

 

Here is my code to print for single message

include('includes/Connection.php');
 

$l1 = $con->prepare("SELECT msg, title FROM messages ORDER BY id DESC limit 1");
$l1->execute();
$cntmsg = $l1->rowCount();
foreach($l1 as $r1)
{
    $m1 =  $r1['msg'];
$someJSON = $m1; 
$someArray = json_decode($someJSON, true);
  
  $pr1 = $someArray["data"]["commsettings"];
  foreach($pr1 as $MyIndex => $MyValue){

 echo $MyValue["name"];  
  }
}

It is working. But i should display for all the messages like this

$l1 = $con->prepare("SELECT msg, title FROM messages ORDER BY id DESC limit 4");
$l1->execute();

It wont work with my logic. How can i separate the array , i am not getting.

Link to comment
Share on other sites

Each json message appears to be in a separate row in the table. I don't understand what you mean by "how to separate each json message."

Do you mean just adding breaks in the output...

$l1 = $con->prepare("SELECT msg, title FROM messages ORDER BY id DESC limit 4");
$l1->execute();
$cntmsg = $l1->rowCount();
foreach($l1 as $r1) {
    $m1 =  $r1['msg'];
    $someJSON = $m1; 
    $someArray = json_decode($someJSON, true);

    $pr1 = $someArray["data"]["commsettings"];
    foreach($pr1 as $MyIndex => $MyValue) {
        echo $MyValue["name"] . '<br>';  
    }
    echo '<hr>';
}

giving

image.png.0aa1d13a45ee938c2e305a14dccef2fc.png

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.