mattix Posted June 30, 2020 Share Posted June 30, 2020 (edited) How can I get rid of "Notice:Array to string conversion on line 78" error from this code? can i get a little help please? This is line 78 : $table .= $columnValue; Basically this code's supposed to do is to group the values from grpa, grpb of datatb to 4 different categories according to their value. and as end result, category index number of each category is put into the html table. Thanks. <?php include ("configd.php"); $sql = "SELECT grpa, grpb From datatb"; $result = $conn->query($sql); //categories $cat = [ 4 => [1, 2, 3, 4], 3 => [5, 6], 2 => [7, 8], 1 => [9, 10] ]; while ($row = $result->fetch_assoc()) { foreach ($cat as $catKey => $catValue) { $columns[$catKey] = array_filter($row, function ($col) use ($cat, $catKey) { if (in_array($col, $cat[$catKey])) { return true; } }); } $output[] = $columns; } // the output $table = <<<EOT <table class='maintable'> <tr><th>9:10</th></tr> <tr><th>7:8</th></tr> <tr><th>5:6</th></tr> <tr><th>1:4</th></tr> </table> EOT; foreach ($output as $row) { $table .= "<table class='maintable'>"; foreach ($row as $columnKey => $columnValue) { $table .= "<tr>"; $table .= "<td>"; $table .= $columnValue; $table .= "</td>"; } $table .= "</tr>"; $table .= "</table>"; } echo $table; ?> Edited June 30, 2020 by mattix small additions. Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/ Share on other sites More sharing options...
gw1500se Posted June 30, 2020 Share Posted June 30, 2020 You don't say what data type '$columnValue' is so I am assuming it is int. $table .= strval($columnValue); 1 Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579286 Share on other sites More sharing options...
mattix Posted June 30, 2020 Author Share Posted June 30, 2020 true. thanks. but even though i made the change you proposed, i still get the same error. Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579288 Share on other sites More sharing options...
Barand Posted June 30, 2020 Share Posted June 30, 2020 perhaps $table .= join(',', $columnValue); 1 Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579289 Share on other sites More sharing options...
mattix Posted June 30, 2020 Author Share Posted June 30, 2020 yes, this doesn't give error. but it doesn't list the category keys, it lists category values. any ideas there too? if not, thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579290 Share on other sites More sharing options...
Barand Posted June 30, 2020 Share Posted June 30, 2020 $table .= $columnKey ? Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579300 Share on other sites More sharing options...
mattix Posted June 30, 2020 Author Share Posted June 30, 2020 (edited) I tried this before, it lists all category keys in the html table. It's supposed to list only the category key numbers that matches the value, if not the output to be blank. Edited June 30, 2020 by mattix Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579303 Share on other sites More sharing options...
Barand Posted June 30, 2020 Share Posted June 30, 2020 Why don't you reveal the data in the datatb table so we can see the input. Then show us what the output should look like from that data. That way we might able to see the path from one to the other. Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579306 Share on other sites More sharing options...
mattix Posted June 30, 2020 Author Share Posted June 30, 2020 (edited) This is the data on the table. id, grpa, grpb 1, 1, 5 2, 4, 6 3, 8, 9 4, 3, 3 5, 5, 4 6, 7, 8 7, 9, 10 8, 10, 5 9, 10, 9 and this the desired table from the original table. I showed the expected table in horizontal view to prevent confusion. My real table is vertical in the code. Edited June 30, 2020 by mattix Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579307 Share on other sites More sharing options...
Barand Posted June 30, 2020 Share Posted June 30, 2020 I created an extra table to define which category the values were in mysql> select * from catval; +-----+------+ | val | cat | +-----+------+ | 1 | 4 | | 2 | 4 | | 3 | 4 | | 4 | 4 | | 5 | 3 | | 6 | 3 | | 7 | 2 | | 8 | 2 | | 9 | 1 | | 10 | 1 | +-----+------+ then $sql = "SELECT a.cat as cata , b.cat as catb FROM datatb d JOIN catval a ON d.grpa = a.val JOIN catval b ON d.grpb = b.val "; $result = $db->query($sql); //categories $cat = [ 4 => ['name'=>'1:4', 'recs'=>[]], 3 => ['name'=>'5:6', 'recs'=>[]], 2 => ['name'=>'7:8', 'recs'=>[]], 1 => ['name'=>'9:10','recs'=>[]] ]; $n = 0; while ($row = $result->fetch_assoc()) { $cat[$row['cata']]['recs'][$n][] = $row['cata']; $cat[$row['catb']]['recs'][$n][] = $row['catb']; $n++; } // the output echo "<table border='1' style='width:500px; border-collapse:collapse;'>"; foreach ($cat as $c) { echo "<tr><th>{$c['name']}</th>"; for ($i=0; $i<$n; $i++) { echo '<td style="text-align:center;">' . (isset($c['recs'][$i]) ? join(',', $c['recs'][$i]) : '–') . "</td>"; } echo "</tr>\n"; } echo "</table>\n"; 2 Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579311 Share on other sites More sharing options...
mattix Posted July 1, 2020 Author Share Posted July 1, 2020 Thank you, Barand. This is all new. I'll work on this. Quote Link to comment https://forums.phpfreaks.com/topic/311022-help-with-array-to-string-conversion/#findComment-1579317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.