Jump to content

Array or loop to read data?


jeff5656

Recommended Posts

I am a beginner with php so I wanted to know if there was an easier way to do this.  I have maybe 30 variables so I don't want to type all that code out to display the records.  Can I use a loop or something to do this, without specifically having to name all the fields?  For instance query the database and spit out all fields then go to next row and spit out the next record:

Here would be the manual way (only included a few variables):

 

$query = "SELECT * ".
	"FROM sarcoid ".
	"ORDER BY l_name ";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc ($results)) {
?>
<td><?php echo nl2br ($row['dx']);?> </td>
    <td  > <?php echo nl2br ($row['pmhx']);?> </td>
    <td >  <?php echo $row['diet']; ?> </td>
     <td > etc.

Link to comment
https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/
Share on other sites

$query = "SELECT * FROM sarcoid ORDER BY l_name";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_assoc($result)) {
      foreach ($row as $value) {
        echo "<td>" . nl2br($value) . "</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.