Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Images from a database, Multiple images per page

Posted by Honk (Honk), 13 September 2007
Im having some trouble with the script to to place into and retrive images from a database. Im trying to use this in conjunction with a script to repeat 5 or so entries per page (like a blog) but only the first image appears. I only have a basic knowledge of php and have no idea what im doing wrong lol. heres the code I have so far



Code:
<?php

      include'database.php';

$sql = "SELECT * FROM shop order by pid desc limit 5";

$result = mysql_query($sql) or print ("Can't select entries from table shop.<br />" . $sql . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {

   $entry = stripslashes($row['entry']);
     $pid = stripslashes($row['pid']);
   $title = htmlspecialchars($row[title]);
   $bytes = $row[imgdata];
     
           if ($_REQUEST[gim] == $pid) {
       header("Content-type: image/gif");
       print $bytes;
       }
   ?>

   <p><strong><?php echo $title; ?></strong><br /><br />
     <img src=?gim=<?php echo $pid ?>>
   <?php echo $entry; ?><br /><br />
   <?php
}
?>


what I have so far can be viewed here.

http://www.edwingills.com/testshop/index2.php

I'm using Pid to create unique instances each time I dont know if this is the correct way to do this but it seemed the logical way ^^.

any help would be appreciated.

Posted by admin (Graham Ellis), 15 September 2007
For starters, you need to generate either an image or HTML in each request to the script - not both; you have a mixture there.  Basically, I'm afraid you have a lot of the right code but a major structural change needed back to how it was done in the original - your script appears to be loosely based on my original at http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html

Have a look at the tips and techniques at the bottom of that original web page to help you ..  and get the original working first. Then adapt and adopt slowly, step by step.  Although short, this script glues together a graet many subjects and isn't trivial.

Posted by Honk (Honk), 16 September 2007
Hey thanks for the reply.

I did adapt this from your script, taking it in steps (I got it working from scratch and then took out the pieces I needed until I merged it with the script I have now) and the script I have does work. The problem is that only the first image is showing up.

Im guessing now the problem I have is that this code


Code:
  if ($_REQUEST[gim] == $pid) {
  header("Content-type: image/gif");
  print $bytes;
  }
   ?>


is only running the once and that would explain why only the first image is showing up. but placing this code outside the first ?> (where the HTML code which will be repeated however many times is) makes all the images fail.

Posted by admin (Graham Ellis), 17 September 2007
That code you quote must run only once - you can only produce one image per request of the server;  you'll note that in my original there was an exit command directly after the print bytes;, and that the script as a whole ran once to generate the HTML (no gim parameter), then again with gim parameters to generate the images.  There are links in the first (HTML) output of the <img src=?gim=6 style to call up each image.

To get the script to work for you, modified, as a single piece of PHP to generate the whole web page, you;re best advised to revert to my scheme. As a reelative newcomer, you would probably find it much easier to use different PHP scripts for the HTML and for the Images.


I'm glad to read that the script sample was working until you made a series of incremental changes. Have a very careful look back at what you did at the point that the program ceased to work as you expected

Posted by Honk (Honk), 17 September 2007
so would it be possible to generate more then one image per page from thi script? I'm guessing it would just not with this script but a whole new one lol. And I dont have the experience to write scripts from the top of my head as of yet

tis a shame as I was looking for a php solution that would allow a user from a simpliefied control panel to upload an image..most systems to upload were complex and would still require a reference from or to  the database that would make the idea of a simple control panel useless as the user would have to search for complex information. The idea of adding the image directly into the database made things much easier! shame I cant get it to work ^^

Posted by admin (Graham Ellis), 18 September 2007
on 09/17/07 at 12:07:06, Honk wrote:
so would it be possible to generate more then one image per page from thi script? I'm guessing it would just not with this script but a whole new one lol.


There in nothing at all to stop you modifying the part of the script that generates the HTML to produce a whole loop of <img ...> tags, each of which runs the script again to produce an image.  That is exactly what the other sample script that's referenced for my original document does - have another look at it

http://www.wellho.net/resources/ex.php4?item=h113/pic_alog.php4
and
http://www.wellho.net/demo/pic_alog.php4?iwant=gr

If you have a script that is nearly working then, yes, there are a few occasions you may want to start from scratch if it is fundamentally flawed.  But I don't think that this one is, and I think you have little chance of getting the system working easily as you want by throwing it away in frustration and starting again.  That approach would be a far longer road.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho