Re: Dynamic Display of Images Stored in DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/28/07, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
>
> you need a *seperate* script that outputs the image data only (+ relevant
> headers)
> and then you refer to that script (passing it a suitable parameter so it
> knows
> which image to output) in the src attribute of an IMG tag.
>
> you seem to be trying to output image data and html in a single request,
> which
> won't work (well actually there is a way to embed the raw image data
> directly in
> the html but AFAICR, but that's somewhat too tricky to cover right now
> given that
> your still trying to grok the basics of this - no offence intended)
>
> or have I misunderstood your question?
>
> David Giragosian wrote:
> > Good Afternoon All,
> >
> > The recent threads about images got me to finally experiment with
> storing
> > into and retrieving/displaying images from a database.
> > Uploading and retrieval is fine, I'm just a bit uncertain about creating
> the
> > dynamic display part.
> >
> > // getting the data out of the db
> > $imageCountinDB = 0;
> > $selectedData = @mysql_query("select title, imagedata from pictures
> order by
> > pid desc");
> > while ( $row = @mysql_fetch_assoc($selectedData )) {
> >         $title[] = htmlentities( $row['title'] );
> >         $imageBytes[] = htmlentities( $row['imagedata'] );
> >         $imageCountinDB++;
> > }
> >
> > // creating the an html table with one img tag per cell
> > if ( IsSet( $_GET['im'] ) && $imageCountInDB > 0 ) {
> >         $imageOutputStr = "<table><tr>";
> >         for ( $i = 0; $i < $imageCountinDB; $i++ ) {
> >                 $setID = $i + 1;
> >                 $imageOutputStr .= "<td><img src=?im=$setID
> > width=300></td>";
> >                 if ( $setID % 3 == 0 && $imageCountinDB > $setID ) {
> >                         $imageOutputStr .= "</tr><tr>\n";
> >                 }
> >         }
> >  $imageOutputStr .= "</tr></table>";
> > }
> >
> > // associating the image data with the img tags
> > switch ($_GET['im']) {
> >    case 1: header("Content-type: image/jpeg");
> >        print $bytes[0];
> >        exit ();
> >        break;
> >    case 2: header("Content-type: image/jpeg");
> >        print $bytes[1];
> >        exit ();
> >        break;
> >
> >      (snip)
> > }
> >
> > <html>
> > <body>
> >
> > <?php echo $imageOutputStr; ?>
> >
> > </body>
> > </html>
> >
> >
> > The question is, with all this happening in one page, is it possible to
> do
> > the last bit dynamically?
> >
> > BTW, the core of the above was nicked from
> >
> http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html
> >
> > Thanks very much for reading this long post.
> >
> > David
> >
>
>
Thing is, the above works just fine as long as I hard code a switch case for
every image file pulled from the db. What I can't seem to do is dynamically
extend it to include newly uploaded images, i.e., for an image count greater
than what I've hard coded. I thought maybe something like 'eval' might be
used (I know, if eval is the answer, I'm asking the wrong question). To be
honest, I don't even know _why_ the above works because it seems that stuff
is happening as if in a while loop when there isn't one (the switch case
part).

For the moment, I'd be happy to just understand how it works as it does.
Maybe as you suggest, there is raw image data somehow being embedded in the
html.

On a peripherally related note, I did learn that syntax like <img
src=?im=6> looks like this in the page source:
<img src=http://www.mypage.php?im=6>.

Never liked the short-hand stuff...

Thanks, Jochem, for responding.

David

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux