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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.