Encoding Json from MySQL
===

So I am trying to encode to JSON from MySQL and I need it in a [pagenumber][id,type,description][answerid,answerdescription] format. The goal of this is to read the data in a javascript file that will generate a multi step poll for me.

I will try to draw a pseudocode on how I want it to look right here:

    {"pages":
      [{1:
        [{"id":1,"text":"U mad?","options":
          [{"opt_id":1,"option":"yes","answer:''"},
           {"opt_id":2,"option":"no","answer:''"},
           {"opt_id":3,"option":"perhaps","answer:''"}]},
           {"id":2,"text":"Got it?","options":
        [{"opt_id":1,"option":"yes","answer:''"},
           {"opt_id":2,"option":"no","answer:''"}]
        }]
       },
       {2:
        [{"id":3,"text":"Help me?","options":
         [{"opt_id":1,"option":"yes","answer:''"},
           {"opt_id":2,"option":"no","answer:''"},
           {"opt_id":3,"option":"perhaps","answer:''"}]},
         {"id":4,"text":"Please?","options":
          [{"opt_id":1,"option":"yes","answer:''"},
           {"opt_id":2,"option":"no","answer:''"}]
        }]
      }]
    }	

This is what I got so far, but I can't seem to think of a way to add the 3rd "dimension" to this, I want an array of [id => (int), description => (string)] attached to each question. And each question needs room for several answers connected to them. The last column in the answers/options array is for text strings (most answers are answered by ID numbers, but some are textareas that needs whole strings). this might not be needed as I can probably send the form results back by serializing.

    $rows = array();

	while($r = mysql_fetch_assoc($sth)) 
	{
		$Qid = $r['id']; 
		$page=$r['page']; 
		$type=$r['type']; 
		$Qdesc=$r['description'];
		
		$rows[$page][] = array(
						'id' => $Qid,
						'type' => $type,
						'description' => $Qdesc);
    }

The result of this is the following (first 3 pages).

    {
    "1":[
      {"id":"2","type":"1","description":"U mad?"},
      {"id":"3","type":"1","description":"Got it?"},
      {"id":"4","type":"1","description":"Help me?"}],
    "2":[
      {"id":"5","type":"1","description":"Please?"},
      {"id":"6","type":"1","description":"Any clues?"}],
    "3":[
      {"id":"7","type":"2","description":"Foobar?"}]}