Hi there, does anyone know any good tuts for displaying images using php. The thing is, I have a site that i search for a part number and it displays the info, that i can do but for some reason im lost when it comes to the images.

Do i save them in a db

Do i save them as link and save the link in a db

I just dont really know where to start, im self taught php and so a little help would be much appreciated

thanks
Dan

Member Avatar for diafol

You can save images as data within a blob field. But to be honest, I'd stick with an image folder with a reference to the url in the DB.

images table
id (PK)
displayname (could be a false filename or title)
extension (file type, e.g. jpg/png/gif/svg)
tags (useful for searching)
description (useful for searching)
uploader_id (user id)
uploaded_timestamp (unix integer timestamp)
(etc)

This is how I usually set my db:

In order to stop duplicate files and overwriting:

1) Send form (with upload file and info on picture)
2) Add new entry to DB, filling in details from $_POST.
3) Get PK value from mysql_insert_id() function and rename the image file to "img_" & id value followed by the original extension.
4) All images now are named: img_1.gif, img_2.jpg, img_3.jpg, img_4.png etc.

Overwriting is no longer an issue. Replacing an image is more straightforward that it would suggest.

Anyway, you can do all this without using the 'id' method - just copy the proper filename to a filename field.

i hear where you are coming from and a understand most of what you say it's just im not fluent in php and am not sure how to call files from a folder

Member Avatar for diafol

OK mate, I hear you, but I don't want to be acting the sap and do the work for you. Clue:

<img src="<?php echo $img;?>" ...(other attributes perhaps) />

Where the $img variable has come from the db. It's just a path and a filename.

Hi Dan,

I'm willing to give this a try. I am also a PHP MySQL novice. I'm not sure how much you understood from the preceding post so I'm going to start at the beginning.

You don't put pictures in a database. Databases are records of text and numbers. You'll need to upload your pictures into a folder or directory.

Let's say that your URL is:

http://www.yoursite.com/

A folder or sub directory that you put your photos in could be called:

http://www.yoursite.com/images/

You upload all of your photos into that directory.

Now the simplest way you could have a database for your records is:

An Item number and a photo address that corresponds to that item number

Let's assume that your photos are in .jpg format.

Here's how the MySQL database will look:

Record 1
------------

Item Number: 1

Photo Address: /images/img_1.jpg

Record 2
-------------

Item Number: 2

Photo Address: /images/img_2.jpg

And so on...


Of course you may have more information for the record than just item number and photo address. You might also have the manufacturer or other information.

I hope this helps.

Larry

Assign $row to a variable. Then put it in an image tag.

$pic = $row;
echo "<img src = '$pic' >";

Also, just putting the name of the file, instead of the full path, in the database might work better. In which case, you would put the relative path in the img tag before the pic variable. Like this.

echo "<img src = 'images/$pic' >";

Thnks guys for your help, by any means i dont want it doing for me becuase i really want to learn this, just the process in my head and the starting point like you have given me.

Really appreciate it, im going to go and right some code and ill let you know where i get to and if i hit any snags

thanks
dan:)

thanks to you guys i've figured out the image problem i had and now opened a whole new can of worms. I think i overcomplicated it in my head but once i sat down and thrashed out the code it wasnt too bad.

the new problem which i may hav to make a new post for is i need to query the db and return a part number and model, but the thing is these results need to link a page where they are displayed fully

heres an example http://www.airsprings.com/search/airsprings

if u type in the bottom box W02-358 in returns 3 results which are in turn linked to a page where they display fully.

Im not asking for the code or anything doing for me but suggestions of the best way to go about this

thanks again for all your help

Hi Dan,

I am new here and I'd definitely don't want to break any rules. There seems to be a rule about giving too much help, but I'm finding with your post that it feels like I'm trying to read your mind. I'm also having a little trouble with your terminology. Your use of the word link seems to be used for different things. I'm not criticizing you, it's just that I'm having some problem with that. You may use that terminology in Great Britain. I'm from the States.

