Jump to content

Help with Array to string conversion


mattix

Recommended Posts

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 by mattix
small additions.
Link to comment
Share on other sites

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. 

 

2020-06-30 21_29_46-Test - Opera.png

Edited by mattix
Link to comment
Share on other sites

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. 

 

 image.png.2090b73c590b448cd47bfa4946746076.png

 

I showed the expected table in horizontal view to prevent confusion. My real table is vertical in the code. 

 

 

Edited by mattix
Link to comment
Share on other sites

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]) : '&ndash;') . "</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";

image.png.3b2bceff4d0a9d8c232c5f970619599f.png

  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.