0

I want to print json_encode array into a table format inside html.I tried the following method:

<table align="center">
   <tr>
        <td style="min-width: 400px; height: 400px; margin: 0 auto;margin-top:50px">
            <div id="container2" style="min-width: 400px; height: 400px; margin: 0 auto;margin-top:50px"></div>       
        </td>
        <td><?php echo json_encode($names1,JSON_NUMERIC_CHECK); ?></td>
    </tr>
</table>

but sadly its just giving the output in normal json_encode. How to print the json_encode output as a table?

TNK
  • 4,263
  • 15
  • 58
  • 81
  • Walk through each member using `foreach` and output it with proper `` and `` tags. – Pekka Mar 02 '13 at 12:50
  • see you cant use foreach loop with json_encode na? –  Mar 02 '13 at 12:56
  • Oh, it's ENcode. Why do you want to use `json_encode()` in the first place? What is your desired output? – Pekka Mar 02 '13 at 12:58
  • just to output the value got from mysql, to html near the graph –  Mar 02 '13 at 12:58
  • Why do you need `json_encode()` for that? Why not just output a normal table? – Pekka Mar 02 '13 at 12:59
  • itried its giving array to string conversion error. check the code i have added parts of the code. –  Mar 02 '13 at 13:04
  • "; echo $n; echo "";} ?> –  Mar 02 '13 at 13:05
  • while($row=mysql_fetch_array($query1)){ $names1[$indexb]=array($row['username'],$row['score']); $indexb++; } –  Mar 02 '13 at 13:06
  • sorry i am not finding how to add code to the question. –  Mar 02 '13 at 13:06
  • You should have an "edit" button. Anyway, using `json_encode()` is definitely the wrong way to go about this. I can't tell from your code above where the problem is, but outputting HTML is the way to go - it'll be worth trying to fix that. – Pekka Mar 02 '13 at 13:07

2 Answers2

1

This is not clear to me, but it sounds like you just want to display an array in HTML table format so I'm not sure what json_encode has to do with it.

However, this has been answered before:

How to create a HTML Table from a PHP array?

Community
  • 1
  • 1
Keith Morris
  • 2,125
  • 1
  • 19
  • 21
  • no i want to output the json_encode value into tables. simply echoing it will give the output in the following way [["Ahmed",6],["Aldein",3],["Allouh",2],["Salim",0]] –  Mar 02 '13 at 12:57
  • itried foreach but its giving me array to string conversion error –  Mar 02 '13 at 13:03
  • dued ur awesome..it was already given na ididn notice sorry... :) –  Mar 02 '13 at 13:25
0

If you want each value in it's own td element, then you will need to iterate over the array and output the td elements yourself. You can do this in php from the $names array, or in JavaScript on the client using a json array. I'd do it in php.

To output each pair in its own row

foreach ($names1 as &$value) {
Echo "<tr>"
Echo "<td>"
Echo  $value[0]
Echo "</td>
Echo "<td>"
Echo  $value[1]
Echo "</td>
Echo "<tr>
}
SteveP
  • 18,840
  • 9
  • 47
  • 60
  • ididn understand it properly..how ? imean how to get the json_encode element individually? –  Mar 02 '13 at 12:57
  • but its a two dimensional array. Its giving array to sting conversion error –  Mar 02 '13 at 13:18
  • see these are the values in the array...how to print them? [["Ahmed",6],["Aldein",3],["Allouh",2],["Salim",0]] –  Mar 02 '13 at 13:21