Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display specific images with mysql / PHP 2

Status
Not open for further replies.

Programz8

Programmer
Mar 27, 2007
33
US
Greetings all:

I'm trying to figure out how I can pull an image from my database and display it on my website. I ONLY WANT A SPECIFIC image that I can call by the id number. I have bigimage and image as fields on a table in my DB. How can I call only specific images instead of using an array or random image display?


I want to display the image(s) on a page called kma_steelo.php

So can I somehow write a line of code like this
echo kma_steelo.php?$bigimage?id=5
echo kma_steelo.php?$image?id=22

Or would it be a line of code like
echo $bigimage.php?id=5
echo $image.php?id=23

??????????????????????????????????????????????????


Thanks.






 
how about something more like this

Code:
$result = mysql_query('select bigimage from  tablename where id='.mysql_escape_string(trim($_GET['id'])) 
  or die (mysql_error());
if (mysql_num_rows($result) > 0){
 $image = mysql_result($result, 0,0);
 header('Content-type: image/jpeg');
 header('Content-length: ' . strlen ($image));
 echo $image;
} else {
  //output some default image
}
 
Thanks I'll check this out when I get home. I'm sorry IF I wasn't very clear but I need to implement this code with multiple specified images? Such as Select.....WHERE id = 8 OR ID = 15 OR ID 17

What I'm trying to do is display multiple images from my server on the same page. the $image field stores only text links ie. <img src=picture>
So if I want 3 items to display on the page I want
<echo $image?id=1> then echo<image?id=15> then echo<image?id=35>

The trick is that I want to select which images are displayed.

Thanks.
 
that's a completely different kettle of fish and you've made it materially more difficult to do this by storing the images in the db.

you will need two scripts.

to answer your question fully - please explain how the script gets the id's of the images
 
The OP said that the DB only has text links to the images.

However we really need to know how your page pulls out the image links from the DB.


But if i had to guess, you would have to execute a query for the specified ID and then echo out the link into an image tag:

Code:
echo"<img src='$linkfromdb'>"

however before that can be done we need to know how they are being pulled from the DB. Could you show us the code that pulls the image links from the DB?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
@vacunita
you may be right but i didn't read his postings like that. i take from the first post that the images are stored in the db and from the second post that he wants to generate links which will in turn pull the actual image.

@OP - take another shot at explaining your issue.
 
I read
the $image field stores only [red]text links[/red] ie. <img src=picture>

I took that to be something like "\path\to\image.jpg"

But we still need some code to understand what is going on.

This:
Code:
echo $bigimage.php?id=5
or this
<echo $image?id=1>
make no sense to me.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Okay people first off I appreciate all of your help. What does @OP mean? oh Original Poster? lol. Now to clear things up I am storing a link to the image in my DB not the actual image. Just a simple link in a field such as <img src=pictureGoesHere.jpg"> then I use a variable to call the link. I'll give an example below but firsthere is what I want to do in html.

<tr><td><a href="teeloview5.html"><img src="er24772.jpg" width="90" height="113"></a>&nbsp; &nbsp; &nbsp;
<tr><td><a href="teeloview6.html"><img src="er27308.jpg" width="90" height="113"></a>&nbsp; &nbsp; &nbsp;
<tr><td><a href="teeloview7.html"><img src="rw102187.jpg" width="90" height="113"></a>&nbsp; &nbsp; &nbsp;
<tr><td><a href="teeloview8.html"><img src="rw124197.jpg" width="90" height="113"></a>&nbsp; &nbsp; &nbsp;

Now I'm trying to accomplish that with php Here is an example below of just one image being shown on a page I have already:

CODE ABOVE HTML:
$db_name = "database"; $table_name = "example";
$connection = @mysql_connect("localhost", "database", "***") or die (mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
$sql ="SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE id = \"12\" ";
$result = @mysql_query($sql,$connection) or die ("aint gonna be able to do it");
while ($row = mysql_fetch_array($result)) {
$article_name = $row[article_name];
$image = $row[image];
$text = $row[text];
$bigimage = $row[bigimage];
$alttext = $row[alttext];
}

CODE IN HTML:
<a href="kma_ce.php?id=12"><? echo "$image"; ?><? echo "$text"; ?></a>

This is what I want to do. However in this example $image is being pulled from the $sql statement where the ID is specified. Now I want to be able to display another image
on the page the same way only with a different ID. so maybe my select statement would be like Select.......FROM $table_name WHERE id=12 or id=15 or id=17
then I want to display each picture on the page like this
<a href="kma_ce.php?id=12"><? echo "$image"; ?><? echo "$text"; ?></a><br>
<a href="kma_ce.php?id=15"><? echo "$image"; ?><? echo "$text"; ?></a><br>
<a href="kma_ce.php?id=17"><? echo "$image"; ?><? echo "$text"; ?></a>

So I need each image to pull from the DB to display it for the link. I hope this clears things up please let me know if you need more information to further assist me
with this matter. Thanks, again.

Programz







 
Code:
$ids = array(1, 3, 5, 6, 17, 32);
$sql = "SELECT article_name, image, text, bigimage, alttext,id FROM $table_name WHERE id IN (". implode(",", $ids) . ")";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($result)){
  $image = str_replace ("src=", "alt=\"".$row['alttext'].\" src=", $row['image']);
  echo <<<HTML 
 <a href="kma_ce.php?id={$row['id']">{$image}{$text}</a><br>
HTML;
}
the above code also pulls apart the image html and inserts what i presume to the be alt text before reassembling the tag.

the difficulty may be in getting the IDs in an array to start with. this depends on the format in which you receive the id's in the first place.
 
Thanks for the response the problem is that the id is generated by auto incrementing the id field in the db.

This is exactly what I want to do but is it possible to do this without using an array? I want to be able to pick and choose which id I want to show.
 
I might be a little slow, but aren't you doing just that? with the 12????

Or what you want is being able to make links to specific images, and then re using your code to get them out???

instead of hard coding the 12 in the query use a variable:
something like:
$sql ="SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE id = $id_variable";

now if you want to use the link format you posted just address the parameter you are passing at the end via the GEt super global.

so.

$id_variable=$_GET['id'];

When you have a link that looks like this:
<a href="kma_ce.php?id=12">View image 12[/a>

this will send the 12 value to the $_GET variable which you can then use to place inside your query.

I hope that's what you mean.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Unfortunately that doesn't solve my problem. I want to be able to show image1 next to image2 next to image15 next to image 17. How can I do that on the same page without using an array. There has to be 2 things accomplished here.

1) The script has to retrieve these links to images from the database and display them. ie... echo $image

2) It can't be an array the database has more links to images than the ones I want to display on this page.