I also hope that my previous example wasn't too simplistic. I didn't mean to insult your intelligence by starting at step one. If I was then please accept my apologies.

Now to the problem. So you are querying the database and that database query results in a part number and model name. Is this going to be a unique number and model? In a way it doesn't matter. I'm just trying to understand the problem.

When you say "link to a page where they are displayed fully", do you mean to display (load) a new page where you can display the contents/result of your database query? For now I'm going to assume that is what you're asking.

You may want to look at the PHP header() function. Specifically, header('Location: http://www.yoursite.com/'); that will load a new page. http://www.yoursite.com/ can be a local file name such as newfile.php. Also since it's a PHP command you can send arguments to that page.

If this isn't what you're looking for, and you haven't figured it out yet, then please rephrase the question and I'll try to do what I can.

I'm wondering if this isn't the more difficult way to do things. Again, I don't want to insult your intelligence, but I was wondering. If you take the input from the form and the action="newfile.php" in the HTML form. Wouldn't that display a new page where you can then call the database and display the results? I'm just thinking out loud here. Unless of course you are not using a form to get the information to query the database for the part number and model name. In that case the PHP header() function might be what you're looking for.

Good luck,

Larry

PS - More info on PHP header() funciton can be found at:

http://php.net/manual/en/function.header.php

and

http://www.w3schools.com/php/func_http_header.asp

Hi Larry

I have had this problem before with terminology, by any means i dont want to break rules and i dont want any code writing for me becuase i want to to learn if not whats the point, i may as well just employ someone.

I just want to know some ideas about the best way to go about doing things and make sure i do them in the right order.

The best way for me to explain things is to go to the link that i posted http://www.airsprings.com/search/airsprings and type this "W02-358" without qoutes into the botto box, as you will see this brings multiple results, which is the point where i have got to in my own script, but then if you click one of the results it taks you to a page where it displays the entire details for that result.

I just need a few suggestions about how i would do the same

Thanks again for helping me and im sorry about the accent
Dan

Hi Dan,

OK. I think I know what you are doing now. The http://www.airsprings.com/search/airsprings link is just an example. This is not the project you are working on. Is that correct? If so, I will try to tell you how I would achieve this.

You will need an HTML form to collect the data. In that form it will look something like this:

<form  method="post" action="get_database_value.php">
   <input type="text" name="partnumber" value=""  />
   <input type="text" name="model" value=""  />
   <input type="submit" name="submit" value="Submit"  />
</form>

In the file called get_database_value.php you will be doing the actual call to your MySQL database. You need to figure out what you're going to use as the key and have something like:

get_database_value.php

<?php
.
.
.
$result = mysql_query("SELECT * FROM databasename WHERE ...");

while ($row  = mysql_fetch_array($result) {

// This is where you retrieve the part number and model name for each database record found.

// This is where you put the link to the unique page for the part number and model name. IE.

echo "<a href='partxpage.php' >Part x</a>";

// Where x is the unique part name. I would make Part x a php variable and stick it into the hot link 
//with a PHP include(). Something like <? include($linkname) ?> where $linkname is made up //from the variables you got from the database.

} // End while
.
.
.
.

?>

I am assuming that you are familiar with the mysql_query command with the SELECT option and the mysql_fetch_array() functions. If not you need to look in a PHP book that has MySQL commands and see how to use them.

Please let me know if any of this is not clear to you.

Again, good luck!

Larry

PS - I could have made some syntax errors in the code, so make sure you check it.

commented: Very helpful and understanding +1

Yeah it is just an example i found but its exactly what i want, thanks for the help.

Thats pretty much what im looking for, i got the form and query sorted it was just the hot link i was struggling with and the how by clicking on the hotlink i get it to to display the full details on another page or the "unique page" as you call it.

Thanks Larry, i think ive got it, spent most of the day rattling through code but it works the way i want it to. When its finished ill send you a link.

Just gotta design the site now:(

Thanks
Dan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.