Closed

Nested Array to JSON in Php MySQL

This project received 2 bids from talented freelancers with an average bid price of $15 USD.

Get free quotes for a project like this
Employer working
Project Budget
N/A
Total Bids
2
Project Description

I have the following data in mysql table

MsgID|MsgBody|MsgDate|MsgParentId
1 | Parent Msg | 10/10/2012| null
2 |Reponse to parent Msg| 10/10/2012| 1
3 |Response to parent msg | 10/10/2012|1

I need the nested output(multidimensional array) in JSON like this:
[Messages:[Msgid, MsgBody,Msgdate, Replies[ MsgId,MsgBody,Msgdate]]

This will output all the messages and also the replies for each message as an array.


<?php
require_once("[url removed, login to view]");

$response = array();
$messages = array();


$query="SELECT * FROM (SELECT username, userpicurl, userlocation, userbio, msgbody, msgsenddate, msgpersonal, msgparentid, msgdateposted FROM tblMsg INNER JOIN tblUser ON [url removed, login to view] = [url removed, login to view] ORDER BY MsgId DESC LIMIT 0 , 10) AS tbl ORDER BY [url removed, login to view]";

$result = mysql_query($query,$con) or die('Errant query: '.$query);

while($row=mysql_fetch_array($result))
{
$username =$row['username'];
$picurl =$row['userpicurl'];
.....

$messages[] = array('username'=> $username,'picurl'=>$picurl,'location'=>$location,'bio'=>$bio, 'msgbody'=> $msgbody,'senddate'=>$senddate,'personal'=>$personal,'receiverid'=>$receiverid); }
$response['messages'] = $messages;
echo(json_encode($response));

?>

Looking to make some money?

  • Set your budget and the timeframe
  • Outline your proposal
  • Get paid for your work

Hire Freelancers who also bid on this project

    • Forbes
    • The New York Times
    • Time
    • Wall Street Journal
    • Times Online