开发者

JSON Select option question?

I wrote somethinh like this,

`$sql = "SELECT * FROM dbase";

    $strOptions = "";

    if (!$q=mysql_query($sql)) {
        $strOptions = "<o开发者_C百科ption>There was an error connecting to database</option>";
    }
    if (mysql_num_rows($q)==0) {
        $strOptions = "<option>There are no news in database</option>";
    }else {
        $a=0;
        while ($redak=mysql_fetch_assoc($q)) {
            $a=$a+1;
            $vvvv[$a]["id"]=$redak["id"];
            $vvvv[$a]["ssss"]=$redak["ssss"];
        }
    }
    for ($i=1; $i<=$a; $i++) {
        $strOptions = $strOptions. '<option value="'. $vvvv[$i]["id"] .'">'.$i.'.) - '.strip_tags($vvvv[$i]["ssss"]).'</option>';
    }


    echo '[{ "message": "3" },{ "message": "' . count($wpdb->get_results("SELECT * FROM dbase")) . '" },{ "message": "'.$strOptions .'"}]';`

I just cannot later parse json file,later I parse it on this way to fill select-option

$jq("#select-my").children().remove();
        $jq("#select-my").append(data[2].message);

I use jquery form pluing,everything work fine,except this,I cant parse data for select-option element.I try and with json_encode in php.Can someone help please?


If I'm reading your PHP right, your JSON ends up looking like this:

[{ "message": "3" },{ "message": "(count_goes_here)" },{ "message": "(options_go_here)"}]

The top level of a JSON string must be an anonymous object, not array (details). So this is valid:

{"test": ["one", "two", "three"]}

but the array on its own is not:

["one", "two", "three"]

To fix it, just wrap your array in an object:

{"values": [{ "message": "3" },{ "message": "(count_goes_here)" },{ "message": "(options_go_here)"}]}

...and then when you've deserialized the JSON string, use the values property of the resulting object to access the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