jeff5656 Posted January 19, 2009 Share Posted January 19, 2009 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 More sharing options...
trq Posted January 19, 2009 Share Posted January 19, 2009 $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>"; } } } } Link to comment https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/#findComment-740741 Share on other sites More sharing options...
jeff5656 Posted January 19, 2009 Author Share Posted January 19, 2009 Thank you!! That worked perfectly (although I did have to change "v" to "value" and added a <tr> to advance the row -- wait I think OI'm going crazy now your code says $value - did you go back and edit it? Link to comment https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/#findComment-740753 Share on other sites More sharing options...
jeff5656 Posted January 19, 2009 Author Share Posted January 19, 2009 Ok maybe I'm pushing my luck here :-) how to I do the exact same thing for the field names (titles) themselves? I.e. spit out <th>l_name</th><th>f_name</th> etc? Link to comment https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/#findComment-740754 Share on other sites More sharing options...
trq Posted January 19, 2009 Share Posted January 19, 2009 Yeah, sorry, I edited it. I always use $v as a short for $value but figured it would be more descriptive for you if I actually named the variable properly. ps: We have a topic solved button in the bottom left corner for such occasions. Link to comment https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/#findComment-740755 Share on other sites More sharing options...
trq Posted January 19, 2009 Share Posted January 19, 2009 foreach allows you to loop through and retrieve keys and values, values alone is default. As an example.... foreach ($row as $key => $value) { echo "$key = $value\n"; } Have a crack at it from there and let us know how you go. Link to comment https://forums.phpfreaks.com/topic/141513-array-or-loop-to-read-data/#findComment-740759 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.