Page 1 of 1

Another loop question

Posted: Sun Sep 15, 2002 1:18 pm
by gijs
Hi,

First let me thank everybody who helped me in the past few weeks. Must say, got some great support on this forum. As a 'oldie' newbie its been a great help.

I've got the follwing code which works fine as a first step in what I want to achieve:

Code: Select all

$sql = "SELECT FileData FROM filestore WHERE ID=6";
$result = mysql_query($sql);
if(!$result) die("database error: " . mysql_error());
$image = mysql_result($result,0,"FileData");
header("Content-Length: " . strlen($image));
echo ($image);
In FileData images are stored.
How can I list all the images stored in FileData.
Tried several things but no result. In the best case I still get only 1 image displayed. :?

Help would be appreciated,

Thanks in advance,

Gijs

Posted: Sun Sep 15, 2002 1:32 pm
by MattF
I don't understand what you are trying to do? IFf you want to select multiple rows from a database and id is a unique field then you should remove the WHERE id=6

If you are planning on outputing multiple images then it just won't work if you set the header to the length of one file. It's just very confusing. My head hurts :?:

Posted: Sun Sep 15, 2002 11:01 pm
by gijs
Hi MattF,

Sorry for badly formulating what I want to do.

The code was mearly to illustrate that I could make it work by retrieving 1 one record and therefore display only 1 image.

What I want to do is outputting all the images and their discription.

FileDescription | FileData
varchar | blob
Pets | image of a pet


a) Removing the WHERE-clause: even this I now :wink:
If you are planning on outputing multiple images then it just won't work if you set the header to the length of one file. It's just very confusing. My head hurts
b) Now, I'm confused. :?

Thanks for your reply,

Gijs

Posted: Mon Sep 16, 2002 2:28 am
by Takuma

Code: Select all

<?php
$sql = "SELECT FileData FROM filestore WHERE ID=6"; 
$result = mysql_query($sql) or die("database error: " . mysql_error()); 
while(list($image) = mysql_fetch_array($result)) {
  header("Content-Length: " . strlen($image)); 
}
while(list($image) = mysql_fetch_array($result)) {
  echo $image;
}
?>
Would this work?

Posted: Tue Sep 17, 2002 3:34 am
by gijs
Thanks Takuma,

Sorry, but doesn't work.
No display what so ever.

Gijs

Posted: Tue Sep 17, 2002 4:05 am
by volka
http was designed to transfer one document - only one at a time.
You have to convience the browser that it has to download multiple documents.
In case of images the simplest way is to send a html-document that contains <img src="myimages.php?catid=[number]&serial=[number]" />
  • write a script that does the following
  • catid equals ID in your query
  • check if there has been transmitted a catid (if not abort)
  • if there's no serial-id count the entries in the database and send back a html-doc with according <img src="myimages.php?catid=[transmitted number]&serial=[serial-number]" />-entries (with serial-number from 0 to entrycount-1)
  • if there is a serial-id request the data from the db with $query = "SELECT .... LIMIT {$_GET[serial]},1" and send back the data

Posted: Tue Sep 17, 2002 7:29 am
by Wayne
If you have the nessecary GD librarys installed on the server you could use the php ImageCreate() function and call them as you would any other image in HTML with the <IMG SRC=....> tag.

this would enable you to display all of the images on 1 page.

Posted: Tue Sep 17, 2002 9:05 am
by gijs
Thanks volka and Wayne,

You have to excuse me being a Newbie I'm and not advanced enough yet to fully understand volka's method. I see what he means, but would not now how to handle it and translate it into code. :oops:

I've already heard about GD-library's and I'm going to do some research on that subject.

I'll let you know the outcome and thanks again,

Gijs

Posted: Thu Sep 19, 2002 10:47 am
by volka
have been a little off the last days ;)
try

Code: Select all

