Jump to content
Larry Ullman's Book Forums

Adding Images To Browse_Prints.Php


Recommended Posts

Hi guys, in need of some help attaching images to the table on the browse_prints.php page, is this possible to do?

 

Sorry I am pretty new to php and have tried playing around with some code but only get errors - I have managed to get the 'request image is not available' image to appear in the table so think im not too far off.

 

If anyone has managed to do this or could be off assistance would be great to hear from you!

 

Thanks

Link to comment
Share on other sites

browse-prints.php is part of the ecommerce example. You need to provide your own images, store them in a directory and set up the database which accesses the filename of the images. Ideally use the admin functions addArtist.php and addPrint.php to populate the d/b and get the files stored in the correct directory. If you work through the beginning of the chapter you'll set up the database, file structure and admin functions.

 

I've modified the scripts to enable a client to upload images to his portfolio site - a mini CMS without the ecommerce side.

Link to comment
Share on other sites

browse-prints.php is part of the ecommerce example. You need to provide your own images, store them in a directory and set up the database which accesses the filename of the images. Ideally use the admin functions addArtist.php and addPrint.php to populate the d/b and get the files stored in the correct directory. If you work through the beginning of the chapter you'll set up the database, file structure and admin functions.

 

I've modified the scripts to enable a client to upload images to his portfolio site - a mini CMS without the ecommerce side.

 

Thanks for the help

 

I have set up the database and populated and currently have prints (with details) correctly displayed on browse_prints.php in the table, the links for each print also work and take me through to view_prints.php with the image displayed.

 

What I wanted to know was how to get this image to also appear in the table of prints on the browse_prints page.

 

Does anyone have a solution for this - I keep getting errors

Link to comment
Share on other sites

"I ... currently have prints (with details) correctly displayed on browse_prints.php in the table, [but] ... I wanted to know how to get this image to also appear in the table of prints on the browse_prints page."

 

 

 

What do you mean?

You mean you want to add additional pics to the table?

Link to comment
Share on other sites

ok I think I get what you are trying to do, sorry for not getting it the first time. You'll need to do some formatting/image re-sizing to get a nice display but here's what worked for me.

<?php
$page_title = 'Display the Prints';
include ('includes/header.html');
require ('includes/mysqli_connect.php');

$q= "SELECT artists.artist_id, CONCAT_WS(' ', firstname, middlename, lastname) AS artist, printname, price, description, print_id, imagename
FROM artists, prints WHERE artists.artist_id = prints.artist_id ORDER BY artists.lastname ASC, prints.printname ASC";

if (isset($_GET['aid']) && filter_var($_GET['aid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
   $q = "SELECT artists.artist_id, CONCAT_WS(' ', firstname, middlename, lastname) AS artist, printname, price, description, print_id, imagename
   FROM artists, prints WHERE artists.artist_id = prints.artist_id  AND prints.artist_id={$_GET['aid']} ORDER BY prints.printname";
   }
echo '<table border="0" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="15%"><b>Artist</b></td>
<td align="left" width="25%"><b>Thumbnail</b></td>
<td align="left" width="15%"><b>Print Name</b></td>
<td align="left" width="20%"><b>Description</b></td>
<td align="right" width="5%"><b>Price</b></td>
</tr>';
$r = mysqli_query($dbc, $q)  or trigger_error("Query: $q\n <br />MySQL Error: " . mysqli_error($dbc));

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$image = $row['imagename'];
$pid = $row['print_id'];
echo "<tr><td align=\"left\"><a href=\"browsePrints.php?aid={$row['artist_id']}\">{$row['artist']}</a></td>";
   if ($image = @getimagesize ("../uploads/$pid")) {
           echo "<td align=\"center\"><img src=\"showPrint.php?image=$pid&name=" . urlencode($row['imagename']) . "\" $image[3] alt=\"{$row['printname']}\" />
           </td>\n";
   }

echo "<td align=\"left\"><a href=\"viewPrint.php?pid={$row['print_id']}\">{$row['printname']}</td>
<td align=\"left\">{$row['description']}</td>
<td align=\"right\">{$row['price']}</td>
</tr>\n";
}
echo '</table>';
mysqli_close($dbc);
include ('includes/footer.html');
?>

  • Upvote 1
Link to comment
Share on other sites

Good point about the image resizing and formatting! A quick google search should find a quick bit of code for 00cakea to limit the size and placement of the shown image, such as applying some CSS.

 

Also your first instance of creating the $image variable seems unnecessary(isn't it?), since you re-assign the variable a couple lines down anyway and never use it outside the if conditional there.

 

I do admire your ability to improvise, since that seams to be a large portion of programming. Trust me when I tell you I am not speaking from superiority, since you are likely a better programmer than me. Good job!!!

Link to comment
Share on other sites

@giantsfan24 - good point, there is actually no need to create the first instance of $image, can't remember what I was thinking when I created it.

 

In my previous post, the amended code didn't show up so here it is,

if ($image = @getimagesize ("../uploads/$pid")) {
  echo "<td align=\"center\"><img src=\"showPrint.php?image=$pid&name=" . urlencode($row['imagename']) . "height=\"200\" width=\"200\" alt=\"{$row['printname']}\" />
  </td>\n";

 

nothing new logically, just a height and width on the image tag to help with the display. Probably not the best way performance wise as I think all this re-sizing could slow down the page load.

  • Upvote 1
Link to comment
Share on other sites

I think the resizing itself is not that bad, but if you're loading a 2 MB image file that's shrunk down to a 50 x 50 thumbnail pic, then you could probably do better. I think there are some JavaScript/jQuery interfaces out there for point-and-click resizing an image and then saving a thumbnail as a separate image. In general, it's best to create and save separate thumbnails.

Link to comment
Share on other sites

 Share

×
×
  • Create New...