michelle1404 Posted September 24, 2021 Share Posted September 24, 2021 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 24, 2021 Share Posted September 24, 2021 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.