jQWidgets Forums

jQuery UI Widgets Forums Plugins Data Adapter php bug while sending dynamic data via json

This topic contains 3 replies, has 2 voices, and was last updated by  claudegel 10 years, 6 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author

  • claudegel
    Participant

    Hi,

    I’m writing a php script to send back data via json for an html file that contain a drop down list. The idea is to have a php script that can handle any type of request for select to be able to reuse that script for many different DataAdapter request, so field data number returned via json is not known in advance.

    I’ve made a test script making a request with two field on one mysql table.

    if I return the data like:

    while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){
    $resultat[] = array(
    $champ[$i] => $row[$champ[$i]], //$champ[$i] is containing the table field name
    $champ[$i+1] => $row[$champ[$i+1]]
    );
    }
    echo json_encode($resultat);

    I’m retriving the json data correctly in my html file but in that case I need to know in advance how many table field data I need to send back.
    Data retreived is :
    [{“nom”:”Yvan Perreault et Fils Inc.”,”nocli”:”227-1″},{“nom”:”Yvan Perreault et Fils Inc.”,”nocli”:”227″},….
    which is working well.
    But if I try to make a generic script to return an unknown number of table field data like this:

    while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){
    $result_array = “”;
    $i = 1;
    for ($i==1;$i<=$nbfield;$i++){ // $nbfield contain the number of field sent to the php script so I know how many to send back
    $result_array .= $champ[$i].” => “.$row[$champ[$i]].”,”; //in that case field name are nom and nocli
    }
    $result_array = rtrim($result_array,”,”); // to remove the last ‘,’
    $resultat[] = array($result_array);
    }
    echo json_encode($resultat);

    I’m getting a corrupted json data that do not work in the html file
    like this:
    [[“nom => Yvan Perreault et Fils Inc.,nocli => 227-1”],[“nom => Yvan Perreault et Fils Inc.,nocli => 227”],…

    any idea of what to change in my script ???

    php bug while sending dynamic data via json #64766

    Peter Stoev
    Keymaster

    Hi claudegel,

    I don’t know what’s wrong with your PHP script. We can help with things only related to our widgets and their own API and initialization code.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/


    claudegel
    Participant

    I’ve found the solution

    while($r = mysql_fetch_assoc($result)) {
    $resultat[] = $r;
    }
    echo json_encode($resultat);

    very simple, in case other need to know

    Thank you


    claudegel
    Participant

    Now that it’s working what should I do if the mysql result is empty
    does it cause an error with json_encode or should I send something else in order to not crash on the java side

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.