PDA

View Full Version : Adding an Image reference to the DB


kenwvs
02-16-2008, 07:26 AM
I have a MySQL database and have it displaying the results in a table. I would like to add another column where the user can click on the word "map" and have it display a map of the city they have chosen. I have read that it is not recomended to put the actual image in the database, but rather just put a reference to it. If I have a column in my DB for this reference, I am not sure how to put the reference in the table to make it a link that someone could click on. I have stored the actual images in /maps/name of map.jpg and am not sure how to write that in the DB so it is a link.

Thanks in advance,

Ken

StupidRalph
02-16-2008, 02:22 PM
You will dynamically write the hyperlink in whatever serverside language you are using.
Since this is a MySQL forum I'm going to assume that you're using PHP although, I'm well aware that you could be using another language just in form me of such event.

Suppose in your database the column holding your "reference to you picture" is named `imgname`. And inside it contains the image name (i.e. paris.jpg). You could use something like the following code...

//you should connect to your db first.

$q = mysql_query('SELECT `imgname` FROM `database_table` WHERE `primary_key` = "1" ') or die(mysql_error()); //run the SQL query

$r = mysql_fetch_assoc($q); //get the result and store it in a variable
echo "<img src =\"images/maps/$r['imgname'']\">"; //write out the dynamic image code
// should print <img src="images/maps/paris.jpg">

I personally would store the entire image path opposed to just the image name to allow me more flexibility in the future.

kenwvs
02-16-2008, 02:31 PM
Thank you for your reply, and you are correct that I am using PHP.

When displaying the results from the DB table, I am displaying all columns, is there a way that I could put the information into the DB table directly through phpMYADMIN and have it be a dynamic address, and have it appear in the results this way.

StupidRalph
02-16-2008, 02:44 PM
Yes. That is what my example shows you. In PHPMyAdmin for my example you would have typed in "paris.jpg" in your column that holds the reference to the image.

In PHP, you will simply write out the value of whats in your DB in your img src attribute.

The site's search function seems to be experiencing difficulties so I couldn't find the link I was looking for but here is a older thread on this same topic.
http://www.codingforums.com/showthread.php?t=118868

kenwvs
02-16-2008, 06:19 PM
I apologize if I am sounding dumb here, but am not quite understanding this.

I am making a list of meetings and have made a jpg of the area where the meeting is held. In the DB table, I have columns for Day, Address, Building, City, Time, Map

In the php file I am using the command

$result = mysql_query("SELECT * from Meetings WHERE City='".$_POST['City']."' and Day='".$_POST['Day']."' ORDER BY Time")
or die(mysql_error());


which then displays all meetings in the table that meet this criteria. If I put the physical location of the map in the column called MAP it isn't dynamic and can't be clicked on. Am I wrong in thinking that I should be able to create a link in this column somehow, and instead, I need to call this map using a separate command?

Thanks,

StupidRalph
02-16-2008, 08:17 PM
If I put the physical location of the map in the column called MAP it isn't dynamic and can't be clicked on. Am I wrong in thinking that I should be able to create a link in this column somehow, and instead, I need to call this map using a separate command?

Thanks,

Why wouldn't it be dynamic? Dynamic in this instance refers to being able to have the application write out the location automagically for you based on certain criteria and not having to hardcode in the values. Whether or not it is clickable is simply X/HTML that you implement with it. If you want a link to the map just use the anchor tag (<a href="path">link</a>)

If you still don't understand, simply code it (or imagine you coded it) statically (without any server-side language or database) and then look at what values change and what stays the same. The values that change should go in your database and used as variables in your PHP. The rest should be regular HTML. This is one of the more popular topics so a quick search can turn up more than a few results as well as a few tutorials.

http://www.google.com/search?q=site:www.codingforums.com+store+image+database&hl=en&start=10&sa=N
http://www.digital-web.com/articles/organizing_images_with_a_database/

kenwvs
02-16-2008, 09:38 PM
Worked like a charm. Thank You very much for sharing your time and talents!!