从MySQL编码Json [英] Encoding Json from MySQL

查看:130
本文介绍了从MySQL编码Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试从MySQL编码为JSON,并且需要[pagenumber] [id,类型,描述] [answerid,answerdescription]格式.这样做的目的是读取javascript文件中的数据,该文件会为我生成一个多步轮询.

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:''"}]
    }]
  }]
}   

这是到目前为止我得到的,但是我似乎想不出一种向其添加第三个维度"的方法,我想要一个[id =>(int),description =>(string的数组)]附加到每个问题.每个问题都需要空间来容纳与之相关的多个答案. Answers/options数组的最后一列用于文本字符串(大多数答案由ID号回答,但有些是需要整个字符串的textarea).可能不需要,因为我可能可以通过序列化将表单结果发送回去.

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);
}

结果如下(前3页).

{
"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?"}]}

推荐答案

选项表如何?

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

(您的答案数据始终为空,因此为了保持一致性我将其包括在内)

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

然后对于每个问题,您将有一个选项字段,其中所有选项"1,2"(仅是/否)都带有"1,2,3".

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

这篇关于从MySQL编码Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