Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Displaying records from a mySQL DB

mysql records

  • Please log in to reply
3 replies to this topic

#1 aakinn

aakinn

    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 09 March 2010 - 05:25 AM

Hello all

I am currently trying to teach myself some PHP and mySQL work in order to build a DB driven website.

I have a table in my database which stores information about a product eg (product name, price, description, image(set as a blob))

What I want to do is list all the items in this table in a webpage with the image showing following by the name and price of the item under it, something similar to the way Game.co.uk website displays their product information

I also would like when an item has been clicked on to show a new page with all the item's details.

Are there ay templates any knows of or has which I could go by as I am struggling to get this working

Kind Regards

Akin
  • 0

#2 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 09 March 2010 - 06:32 AM

You'd need 3 pages. The products page which displays all products, the image which will display the image, and the product details page which will show all the details about the product.

<?PHP
//display all products
$query = mysql_query("SELECT * FROM `products`") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'">'.$row['name'].'</a>';
}

<?PHP
//display image
$id = $_GET['id'];//FILTER!!
$query = mysql_query("SELECT `image` FROM `products` WHERE `id`='$id'") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
$im = imagecreatefromstring($row['image']);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
}

<?PHP
//display full product
$id = $_GET['id'];//FILTER!!
$query = mysql_query("SELECT * FROM `products` WHERE `id`='$id'") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
}

  • 0

#3 aakinn

aakinn

    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 09 March 2010 - 07:44 AM

Excellent thats worked like a charm thanks alot mate just another quick thing

When im formatting the output I want each image to appear side by side/horizontally with the details of the items below the image so far I have this

...//display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<table border="1" cellpadding="10">
<tr>
<td><a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"> </td>
</tr>
<td>'.$row['name'].'</a><br />£'.$row['price'].'</td>
</tr>
</table>';
}

This displays the details below the images as I want but each record is shown vertically, how would I get the images to appear side by side?

Thanks
  • 0

#4 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 09 March 2010 - 07:51 AM

Try this
<?PHP //display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<table border="1" cellpadding="10">
<tr>
<td><a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"></a></td>
<td><a href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br />£'.$row['price'].'</td>
</tr>
</table>';
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download