Page 1 of 1

Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 7:52 am
by Aleroth
Hi there

okay, i have a database "db_house" with a table "flat" and in this table there is a few rows with all the needed info and including 3 images i want to be displayed when this info is displayed. but i have no idea how, i got it right to display one image at a time. but i want all three to be displayed at the same time, meaning on the same page.

i have this to display the image, but how am i gonna have all three show :

Code: Select all

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

include 'connection.php';

$id    = $_GET['id'];
$query = "SELECT filename, type, size, content " .
         "FROM flat WHERE id = '$id'";

$image = mysql_query($query) or die('Error, query failed');

list($filename, $type, $size, $content) = mysql_fetch_array($image);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$filename");
echo $content;

}

?>
and on the page to display the image i use this :

Code: Select all

<img src='image.php?id=$id' width='300' height='250' />
in the table i use filename, size, type and content [*content is the blob] and for the other 2 images i just used filename1, size1, type1, content1 and then filename2, size2, type2 and content2.

i got it right to upload the images to the mysql database, but yes...its just displaying it on the output page thats not working. :banghead:

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 10:41 am
by AbraCadaver

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 11:46 am
by Aleroth
Okay thats for when u have a single image in each id, i want to display one id's info and in one id there is 3 images.

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:03 pm
by AbraCadaver
Aleroth wrote:Okay thats for when u have a single image in each id, i want to display one id's info and in one id there is 3 images.
You'll have to show how you retrieve these images.

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:20 pm
by Aleroth
i did in the first post, i added them using a form with file upload...each image has 4 fields, filename, size, type and content (blob) the second image is filename1, size1, type1 and content1 and 3rd is filename2, size2, type2 and content2 so if you want to recall them i guess you will have to connect to the table "flat" and call the $filename, $size, $type,$content, etc.

like in my first post if u use the <img src='image.php?id=$id' /> then it gives the normar $content image and if i change the names in image.php to the content1, size1, etc. then the second image apears and if i change the names to filename2, size2, etc the third image apears.

but how do i make all three apear at the same time??

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:22 pm
by AbraCadaver
You need 3 separate img tags.

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:33 pm
by AbraCadaver
Maybe I understand now after rereading. You need to tell image.php which one to get (rough example):

Code: Select all

<img src='image.php?id=$id' width='300' height='250' />
<img src='image.php?id=$id&num=1' width='300' height='250' />
<img src='image.php?id=$id&num=2' width='300' height='250' />

Code: Select all

if(isset($_GET['num']) && $_GET['num'] == '1' || $_GET['num'] == '2' ) {
   $num = (int)$_GET['num'];
} else {
   $num = '';
}
$query = "SELECT filename$num, type$num, size$num, content$num " .
         "FROM flat WHERE id = '$id'";
//etc...

Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:42 pm
by Aleroth
okay, sounds logical and what code must i put in image.php this is what i tried but it gives blank then :

Code: Select all

<?php
if(isset($_GET['id']))
{
 
include 'connection.php';
if (is_numeric($_GET['id'])) {
$id = $_GET['id'];
}
else
{
$id="";
}
$query = "SELECT filename, size, type, content, filename1, size1, type1, content1, filename2, size2, type2, content2 " .
         "FROM flat WHERE id = '$id'";

$num = ''; // what u gave me to add

if(isset($_GET['num'])) {
   $num = (int)$_GET['num'];
}

$result = mysql_query($query) or die('Error, query failed');
 
list($filename, $type, $size, $content, $filename, $type1, $size1, $content1, $filename2, $type2, $size2, $content2) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$filename");

header("Content-length: $size1");
header("Content-type: $type1");
header("Content-Disposition: attachment; filename=$filename1");

header("Content-length: $size2");
header("Content-type: $type2");
header("Content-Disposition: attachment; filename=$filename2");

echo $content;
echo $content1;
echo $content2;

exit;

}
 
?>



Re: Displaying Multiple Images from one Table??

Posted: Tue Jan 25, 2011 12:47 pm
by Aleroth
oh wait i didnt click expand by the code, let me try that first...sorry

Re: Displaying Multiple Images from one Table??

Posted: Wed Jan 26, 2011 3:55 am
by Aleroth
okay, its still not working as i thought it would.

should the code of image.php change allot or what? and you understand that the 3 images is in one entry, meaning all under one ID so how do i diplay all three those images from one ID?

Re: Displaying Multiple Images from one Table??

Posted: Wed Jan 26, 2011 10:30 am
by AbraCadaver
This is how it should be put together (not tested). If it doesn't work then you'll have to be more specific about the problem:

Code: Select all

// image.php
if(isset($_GET['id']))
{
    include 'connection.php';
    
    $id = $_GET['id'];
    
    if(isset($_GET['num']) && ($_GET['num'] == '1' || $_GET['num'] == '2'))
    {
        $num = (int)$_GET['num'];
    } else {
        $num = '';
    }
    $query = "SELECT filename$num, type$num, size$num, content$num FROM flat WHERE id = '$id'";
    
    $image = mysql_query($query) or die('Error, query failed');
    
    list($filename, $type, $size, $content) = mysql_fetch_array($image);
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$filename");
    echo $content;    
}

Code: Select all

<!-- html file -->
<img src='image.php?id=$id' width='300' height='250' />
<img src='image.php?id=$id&num=1' width='300' height='250' />
<img src='image.php?id=$id&num=2' width='300' height='250' />

Re: Displaying Multiple Images from one Table??

Posted: Wed Jan 26, 2011 11:46 am
by VladSun
The code introduces SQL injection attacks.
Should be e.g.:

Code: Select all

 $id = intval($_GET['id']);