Help with getting an array (novice question)

Here I have this php snippet:

<?php
 include ("dbconfig.php");
   
  $sql = "SELECT Sta, Stb, Stc, Std From MyDb";
  $result = $conn->query($sql);

After this code, I’d like to produce an output like this from database:

$rows = array(
    array(91, 55, 78, 92),
    array(80, 87, 59, 64)
    );

My question is what code should I write here to get such a result? Thanks.

How do those values related to the information returned from the database? If they are just representative of the columns returned, row-by-row, then I would recommend you use PDO and the fetchAll() function, which will return what you need.

http://php.net/manual/en/pdostatement.fetchall.php

Here is the rest of the code:

$rows = array(
    array(91, 55, 78, 92),
    array(80, 87, 59, 64)
    );

  if($result->num_rows){
    while ($row = $result->fetch_object()) {

	
foreach ($rows as $row) {
    echo "<tr>";
    sort($row);
    $groups = array_reduce($row, function ($c, $v) { $c[(int)floor(($v-1) / 20)][] = $v; return $c; }, array());
    for ($i = 0; $i < 5; $i++) {
        echo "<td>" . implode(',', isset($groups[$i]) ? $groups[$i] : array()) . "</td>";
    }
    echo "</tr>\n";
}
}
  }

?>

I couldn’t make the connection with the db here. I tried your suggestion, but I couldn’t make it work. Instead of given values in $rows = array( …) , I want it to grab the data from the db.

There is almost nothing correct in this snippet.

First, there is no need for the if()–if there are no results, the loops will have nothing to loop over anyways.

Second, you fetch each row as object and then loop over the object (or rather something totally unrelated, as $rows is not coming from the DB), WTF? It suffices to foreach() over $result as mysqli results are iterable.

And because of that, the variables inside your foreach() are not what you expect them to be.

I’d just do something like

if ($result) { 
  $rows = $result->fetchAll();
  }

What happened when you tried? What did your code look like?

$rows = mysqli_fetch_assoc($result, MYSQLI_ASSOC);

I used this code. And I got many Invalid argument supplied for foreach() errors.

That’s not the function I suggested, but I didn’t realise you were using mysqli rather than PDO. I’m not sure what the equivalent is in mysqli. Perhaps, if you’re using the native driver:

http://php.net/manual/en/mysqli-result.fetch-all.php

But if your driver doesn’t support it, I’d loop through using something like:

if ($result) { 
  $rows = array();
  while ($row = $result->fetch_assoc()) { 
    $rows[] = $row;
    }
 }

First things first - can you get the query to run without errors and return some results? Your display loop seems quite complex, so first make sure the data is correct before you try to figure out any issues with that.

1 Like

You’re great! This worked perfectly.
The final code is this:

 <?php
 include ("dbconfig.php");
   
  $sql = "SELECT Sta, Stb, Stc, Std From MyDb";
  $result = $conn->query($sql);

 if ($result) { 
  $rows = array();
  while ($row = $result->fetch_assoc()) { 
    $rows[] = $row;
    }
 }
 
     foreach ($rows as $row) {
    echo "<tr>";
    sort($row);
    $groups = array_reduce($row, function ($c, $v) { $c[(int)floor(($v-1) / 20)][] = $v; return $c; }, array());
    for ($i = 0; $i < 5; $i++) {
        echo "<td>" . implode(',', isset($groups[$i]) ? $groups[$i] : array()) . "</td>";
    }
    echo "</tr>\n";
}
  
      echo "</table>";
?>

Thanks a lot, droopsnoot.

1 Like

are you looking for this: http://php.net/array-chunk ?

1 Like

Thanks Dormilich, I found my solution.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.