2

This is my php so far. I have my main information added first and then my dates with users who have voted for that date.

$id = $CURUSER["id"];

$eventid = $_GET['eventid'];

$z = SQL_Query_exec("SELECT * FROM cal_events WHERE eventid = '$eventid'");
$rowz = mysql_fetch_array($z);
$y = SQL_Query_exec("SELECT userid FROM cal_votes WHERE eventid = '$eventid'"); 
$y1 = mysql_num_rows($y);
$x = SQL_Query_exec("SELECT userid FROM cal_votes WHERE eventid = '$eventid' AND voted = 'no'");    
$x1 = mysql_num_rows($x);

$data = array();
            $data['eventid'] = $eventid;
            $data['eventname'] = $rowz['eventname'];
            $data['aboutevent'] = $rowz['aboutevent'];
            $data['lefttovote'] =   $x1;
            $data['enddate'] = date("D jS F Y",strtotime($rowz[enddate]));

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $data['dates'][] =  date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){
                        $data['dates']['names'][] = "$rowb[forename] $rowb[surname],";
                            }
            }

echo json_encode($data);

Problem is my json is being return like this

    {"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013",
"dates":{"0":"Sat 23rd March 2013","
names":["John ,","Clare ,","Scott ,","Clare ,","Scott ,"],"1":"Sat 30th March 2013"}}

and im trying to output this. This is just a slung together example but im sure you will get the idea

 {"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013",
"dates":{"0":"Sat 23rd March 2013","
    names":["John,","Clare ,","Scott ,"}
"dates":{"1":"Sat 30th March 2013","
    names":["Clare ,","Scott ,"]}}

this is so i can loop through the dates and echo them out using jquery mobile. I can do it with straight php as i dont need to put them into an array but this array business is baffling

update *

$data = array();
            $data['eventid'] = $eventid;
            $data['eventname'] = $rowz['eventname'];
            $data['aboutevent'] = $rowz['aboutevent'];
            $data['lefttovote'] =   $x1;
            $data['enddate'] = date("D jS F Y",strtotime($rowz[enddate]));

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $date_data = array();
                $date_data[0] = date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){
                        $date_data['names'] = "$rowb[forename] $rowb[surname],";
                        array_push($data,$date_data);
                            }

            }

echo json_encode($data);

output

{"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013","0":{"0":"Sat 23rd March 2013","names":"John ,"},"1":{"0":"Sat 23rd March 2013","names":"Clare ,"},"2":{"0":"Sat 23rd March 2013","names":"Scott ,"},"3":{"0":"Sat 30th March 2013","names":"Clare ,"},"4":{"0":"Sat 30th March 2013","names":"Scott ,"}}

update working answer *

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $date_data = array();
                $date_data[0] = date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){

                        $date_data['names'][] = "$rowb[forename] $rowb[surname],"; 

                            }
                        array_push($data,$date_data);   
            }

echo json_encode($data);
4
  • What you are trying to output is not valid JSON. Why do you want to output invalid JSON? Commented Apr 21, 2013 at 16:50
  • I just cut and pasted it for an example. I know its not perfect but i thought it might give people an idea (edited my first post) Commented Apr 21, 2013 at 16:51
  • If is see this correctly, what you are trying to output will resolve in the key dates being used twice, the one will of course overwrite the other. Why wouldn't dates:[{"date":Sat 23rd..","names":".."},{"date":"Sat 30th ..","names":".."}] be the same? And there is something about using mysql_* functions. Commented Apr 21, 2013 at 16:57
  • updated my example with Juuga example, but not quite there Commented Apr 21, 2013 at 17:06

2 Answers 2

4

This wont work because you can't use the same name (i.e. dates) to more than one childeNode:

 { "eventid":"23",
   "eventname":"Mums Birthday",
   "aboutevent":"Curry Night Alton 7pm",
   "lefttovote":0,"enddate":"Wed 19th June 2013",
   "dates":{
           "0":"Sat 23rd March 2013",
           "names":["John Hunter,","Clare Kinnear,","Scott Kinnear,"
           },
   "dates":{
           "1":"Sat 30th March 2013",
           "names":["Clare Kinnear,","Scott Kinnear,"]
           }
  }

you should combine dates in an array like this:

 { "eventid":"23",
   "eventname":"Mums Birthday",
   "aboutevent":"Curry Night Alton 7pm",
   "lefttovote":0,"enddate":"Wed 19th June 2013",
   "dates":[{
           "date":"Sat 23rd March 2013",
           "names":["John Hunter,","Clare Kinnear,","Scott Kinnear,"]
           },
           {
           "date":"Sat 30th March 2013",
           "names":["Clare Kinnear,","Scott Kinnear,"]
           }]
  }

To acheive this you can do:

$n = 0;
$caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
    while($rowcaldates = mysql_fetch_array($caldates)){
        $data->dates[$n]->date =  date("D jS F Y",strtotime($rowcaldates[eventdates])); 
        $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
        $c1 = mysql_num_rows($b);
        while($rowb = mysql_fetch_array($b)){
            $data->dates[$n]->names[] = "$rowb[forename] $rowb[surname],";
        }
        $n++;
    }

echo json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou razzak your answer also works, Many thanks for your contribution
1

In the while-loop, make an array called, for example, $date_data. Store the date in $date_data[0] and the names in $date_data['names']. At the end if the while, push $date_data into the dates info with $data['dates][] = $date_date;

On a side note, you should not be putting $_GET variables directly into your queries. Make sure you use prepared statements instead, or escape the values in some way ;-)

3 Comments

I have updated the above with my close but not yet there example, Can you take a quick look and see if this is what you ment. Many thanks
$date_data['names'] = "$rowb[forename] $rowb[surname],"; should be $date_data['names'][] = "$rowb[forename] $rowb[surname],"; and array_push($data,$date_data); should be outside your $rowb while (but still inside the $rowcaldates while)
Thank you Juuga I updated code above just incase some one else needs a helping hand

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.