I am using ajax to get a json response and fill my datatable however for some reason some random values in the json have a line break. In the database the value will be lets say "90 g per 100 m of row" but it outputs in the json like below.
All other rows are fine and here is how I make the json object.
for($i = 0; $i < $numRows; $i++) {
$chemicals = getPestsChemicals($connection, $rows[$i]['code']);
$chemNames = '';
foreach($chemicals as $chem) {
$chemNames .= $chem['chemical'].'<br>';
}
$mrls = getPestsMrls($connection, $rows[$i]['code']);
$mrlNames = '';
foreach($mrls as $mrl) {
$mrlNames .= (strlen($mrl['mrl']) == 1 ? $mrl['mrl'].".0" : $mrl['mrl']).'<br>';
}
if($i != $numRows-1) {
$body .= '
{
"crop": "'.$rows[$i]['crop'].'",
"diseases": "'.$rows[$i]['pest_name'].'",
"chemical": "'.$chemNames.'",
"product": "'.$rows[$i]['product']." ".($rows[$i]['footnote'] != NULL ? '<sup class=\"text-danger font-weight-bold\">'.$rows[$i]['footnote'].'</sup>' : NULL ).'",
"rate": "'.$rows[$i]['rate'].'",
"max_no": "'.$rows[$i]['max_no'].'",
"hi": "'.$rows[$i]['hi'].'",
"mrl": "'.$mrlNames.'",
"pcs_no": "'.($rows[$i]['pcs_no'] == 0 ? 'DR' : '0'.$rows[$i]['pcs_no']).'",
"supplier": "'.$rows[$i]['supplier'].'",
"use_by_date": "'.$rows[$i]['use_by_date'].'"
},
';
} else {
$body .= '
{
"crop": "'.$rows[$i]['crop'].'",
"diseases": "'.$rows[$i]['pest_name'].'",
"chemical": "N/A",
"product": "'.$rows[$i]['product']." ".($rows[$i]['footnote'] != NULL ? '<sup class=\"text-danger font-weight-bold\">'.$rows[$i]['footnote'].'</sup>' : NULL).'",
"rate": "'.$rows[$i]['rate'].'",
"max_no": "'.$rows[$i]['max_no'].'",
"hi": "'.$rows[$i]['hi'].'",
"mrl": "N/A",
"pcs_no": "'.($rows[$i]['pcs_no'] == 0 ? 'DR' : '0'.$rows[$i]['pcs_no']).'",
"supplier": "'.$rows[$i]['supplier'].'",
"use_by_date": "'.$rows[$i]['use_by_date'].'"
}
';
}
}
I have tried to wrap the relevant row in strval
or json_encode
but no success. Also here is how it looks in the db