&lt;?php
	$host = 
	$user = 
	$pass = 
	$db = 
	
	if (isset($_GET&#1111;'catId']))
	{
		$_GET&#1111;'catId'] = (int)($_GET&#1111;'catId']);
		$conn = mysql_connect($host, $user, $pass) or die('connct error');
		mysql_select_db($db, $conn)  or die('db_select failed');
		if (!isset($_GET&#1111;'serial']))
		{
			$query = 'SELECT count(catId) FROM filestore WHERE catId='.$_GET&#1111;'catId'];
			$result = mysql_query($query, $conn) or die(mysql_error());
			if ( mysql_num_rows($result) &gt; 0)
			{
				$row = mysql_fetch_array($result);
				for ($i = 0; $i &lt; (int)($row&#1111;0]); $i++)
					print("&lt;img src="{$_SERVER&#1111;'PHP_SELF']}?catId={$_GET&#1111;'catId']}&amp;serial=$i"&gt;&lt;br/&gt;");
			}
			else
				print($_GET&#1111;'catId']. 'unknown category');
		}
		else
		{
			$_GET&#1111;'serial'] = (int)($_GET&#1111;'serial']);
			$query = "SELECT FileData FROM filestore WHERE catId={$_GET&#1111;'catId']} LIMIT {$_GET&#1111;'serial']},1";
			$result = mysql_query($query, $conn) or die(mysql_error());
			if ( mysql_num_rows($result) &gt; 0)
			{
				header("Content-type: image/png");
				$row = mysql_fetch_array($result);
				print($row&#1111;0]);
			}
			// else &#1111;maybe some error pic]
		}
	}
	else
		print('malformed request');
?&gt;
the script is tested only partial (very, very partial ;) )
and keep in mind that you (almost) always start a discussion if you say you want to store pictures in a DB :?

p.s./edit: & has been changed to & in php-code-block

Posted: Fri Sep 20, 2002 3:01 am
by gijs
Hi volka and Wayne,

First I checked GD at http://www.boutell.com/gd/manual1.8.4.html.

Here it states:
To use gd, you will need an ANSI C compiler.
Don't know anything about C and compiling and don't have a AINSI C compiler. So, no longer an option for the time being.

Took a look a the code volka graciously supplied. Must say I completely lost, but maybe you could shed some light into the darkness.

The code it self I understand what it's doing.

:oops: My questions are the following:
a) catid is what I used as ID but where does 'serial' fit in ?

b) where does
In case of images the simplest way is to send a html-document that contains <img src="myimages.php?catid=[number]&serial=[number]" />
from the earlier reply fit in.

c) from where are the $_GET-variables send from.

I'm probabely to stupid to understand. :roll:

Reading up on some discussions on storing images into a db I understand:
and keep in mind that you (almost) always start a discussion if you say you want to store pictures in a DB
My main objective is simply to learn to work with images. Storing them in a db is a given option.
For what I want to use it could be ok, I think: 1 table for 10 images of each 1 kb).


Thanks again,

Gijs

Posted: Fri Sep 20, 2002 3:40 am
by Wayne
The GD libraries might not be a right off. If you are working on a linux server it will probably have a version of a C compiler on it, as this is used to create most of the programs that are installed from source code.

Either way good luck.

Posted: Fri Sep 20, 2002 3:41 am
by twigletmac
Are you working on Linux or Windows? Setting up GD on each of these is different, if you tell us what OS you use we should be able to help you get it working.

Mac

Posted: Fri Sep 20, 2002 5:01 pm
by volka
with the php-zip-distributions for win32 ships a php_gd.dll which announces itself as "1.6.2 or higher" --> png but no gif-support. The dll is ready to go, no other dependecies. sources (if necessary) may be downloaded from http://www.boutell.com/gd/

I really should put some comments in example scripts :oops:

you wanted to to download multiple images.
Let's say there are 3 pictures in your DB that apply to the ID/catId. You have to advise the browser to download picture1, picture2 and picture3.
That's what 'serial' is meant for. If no serial-number is supplied the script counts the (picture-)entries that applies to the given catId ('SELECT count(catId) FROM filestore WHERE catId='.$_GET['catId']). For each entry it then returns an <img>-element to the browser. The src-url contains again the catId but also one of the serial numbers (for ($i = 0; $i < (int)($row[0]); $i++)).
Then the browser starts to download the pictures. With each request it is sending the according catId and serial-number. The script will request the picture data with a "SELECT ... LIMIT ..."-query (SELECT-syntax). The first parameter for limit is the offset, the second the number of rows that shall be delivered.
Increasing the offset but keeping the number of rows at 1 picture by picture will be requested by and transmitted to the browser.

Since the request is used within an image element the GET-method is needed, so the values will be in $_GET (php-version >= 4.1, register_globals off)

Posted: Sat Sep 21, 2002 1:52 am
by gijs
Thanks Mac, Wayne and volka !

I'm overwelmd by the respons !:D

Mac, concerning GD: I'm using: windows 2000.
By the way, congratulations Guru. :wink:

Your comments volka made me understand better ... I think.
So, serial-number is a field that contains the subcategorie of catid.
If, witch is my case, every catid contains only 1 image, you can leave out serial-number. Is this correct ?

Perhaps its time to explain what I want finally want to achieve:

1) Every hotel has a list of 'facilities' which should be displayed together with the rest of the hotel info. What's a facility: f.e. carpark, restaurant, swimmingpool, etc. They would be represented as a kind of logos
2) For this a I have 2 of tables:
a) table which contains all the possible facility-logos in blob-files (10 to be exact)
b) table which contains for each hotel the facilities

schematic:
table a):
fields:
logo_id | logo_name | filedata (blob)
values:
1 | carpark
2 | swimmingpool
3 | restaurant

table b)
fields:
hotel_id | logo_id
values:
1 | 1
1 | 2
1 | 3
255 | 2
126 | 1
126 | 3

So if I select a hotel it should list all the facilities of that particulary hotel displayed as a image.

Anyway, I'll try to get volka's way working and let you know.

Thanks again,

Best Regards,

Gijs

Posted: Thu Sep 26, 2002 4:15 am
by gijs
Hi,

been sick for a couple of days so only started on it yesterday.

I hope, I've got the gd-lib's installed correctly.
Did a phpinfo(); and got the following results.

Code: Select all

gd
GD Support enabled 
GD Version 1.6.2 or higher 
FreeType Support enabled 
FreeType Linkage with TTF library 
JPG Support enabled 
PNG Support enabled 
WBMP Support enabled
On my project:
a) uploading and storing blobs (gifs, pdf, ...) works fine.
b) displaying blobs works fine (gif, pdf, ...) but only one at the time.

So I'll try to work with gd-functions to display multiple images.

Thanks again,

Gijs