Displaying Multiple Images from one Table??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Displaying Multiple Images from one Table??

Post 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:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying Multiple Images from one Table??

Post by AbraCadaver »

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Re: Displaying Multiple Images from one Table??

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying Multiple Images from one Table??

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Re: Displaying Multiple Images from one Table??

Post 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??
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying Multiple Images from one Table??

Post by AbraCadaver »

You need 3 separate img tags.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying Multiple Images from one Table??

Post 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...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Re: Displaying Multiple Images from one Table??

Post 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;

}
 
?>


User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Re: Displaying Multiple Images from one Table??

Post by Aleroth »

oh wait i didnt click expand by the code, let me try that first...sorry
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Re: Displaying Multiple Images from one Table??

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying Multiple Images from one Table??

Post 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' />
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Displaying Multiple Images from one Table??

Post by VladSun »

The code introduces SQL injection attacks.
Should be e.g.:

Code: Select all

 $id = intval($_GET['id']);
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply