mcfmullen Posted May 30, 2010 Share Posted May 30, 2010 This si the code I currently have: <?php $result = mysql_query("SELECT * FROM Animals"); $url='.../animals/'; $numofrows = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'></td>"; echo "<br>"; echo "<td><a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>"; echo "<br>"; } ?> The resulting display resembles this: Photo name animal Photo name animal Photo name animal What I want is this: Photo Photo Photo Name Animal Name Animal Name Animal Any ideas as to how I can achieve this result? Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/ Share on other sites More sharing options...
mcfmullen Posted May 30, 2010 Author Share Posted May 30, 2010 I've tried using two separate calls but what I get is: PhotoPhotoPhotoPhotoPhotoNameNameNameName I'm running out of options... Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065344 Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2010 Share Posted May 30, 2010 Because your issue is in your presentation code, not your query, I'm going to move this thread to the php help section... It will likely get more responses there. Could you show in your desired output what you want in the same table cell and where you want to start new table rows. Also, how many columns across do you want before you start a new table row? Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065359 Share on other sites More sharing options...
jcbones Posted May 30, 2010 Share Posted May 30, 2010 <?php $result = mysql_query("SELECT * FROM Animals"); $url='.../animals/'; $numofrows = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; echo "<br>"; echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065393 Share on other sites More sharing options...
mcfmullen Posted May 30, 2010 Author Share Posted May 30, 2010 I've managed to get this: <?php $result = mysql_query("SELECT * FROM Animals"); $url='../animals/'; $numofrows = mysql_num_rows($result); echo "<table>"; while($row = mysql_fetch_array($result)){ echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; echo "<br>"; echo "<a href='http://www.cool.com/animals.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>"; echo "<br>"; } echo "</table>"; ?> The result displays what I want.... to a point. What happens is the output is given in only one row. I want 5 items (columns) per row and rows to display until no more items exist. I imagine a count variable would be required but I have no idea how to implement it with this code. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065441 Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2010 Share Posted May 30, 2010 http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065447 Share on other sites More sharing options...
mcfmullen Posted May 31, 2010 Author Share Posted May 31, 2010 Getting closer!! Here's what I've got: <?php $result = mysql_query("SELECT * FROM Animals"); $url='.../animals/'; $numofrows = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ $cols = 0; echo "<table><tr>"; while ($cols < 20) { echo ($cols % 5 == 0)? "</tr><tr>" : ""; echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; echo "<br>"; echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>"; $cols++; } echo "</tr></table>"; } ?> I am now getting 5 columns with the photo and name of animal as I want it. There's one catch: I get a 5x4 table for each animal (20+ tables!) and the same animal is inside each cell of that table! Ex Output: Cow / Cow / Cow / Cow / Cow Cow / Cow / Cow / Cow / Cow Cow / Cow / Cow / Cow / Cow Cow / Cow / Cow / Cow / Cow Horse / Horse / Horse / Horse / Horse Horse / Horse / Horse / Horse / Horse Horse / Horse / Horse / Horse / Horse Horse / Horse / Horse / Horse / Horse Can anyone guide me as to how to make ONE table with a different animal in each cell? I.E. 5x(# of animals) table? Wanted output: Cow / Horse / Sheep / Pig / Duck Cat / Dog / Goose / Turtle / Flamingo Elephant / Zebra / Giraffe / Lion / Llama .... and so on Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065497 Share on other sites More sharing options...
mcfmullen Posted May 31, 2010 Author Share Posted May 31, 2010 I've figured it out! For those curious, Final Code: <?php $columns = 5; $result = mysql_query("SELECT * FROM Animals"); $url='.../animals/'; $numofrows = mysql_num_rows($result); echo "<table>"; for($i = 0; $i < $numofrows; $i++) { $row = mysql_fetch_array($result); if($i % $columns == 0) { echo "<tr>"; } echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; echo "<br>"; echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $numofrows) { echo "</tr>"; } } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/#findComment-1065505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.