thomashw Posted January 5, 2008 Share Posted January 5, 2008 I'm attempting to make a table with one row and 3 columns (holding three different pictures in all.) I want each picture URL to be called from my database. When I use the following code, it places the same picture across the three columns and does this three times (creating three rows.) I want a different picture to be placed across the three columns and to have only one row. What can I do? Here is the code: $result = @mysql_query('SELECT image FROM specials'); $num=mysql_numrows($result); $i = 0; while ($i < $num) { $image=mysql_result($result,$i,"image"); ?> <table> <tr> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> </tr> <? $i++; } echo "</table>"; ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/ Share on other sites More sharing options...
psychowolvesbane Posted January 5, 2008 Share Posted January 5, 2008 Can you make an example and post as an image for us to see what it's meant to look like? Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431481 Share on other sites More sharing options...
thomashw Posted January 5, 2008 Author Share Posted January 5, 2008 It currently looks like the top, but I want it to look like the bottom. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431484 Share on other sites More sharing options...
psychowolvesbane Posted January 5, 2008 Share Posted January 5, 2008 Try: $result = @mysql_query('SELECT image FROM specials'); $num=mysql_numrows($result); $i = 0; ?> <table> <tr> <?php while ($i < $num) { $image=mysql_result($result,$i,"image"); ?> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <php? $i++; } ?> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431497 Share on other sites More sharing options...
thomashw Posted January 5, 2008 Author Share Posted January 5, 2008 Hmm... Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431503 Share on other sites More sharing options...
psychowolvesbane Posted January 6, 2008 Share Posted January 6, 2008 I think this might work: $result = @mysql_query('SELECT image FROM specials'); $num=mysql_numrows($result); $i = 0; ?> <table> <tr> <?php while ($i < $num) { $image=mysql_result($result,$i,"image"); ?> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> </tr> <tr> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <td> <img src="<? echo "$image"; ?>"> </td> <php? $i++; } ?> </tr> </table> However this is not the most efficient method but it will work as long as you only want 6 images. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431508 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 Actually both are just placing the same image beside eachother but now in a different format. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431510 Share on other sites More sharing options...
psychowolvesbane Posted January 6, 2008 Share Posted January 6, 2008 Try looking at this: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html I'm trying to get something similar to you done but a lot more complicated, and still haven't got it done yet. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431516 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 What about this: <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { echo "<td><img src='{$row['image']}' /></td>"; }} echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431522 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 Thanks, that works quite well. I'm trying to get it so when there is more than three items in the database, it will start a new row. I've edited your script to look like this, but it's not working properly. It inserts ""<tr><td><img src='{$row['image']}' /></td></tr>" as the image URL and on the page. $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); echo "<table>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) $max_columns = 3; { echo "<tr><td><img src='{$row['image']}' /></td></tr>"; if(++$i == $max_columns){ echo "<tr><td><img src='{$row['image']}' /></td></tr>"; $i=0; }}} echo "</table>"; ?> Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431530 Share on other sites More sharing options...
acidglitter Posted January 6, 2008 Share Posted January 6, 2008 I've modified ken2k7's code so it counts each <td> and then starts a new row after 3.. <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ $row_amount=0; while ($row = mysql_fetch_assoc($result)) { $row_amount++; if($row_amount==3){ echo "</tr><tr>"; $row_amount=0; } echo "<td><img src='{$row['image']}' /></td>"; }} echo "</tr></table>"; ?> It works but it's not perfect. Like if you are only echo-ing 5 different rows then there won't be any </tr>'s ending that row. You could have it count the total amount of rows and have a separate counter besides $row_amount so when it reaches that total put how ever many empty <td></td> to fill up the space and end with a </tr> and </table> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431533 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 I had to change $row_amount==4, but that worked. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431534 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Uh that only works once though Try this (I don't know if it works though) <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /></td>"; ++$counter; }} echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431538 Share on other sites More sharing options...
acidglitter Posted January 6, 2008 Share Posted January 6, 2008 Uh that only works once though Try this (I don't know if it works though) <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /></td>"; ++$counter; }} echo "</tr></table>"; ?> that probably works better because then you don't need 2 different counters.. but mine works more than once because it resets the $row_amount after it reaches 3 Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431539 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 Yep, works as well. Thanks! Learning PHP is difficult. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431540 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Yep, works as well. Thanks! Learning PHP is difficult. No it's not. You'll find your bearings real fast. It just seems hard from the methodology people use in solving problems. It's helpful to be clever at times because even the hardest thing can be looked upon as simple if you are clever. Again, if you need help, feel free to ask. @acidglitter: Sorry, didn't notice that. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431543 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 I think that's why it's more difficult - you need to actually think and be clever as there are multiple ways to do things. With HTML and such, the way to do things is usually quite obvious. Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431547 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 Question - to add to the above script (the latest one by Ken2k7) how could I add two rows under each image (for text)? The rows would be like this: IMAGE IMAGE IMAGE text text text text text text IMAGE IMAGE IMAGE text text text text text text It's currently: IMAGE IMAGE IMAGE IMAGE IMAGE IMAGE The text is held in the database the same as the image's are - just under a different field name. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431637 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Like this? <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /><br />"; // TEXT CODE HERE // echo "</td>"; ++$counter; }} echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431709 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 That's actually close, but here's what I want to show up on the page: <table> <tr> <td>IMAGE</td> <td>IMAGE</td> <td>IMAGE</td> </tr> <tr> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> </tr> <tr> <td>TEXT2</td> <td>TEXT2</td> <td>TEXT2</td> </tr> </table> Both the texts are kept in a field called image_desc and image_desc1 in the same table as the images are. I believe we'll have to change $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); to $result = mysql_query('SELECT * FROM specials') or die(mysql_error()); so all the fields are called. I'm just not sure how to do the actual script. Any help would be awesome! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431918 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /></td>"; $images = mysql_query('SELECT * FROM specials') or die(mysql_error()); if ($images || (mysql_num_rows($images) > 0)){ echo "<tr>"; while (mysql_fetch_assoc($images)){ // what do you want here? // } echo "</tr>"; } ++$counter; }} echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431924 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 Here's what I came up with by adding to yours a little bit. It doesn't line up properly though. It lines up like this: <table> <tr> <td>IMAGE</td> <td>IMAGE</td> <td>IMAGE</td> </tr> <tr> <td>IMAGE</td> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> <td>IMAGE</td> <td>IMAGE</td> </tr> </table> Here's the code I have: <? $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /></td>"; $images = mysql_query('SELECT * FROM specials') or die(mysql_error()); if ($images || (mysql_num_rows($images) > 0)){ while ($desc = mysql_fetch_assoc($images)){ if ($counter%3==0&&$counter!=0) echo "<td>{$desc['image_desc']}</td>"; if ($counter%3==0&&$counter!=0) echo "<td>{$desc['image_price']}</td>"; } } ++$counter; }} echo "</tr></table>"; ?> Like mentioned before, I want the format to be like this: <table> <tr> <td>IMAGE</td> <td>IMAGE</td> <td>IMAGE</td> </tr> <tr> <td>TEXT</td> <td>TEXT</td> <td>TEXT</td> </tr> <tr> <td>TEXT2</td> <td>TEXT2</td> <td>TEXT2</td> </tr> </table> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431956 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 if ($counter%3==0&&$counter!=0) echo "<td>{$desc['image_desc']}</td>"; if ($counter%3==0&&$counter!=0) echo "<td>{$desc['image_price']}</td>"; I'm not understanding that. Can you explain to me what kind of text you want displayed? I mean does 1 image have 3 text fields stored in the database? Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431960 Share on other sites More sharing options...
thomashw Posted January 6, 2008 Author Share Posted January 6, 2008 I've got a total of four fields in the database. They are: image_id image image_desc image_price I want to display three images in a table like so: <table> <tr> <td>image</td> <td>image</td> <td>image</td> </tr> .... Then I want to display the corresponding image_desc field so it displays under its image, like so: <table> <tr> <td>image</td> <td>image</td> <td>image</td> </tr> <tr> <td>image_desc</td> <td>image_desc</td> <td>image_desc</td> </tr> Then I want to display the image_price field so it displays under the above image and image_desc, like so: <table> <tr> <td>image</td> <td>image</td> <td>image</td> </tr> <tr> <td>image_desc</td> <td>image_desc</td> <td>image_desc</td> </tr> <tr> <td>image_price</td> <td>image_price</td> <td>image_price</td> </tr> </table> And then I want it to duplicate that, like so: <table> <tr> <td>image</td> <td>image</td> <td>image</td> </tr> <tr> <td>image_desc</td> <td>image_desc</td> <td>image_desc</td> </tr> <tr> <td>image_price</td> <td>image_price</td> <td>image_price</td> </tr> <tr> <td>image</td> <td>image</td> <td>image</td> </tr> <tr> <td>image_desc</td> <td>image_desc</td> <td>image_desc</td> </tr> <tr> <td>image_price</td> <td>image_price</td> <td>image_price</td> </tr> </table> Here's a picture of how I want it to look: All of the fields (image, image_desc, and image_price) are in the same table (specials.) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431978 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Like this? (NOTE: I DIDN'T USE THE TDs. IF YOU REALLY WANT THEM, I CAN DO IT. THIS JUST SAVES A LOT OF CODING LINES) <?php $result = mysql_query('SELECT image FROM specials') or die(mysql_error()); $counter = 0; echo "<table><tr>"; if ($result || (mysql_num_rows($result) > 0)){ while ($row = mysql_fetch_assoc($result)) { if ($counter%3==0&&$counter!=0) echo "</tr><tr>"; echo "<td><img src='{$row['image']}' /><br /><{$row['image_desc']}<br />{$row['image_price']}</td>"; ++$counter; }} echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84671-help-with-images/#findComment-431981 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.