Jump to content

Adding Image by $id tag


Shawk

Recommended Posts

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

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.

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 />
    

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.

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.