@vacunita your solution doesn't solve my problem because that only works if you want to display one image on a page. What if you have more than one image? There has to be a way to pull just the image field from the database by the record's ID number.

@jpadie your solution doesn't solve my problem because you are using an array. In an array the database will just pull the next available image. I want to be able to show only certain images in no specific order like

Echo image 15 then next to that echo image 453 then next to that image 67.

I'm not sure if I am explaining it right but my problem is being able to pull any image from the db and display it on a page that has other images from the db displayed on the same page. I do appreciate everyone's help, I am learning some neat stuff here.

Programz.
 
In an array the database will just pull the next available image.

what? it will 'pull' anything you tell it to.

the question i have now asked twice is the one that you need to answer. From where do you get the list of id's to display?
 
This is why I am here asking these questions, lol. I thought that when you create an array it had to be in a order sequence such as 1,2,3,4 I didn't know you could just say 1,17,65,463.

Okay that means that your solution should work for me. The data including Id's are entered on a back end application where I wrote a simple script with an Insert Statement. There is also a few lines of code that I wrote to show me all records in the database by record name. I use the record name to choose the record I want displayed and then I grab the ID by hovering over the link in the show records back end application. So I guess to answer your question I create the list myself from products that are on sale this week or new products.

For instance if I have a store where I sell bikes. I have a picture of 4 bikes on the website. But then we no longer have a bike in stock and so I find another bike and create a new record with the new content so that I can show that bike instead.

Does this answer your question?

Thanks

Programz.
 
Just specify which ID's you want. and *ehem* cycle (sorry for the pun) through them.

$idlist=array(12,27,42,5,17,135);

