Вопрос

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?"}]}
Это было полезно?

Решение

How about an options table?

id, option, answer
1, yes, ''
2, no, ''
3, perhaps, ''

(your answer data is always empty so I've included it for consistency)

Then for each question you would have an options field with "1,2,3" for all the options "1,2" for just yes/no etc..

To implement this you could:-

$options=array();
if(!empty($r['options']))
{
    $sql="SELECT * FROM options_table WHERE id IN (".$r['options'].")";
    $result=mysql_query($sql);
    while($row=mysql_fetch_assoc($result){
       $options[]=$row;
    }
}

$rows[$page][] = array(
                'id' => $Qid,
                'type' => $type,
                'description' => $Qdesc,
                'options'=>$options);

This way you could add options to your hearts content

Другие советы

This is not an entire answer, but to help you start off, you could have:

json_encode(array("pages" => array(1 => $row1, 2=>$row2)));

Generally you can achieve multilevel nesting using array(..) within another array(..). Like:

json_encode(
    array(
        $item1,
        $item2,
        array(
            "key1" => array(
                    1,
                    2,
                    array(
                        "inKey1" => array(4,5,6)
                    )
            )
        )
    )
);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top