tags:

views:

89

answers:

2

Hello,I wrote somethinh like this,

`$sql = "SELECT * FROM dbase";

    $strOptions = "";

    if (!$q=mysql_query($sql)) {
        $strOptions = "<option>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?

A: 

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.

T.J. Crowder
Give me a second to try,sorry I'm new at jquery and json,tnx for answer
A: 

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

I wrap it on this way,like your example Crowder,and call it on this way

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

Still dont work for me. :( Options are well formed,it works for me on ordinary textResponse,but with json dont work.

(You want to post follow-ups by editing your question [you can edit your question any time.] and/or posting a comment on someone's answer, rather than by adding an answer.) Shouldn't that be `data.dataValues[2].message` rather than just `dataValues[2].message`? Since `data` is the top-level object and `dataValues` is a property of it.
T.J. Crowder
Sorry,my mistake,I tried also and data.dataValues[2].message.I gave up for now :(