Jump to content

foreach loop reiterating the same record in an array


jakatu

Recommended Posts

I am trying to call each record in a user database table and display them in a member directory for my site and I only get the first users information for every record in my table.

<?php
$query = "SELECT id FROM users ORDER BY company ASC";
$result = mysql_query($query);
confirm_query($result);
$row = mysql_fetch_array($result);
foreach($row as $id){
$query = "SELECT name, address, city, state, zip, phone, fax, email, company FROM users WHERE id = $id";
$result = mysql_query($query);
confirm_query($result);
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$company = $row['company'];
?>
<td valign="top" style="font-size:12px; text-align:center">
<table>
<tr>
<td><?php echo $name; ?></td>
</tr>
<tr>
<td><?php echo $company; ?></td>
</tr>
<tr>
<td><?php echo $email; ?></td>
</tr>	
</table>
</td>
<?
}
?>

I have tried doing a while loop and get the right id numbers back from the initial query but I have frankly never been able to get foreach right and I just want to finally do it. Any help would be appreciated.

 

<?php
   $query = "SELECT id, name, address, city, state, zip, phone, fax, email, company FROM users ORDER BY company ASC";
   $result = mysql_query($query);
   confirm_query($result);
   while($row = mysql_fetch_array($result)){
      $name = $row['name'];
      $email = $row['email'];
      $company = $row['company'];
?>
   <td valign="top" style="font-size:12px; text-align:center">
   <table>
   <tr>
   <td><?php echo $name; ?></td>
   </tr>
   <tr>
   <td><?php echo $company; ?></td>
   </tr>
   <tr>
   <td><?php echo $email; ?></td>
   </tr>   
   </table>
   </td>
<?
   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.