Problem in formatting in server side

I need some help please how can I create this in my php script in order to achieve this kind of format

[[{v:'1', f:'1'}, '',],[{v:'2', f:'2'}, '1'],[{v:'3', f:'3'}, '1'],[{v:'4', f:'4'}, '3']

here is my attempt but no luck

         $cmd = $this->connection->prepare('SELECT l_id,p_id from prog_tbl');
         $records = $cmd->fetchAll(PDO::FETCH_ASSOC);


      foreach($records as $row) {
           $rowdata[]= ['{v:'.$row['l_id'].',f: $row['l_id']}'.$row['p_id']];

      echo json_encode($rowdata);

Here is data in my table

      l_id        p_id
      2           null
      3           2
      4           2
      5           3
      6           3

Thank you in advance.

To clarify, do you want each element of the rowdata array to contain a string that looks similar to the one you show at the top, or does that show strings for the first four elements of the array, or do you intend it to be a two-dimensional array where each element contains a further array? If it’s the second of those, would this do it?

$rowdata[]= "[[{v:'" . $row['l_id'] . "', f:'" . $row['l_id'] . "'}, '" . $row['p_id'] . "']";

I’m confused that there’s a comma just before the close square-brace in the first but not in any of the others. Also there’s probably a way to do the above without repeatedly opening and closing quotes. And the data in your table doesn’t seem to be the same as in the sample of what you need - there’s no row where ‘l_id’ has a value of 1 but I assume that’s not relevant.

If the above doesn’t help, can you clarify exactly what “no luck” means - doesn’t run, runs but gives errors, runs but doesn’t produce what you want, and so on?


This is what I want to achieve. format

In the data.addRows(),how to do that format if I will fetch the record from my database,

Thank you in advance.

So does the code I put above get any closer? And if not, what happens, do you get error messages?

Sorry, but I’m on limited bandwidth so haven’t really got enough spare to go off around the web reading up on your specifications, but happy to try to help if you post on here what you’re trying to achieve and exactly what problems you’re having.

You don’t need to create the JSON by hand as you’re doing here, the json_encode function will convert associative arrays into JSON objects, and numeric arrays into JSON arrays.

Try this:

foreach($records as $row) {
    $rowdata[] = [['v' => $row['l_id'], 'f' => $row['l_id']], (string) $row['p_id']];

Note that I’m casting the value of $row[‘p_id’] to a string, so that null values will be output as empty strings.


It works, Thank you so much :).


can I output instead of double quote into single quote like this

[[{v:'2', f:'<img src="img/man.gif" /><br />2'}, '', '1']

Thank you in advance.

If you put your HTML string into the array like that, json_encode will take care of properly escaping it, and you’ll end up with output like this:

[[{"v":"2","f":"<img src=\"img\/man.gif\" \/><br \/>2"},"","1"]]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.