Reputation: 171
I'm probably really overlooking something simple but this just won't work, I guess it's just me being stupid when trying to split the array.
Basically the below just gets the ID number, then selects the data from the DB. This works fine and the PDF prints at the end but there's no data on it.
If I change the json_decode
to json_encode
then it gives me just the first part of the array (but obviously it is not decoded so has some brackets and slashes where it shouldn't).
However I cannot get it to display all the rows. Can someone point out where I've gone wrong?
<?php
//etc etc
$idgg = $_GET['idgg'];
$query = "select * from `all_quotes` where quote_id='1493293451_1'";
if ($result = mysqli_query($link2, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
$data = $row["data"];
$qdata = json_decode($data);
foreach($qdata as $trow){
$prods = explode('|', $trow);
$rowall = "<tr><td>$prods[0]</td><td>$prods[1]</td><td>$prods[3]</td>
<td>$prods[2]</td></tr>";
}
}
}
require_once '/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('<table>'.$rowall.'</table>');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("test.pdf");
?>
Array looks like:
["ABC|Lots of description here|31.12|ETC","DEF|Even more....|30.57|ETC"].
Also, I've noticed that if I don't use the $_GET
(i.e. just input the id
into the code instead) then I get the first result, but not the rest. Why on earth does that make any difference?!
Thanks in advance...
Updated code, added different mysqli structure. The above gives me the first row of the data (i.e. the first part of the array)
Upvotes: 1
Views: 1715
Reputation: 823
You are close to getting it right. You fixed the mysqli_fetch_assoc()
issue, but when you did that you brought back the earlier error of overwriting the generated string. The correct loop, as far as I can tell, should be:
if ($result = mysqli_query($link2, $query)) {
$rowall = ""; // Start out with an empty string
while ($row = mysqli_fetch_assoc($result)) {
$data = $row["data"];
$qdata = json_decode($data);
foreach($qdata as $trow){
$prods = explode('|', $trow);
// Here you were overwriting the string in each iteration, since you only used "=", not ".="
$rowall .= "<tr><td>$prods[0]</td><td>$prods[1]</td><td>$prods[3]</td>
<td>$prods[2]</td></tr>";
}
}
}
If this still only gives one row, then run the SQL query directly in your database and make sure that it really does return more than one row.
Upvotes: 1
Reputation: 26
Try First thing you should try the link and check whether your json object is correct
Check what json object are so that you know what you r dealing
Normally a json string when passed to json_decode it will return the object as for.e.g consider
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
/* output
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
} */
whereas
var_dump(json_decode($json, true));
/* output
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}*/
This return associative array which can the be used by you.
Or else I suspect the data maybe stored in serialized format for which you can use unserialize function
Upvotes: 0