You can issue a different query for each ID, or use the "OR" construct to add them to the single query. Then you can cycle through the query results.
For Example:
Code:
$query="SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE ";
for($i=0;$i<=count($idlist)-1;$i++){
$query.="id=$idlist[$i] ;
}
if($i<=(count($idlist)-1){
$query=" OR ";
}

$results=mysql_query($query);
while($row=mysql_fetch_array($results)){
echo "<img src='$row['image']'>";
}

That will cycle through the array of ID's you specify, and create the query for you. :

SELECT article_name, image, text, bigimage, alttext FROM mytable WHERE id=12 Or id=27 Or id=42 etc....

Execute the query to pull out those records, and then cycle through them to output the images.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the easiest way of generating the id's is via an html form. you output a list of, say, picture names with check boxes and then submit the form. the receiving script takes the ids and dynamically creates the array. consider this

Code:
<?
//attach to the database here
$db_name = "database"; 
$tablename = "example";
$connection = @mysql_connect("localhost", "database", "***") or die (mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

if (isset($_POST['submit'])){
	displayImages();
} else {
	displayForm();
}
function displayImages(){
	global $tablename;
	$ids = parseIntoArray();
	$sql = "SELECT article_name, image, text, bigimage, alttext,id FROM $tablename WHERE id IN (". implode(",", $ids) . ")";
	$result = mysql_query($sql) or die (mysql_error());
	while ($row = mysql_fetch_assoc($result)){
	  $image = str_replace ("src=", "alt=\"".$row['alttext']."\" src=", $row['image']);
	  echo <<<HTML
	 <a href="kma_ce.php?id={$row['id']}">{$image}{$text}</a><br>
HTML;
	}
}

function parseIntoArray(){
	//get a list of actual ids to cross check user input
	global $tablename;
	$validID = array();
	$sql = "select id from $tablename";
	$result = mysql_query($sql) or die (mysql_error());
	while ($row = mysql_fetch_assoc($result)){
		$permittedID[] = $row[0];
	}
	
	foreach($_POST['id'] as $id=>$val){
		if (in_array($id, $permittedID)){
			$validID[] = $id;
		}
	}
	return $validID;
}
function displayForm(){
	global $tablename;
	$sql = "select `text`, id from $tablename";
	$result = mysql_query($sql) or die (mysql_error());
	$output = <<<STYLE
<style type="text/css">
.even_row {width:60%; margin:0 auto; border: 1px black solid; background-color:#FFFFFF;}
.odd_row {width:60%; margin:0 auto; border: 1px black solid; background-color:#CCCCFF;}
.cbox {width: 20px;}
</style>
<form action="{$_SERVER['PHP_SELF']}" method="post">

STYLE;
	while ($row = mysql_fetch_assoc($result)){
		$class = ($class=="even_row") ? "odd_row" : "even_row";
		$output .= <<<HTML
	<div class="{$class}">
		<label for="id[{$row['id']}]">
			<span class="cbox">
				<input type="checkbox" name="id[{$row['id']}]" id="id[{$row['id']}]" />
			</span>
			<span class="label">
				{$row['text']}
			</span>
		</label>
	</div>
HTML;
	}
	$output .= <<<HTML
	<div class="row">
		<input type="submit" name="submit" value="Get Photos" />
	</div>
	</form>
HTML;
	
	echo $output;
}
?>
 
Hey guys I've created an html version of what I want so you can actually see what I am trying to do I hope this helps. -- There are images and every image is a clickable image (the text should be clickable too if possible) that takes you to a detail view. Now at any given time I want to be able to change any image and text for another image and text. I tried the following code per vacunita's advice but I have several errors popping up. Maybe with and actual view of what I want to do this will help with an explanation of what kind of code I need. Thanks again.

<?
$db_name = "cayema"; $table_name = "Articles";
$connection = @mysql_connect("localhost", "cayema", "red27") or die (mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
//$sql ="SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE a_id = \"12\" ";
//$idlist=array(12,27,42,5,17,135);
//$sql = "SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE a_id = $id_variable";
$query="SELECT article_name, image, text, bigimage, alttext FROM $table_name WHERE a_id=12 or a_id=8 ";
for($i=0;$i<=count($idlist)-1;$i++){
$query.="id=$idlist[$i] ;
$results=mysql_query($query);
}
if($i<=(count($idlist)-1){
$query="a_id=12 OR a_id=8 ";
}
?>
<html>
while($row=mysql_fetch_array($results)){echo "<img src='$row['image']'>";}
</html>
 
Jpadie, That code is too complex for me without any comments. Is there any way you can comment the code so I can follow what is going on? I am new to PHP and learned the language myself. I certainly would appreciate it. I tried loading your code but it didn't display a form properly though I did see an get photos button.
 
O.k. i think you were trying to tell us something and we understood something else.


You have 3 actions you need to perform from the same page.

1 Display the list of links
2. display a detailed view of a clicked link
3. display a larger image of the clicked detail view.

I'm not exactly sure how to explain the entire process without getting confusing. As it requires that you have conditional statements and run different queries to get the information you want when you want it.













----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'm sorry I'm obviously not explaining this properly please bear with me. I know how to create the detailed view page and the bigger image after the original link is clicked. I only need to create the main page which has images with text under them that can be made into a link. The original page will consist of several images that can be clicked on. Each image is different and will have text under them describing what the image is. I will constantly update the images to different images with different text. Thanks.

Programz.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top