Shawk Posted March 27, 2011 Share Posted March 27, 2011 Hello, I'm just learning PHP and I'm trying to have a different image display for each Item in a shop.. My only guess to how this would be possible is to name the IMG after the ID of the Item, like Item #10 has a name of 10.jpg. My question is, how would I go about doing this? or is there a better way of doing this.. Here is the Code I currently have. Thanks for help. <?php session_start(); $page = 'index.php'; mysql_connect ('localhost','root','') or die(mysql_error()); mysql_select_db ('buggs') or die (mysql_error()); function products() { $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC LIMIT 10'); if (mysql_num_rows($get)==0) { echo "There are no products to display!"; } else { while ($get_row = mysql_fetch_assoc($get)) { echo '<p>'.$get_row['id'].'<br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />'.number_format($get_row['price'], 2).' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>'; } } } ?> and the code to where it displays.. <?php require 'cart.php'; ?> <html> <head> </head> <body> <?php products();?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/ Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 You could add another row called image to your database and save the path to the image in the database under that row. image /images/here/10.jpg <?php $get = mysql_query('SELECT id, name, description, price, image FROM products WHERE quantity > 0 ORDER BY id DESC LIMIT 10'); ?> Then when you echo each row pulled from your query you can include a different image for each item. <?php echo '<p>'.$get_row['id'].'<br />'.$get_row['image'].'<br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />'.number_format($get_row['price'], 2).' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>'; ?> Hope that helps you out. Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192749 Share on other sites More sharing options...
Shawk Posted March 27, 2011 Author Share Posted March 27, 2011 awsome, that works! thanks alot to make my life a bit easier, if this is possible.. what would be the proper coding for something like this.. '.$get_row[<img src="images/here/'image'.jpg" />].'<br /> this way all I need to do is put the # instead of <img src="images/here/1.jpg" /> into all 7000 books in the database =P or even better.. would it be possible for me to just.. '.$get_row[<img src="images/here/'id'.jpg" />].'<br /> Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192760 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 I don't think it will work like that but you could try this: <img src="/images/here/'".$get_row['id']."'.jpg" /> . '<br /> I can't test this but it may work, I may also be off on the quotes here and there. But give it a try! Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192761 Share on other sites More sharing options...
Shawk Posted March 27, 2011 Author Share Posted March 27, 2011 hmm, give me error "Parse error: syntax error, unexpected '"', expecting ',' or ';' in" I tried moving everything around a bit, no luck Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192762 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 '.<img src="/images/here/'.$get_row['id'].'.jpg" /> . '<br /> Give that a try the quotes always seem to get me lol. Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192763 Share on other sites More sharing options...
Shawk Posted March 27, 2011 Author Share Posted March 27, 2011 Parse error: syntax error, unexpected '<' in thanks again, something tells me this isn't going to be as easy as I hoped only reason im being so stubborn with this is because if the image can do this, then the shopping cart link will be able to do this too.. then I can just use the ID for the shopping cart ADD URL. Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192764 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 It and can be done just the formatting isn't correct My brain is fried atm sorry not much more of a help lol. my last try at guessing it lol : '.'<img src="/images/here/'.$get_row['id'].'.jpg" />' . '<br /> Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192768 Share on other sites More sharing options...
Shawk Posted March 27, 2011 Author Share Posted March 27, 2011 awesome that worked just had to take the / from the front of the /images and it worked perfectly Thanks alot I owe you! Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192770 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 so my brain isn't totally fried awesome! Glad I could help you sort out your issues! Link to comment https://forums.phpfreaks.com/topic/231824-adding-image-by-id-tag/#findComment-1192771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.