简体   繁体   中英

How do I append on to the array in the while loop: $rows[] = $row;?

I need help, I tried myself to the fetched rows but cannot to get it appended on to the array in the while loop: $rows[] = $row; then try to just build up the $rows array then output in the foreach later.

How do i do this?

 <?php
    $query = $dbc->query('SELECT * FROM customer');
    $rows = array();
    while ($row = $query->fetch_assoc()) {
        echo $row['clientid'].' '.$row['inital'].' '.$row['firstname'].' '.$row['lastname'].' '.$row['mobile'].' '.$row['landline'].' '.$row['email'].' '.$row['address'].' '.$row['postcode'];
    }
 foreach ($rows as $row) { 
 ?>
  <td><?php $row['clientid']; ?></td>
  <td><?php $row['inital']; ?></td> 
  <td><?php $row['firstname']; ?></td>
  <td><?php $row['lastname']; ?></td>
  <td><?php $row['mobile']; ?></td>
  <td><?php $row['landline']; ?></td>
  <td><?php $row['email']; ?></td>
  <td><?php $row['address']; ?></td>
  <td><?php $row['postcode']; ?></td>
  </td>
  <?php } ?>

Eventually I did it this way,

   <?php
  $query = $dbc->query('SELECT * FROM customer');
if ( mysqli_num_rows( $query ) > 0 )
{
  while ( $row = mysqli_fetch_array( $query, MYSQLI_ASSOC ))
  {
 echo "<tr><td>".$row['clientid']."</td><td>".$row['inital']."</td><td>".$row['firstname']."</td><td>".$row['lastname']."</td><td>".$row['mobile']."</td><td>".$row['landline']."</td><td>".$row['email']."</td><td>".$row['address']."</td><td>".$row['postcode']."</td></tr>";
  }
  echo '</tbody></table>' ;
}
else { echo '<div class="alert alert-error">
There are currently no records.
</div>' ; }
    ?>    

I don't know why you want all records in $rows & use foreach redundantly two times still you can do like,

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

Your variable $rows is initialized as an array, but is not being used.

while ($row = $query->fetch_assoc()) {
    $rows[] = $row;
}

This will create an array of all your $row

Please use enter and tab.

Also, why do you want to build the rows array? fetch_assoc does that for you

<?php while ($row = $query->fetch_assoc()) { ?> 
    <td><?php echo $row['clientid']; ?></td> 
    <td><?php echo $row['inital']; ?></td> 
    <td><?php echo $row['firstname']; ?></td> 
    <td><?php echo $row['lastname']; ?></td> 
    <td><?php echo $row['mobile']; ?></td> 
    <td><?php echo $row['landline']; ?></td> 
    <td><?php echo $row['email']; ?></td> 
    <td><?php echo $row['address']; ?></td> 
    <td><?php echo $row['postcode']; ?></td> 
    <td><?php echo $row['accessibility']; ?></td> 
<?php } ?>

Or, if you want to do it in 2 steps

$rows = $query->fetch_all();

...

<?php foreach ($rows as $row) { ?> 
    <td><?php echo $row['clientid']; ?></td> 
    <td><?php echo $row['inital']; ?></td> 
    <td><?php echo $row['firstname']; ?></td> 
    <td><?php echo $row['lastname']; ?></td> 
    <td><?php echo $row['mobile']; ?></td> 
    <td><?php echo $row['landline']; ?></td> 
    <td><?php echo $row['email']; ?></td> 
    <td><?php echo $row['address']; ?></td> 
    <td><?php echo $row['postcode']; ?></td> 
    <td><?php echo $row['accessibility']; ?></td> 
<?php } ?>

Answering your question:

while ($row = $query->fetch_assoc()) {
    $rows[] = $row;
    echo $row['clientid'].' '.$row['inital'].' '.$row['firstname'].' '.$row['lastname'].' '.$row['mobile'].' '.$row['landline'].' '.$row['email'].' '.$row['address'].' '.$row['postcode'].' '.$row['accessibility'];
}

However, you may take another approach to this. Why not simply using the while loop to print the rows?

while ($row = $query->fetch_assoc()) {
?>
<td><?php $row['clientid']; ?></td>
<td><?php $row['inital']; ?></td> 
<td><?php $row['firstname']; ?></td>
<td><?php $row['lastname']; ?></td>
<td><?php $row['mobile']; ?></td>
<td><?php $row['landline']; ?></td>
<td><?php $row['email']; ?></td>
<td><?php $row['address']; ?></td>
<td><?php $row['postcode']; ?></td>
<td><?php $row['accessibility']; ?></td>
<?php } ?>

Or, if you just need the rows in an array, you can use fetchAll() , like this:

$rows = $query->fetchAll(PDO::FETCH_ASSOC);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM