0

I'm getting this JSON result from mySQL with the following php query:

/* grab the posts from the db */
 $query = "SELECT * FROM ko_timetable";
 $result = mysql_query($query,$link) or die('Errant query:  '.$query);

 /* create one master array of the records */
 $posts = array();

if(mysql_num_rows($result)) {

while($post = mysql_fetch_assoc($result)) {
      $posts[] = array('post'=>$post);
    } 
}

JSON Result:

post =         {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
}
{
post =         {
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
post =         {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};

But how can I change this query to get this result group by "DAY_OF_WEEK". See example below, thanks for your help:

{
Sunday =         {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
{
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
Monday =         {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};

Thanks

4
  • Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented May 4, 2012 at 8:42
  • Also, whatever this is, it's not JSON. Commented May 4, 2012 at 8:44
  • @Truth, the question is How can I change this query to get this result group by "DAY_OF_WEEK"?... Commented May 4, 2012 at 8:44
  • @HernandoZ: This is not JSON response. Please show us the genuine JSON output. Commented May 4, 2012 at 9:33

2 Answers 2

2

You can use DAY_OF_WEEK as array index like this,

while($post = mysql_fetch_assoc($result)) {
      $posts[$posts["DAY_OF_WEEK"]] = array('post'=>$post);
    }

But remember that in the example you have shown the indexes of array are names of the days then the result of previous Sunday will be replaced with this weeks Sunday and so on so forth. You could also use a count variable to avoid replacing of keys with duplicate names, like this.

$count = 1;
while($post = mysql_fetch_assoc($result)) {
      $posts[$posts["DAY_OF_WEEK"].$count] = array('post'=>$post);
      $count++;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , is perfect , how can I tag "DAY_OF_WEEK" so it shows DAY = Monday ({ post = ..... Thanks
Can you show an example how you want Json to be displayed because I am not getting your point.
1

change this:

while($post = mysql_fetch_assoc($result)) {
    $posts[ $post['DAY_OF_WEEK'] ] = $post;
}

2 Comments

That would overwrite duplicate DAY_OF_WEEK array keys. For example, if it adds $post to $posts['sunday'] and then tries to add a different post which also uses sunday, the first one is over-written. Instead try: $posts[$post['DAY_OF_WEEK']][] = $post;
@acido69 Cillosis is right , cillosis is you summit your answer i'll accepted , thanks

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.