lukelee Posted November 14, 2008 Share Posted November 14, 2008 hi, guys, Here is my codes: <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> on this page, it shows a list of images, when people click on a image, it will link to house.php, on house.php, there will be the details of the image people click. so the contents in house.php should be dynamic, anyone know how to do this? I wish people understand what I mean. what I am doing here is to make a cms, people can add a new product, each product has a description and image, all descriptions and images are stored in database. and those information will be showed on house.php, so different image people click, different contents display on house.php I wish I got a correct concept of doing this. Can anyone help? thanks Quote Link to comment Share on other sites More sharing options...
justinh Posted November 14, 2008 Share Posted November 14, 2008 do you have the image details stored in a database? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 14, 2008 Share Posted November 14, 2008 you need to pass the unique id for that image to house.php...i'm going to assume the column is 'id' <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['id']; ?>"><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> then in house.php <?php require_once('db.php'); $sql = sprintf("SELECT * FROM image WHERE id = '%s'",mysql_real_escape_string($_GET['id'])); $query = mysql_query($sql) or die(mysql_error()); if(!mysql_num_rows($query)) die("Image not found"); $row = mysql_fetch_assoc($query); //Now you can echo the data from $row ?> Quote Link to comment Share on other sites More sharing options...
waynew Posted November 14, 2008 Share Posted November 14, 2008 <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> What you need to learn about is GET values. Obviously, each image in your database has a unique primary key /index key? Well, when you're printing them out, place that id in a get variable so so: <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?image_id=<?php echp $row['image_id']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> Then on house.php. <?php if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])){ $id = mysql_real_escape_string($_GET['image_id']); $get_details_of_image = mysql_query("SELECT title FROM image WHERE image_id = $id") or die(mysql_error()); } ?> ?> Quote Link to comment Share on other sites More sharing options...
justinh Posted November 14, 2008 Share Posted November 14, 2008 <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['id']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> house.php <?php require_once('db.php'); $id = $_GET['id']; query = mysql_query("SELECT * FROM image WHERE id = $id); while($row = mysql_fetch_array($query)){ $name = $row['name']; $size = $row['size']; } //then just put it in a table I think this would work as you are describing Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 14, 2008 Share Posted November 14, 2008 <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['id']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> house.php <?php require_once('db.php'); $id = $_GET['id']; query = mysql_query("SELECT * FROM image WHERE id = $id); while($row = mysql_fetch_array($query)){ $name = $row['name']; $size = $row['size']; } //then just put it in a table I think this would work as you are describing don't use this code for the following reasons: #1) It doesn't provide any protection from SQL injection #2) The while loop isn't needed and a waste of resources edit: and the string inside mysql_query() isn't closed Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 14, 2008 Author Share Posted November 14, 2008 you need to pass the unique id for that image to house.php...i'm going to assume the column is 'id' <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['id']; ?>"><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> then in house.php <?php require_once('db.php'); $sql = sprintf("SELECT * FROM image WHERE id = '%s'",mysql_real_escape_string($_GET['id'])); $query = mysql_query($sql) or die(mysql_error()); if(!mysql_num_rows($query)) die("Image not found"); $row = mysql_fetch_assoc($query); //Now you can echo the data from $row ?> thanks for help, I see what you mean. here is my database looks like: pid title address thumb imagedata 1 house carnegie 1 4352fdase234.jpg 2 farm carnegie 2 423fdsg43tghgh45.jpg 1 bed oakleigh 1 hytuy56retgy4.jpg 1 hotel springvale 1 554thser4fs.jpg because each house may have more than 1 image, so I use address to identify different houses. thumb = 1 mean this image will be used as a thumbnail for people to click, thumb = 2 will be the images on house.php if people click the carnegie house, on the house.php, 'house' and 'farm' will be displayed on house.php here is the changed codes: <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['address']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> <?php require_once('db.php'); $sql = sprintf("SELECT * FROM image WHERE address = '%s'",mysql_real_escape_string($_GET['address'])); $query = mysql_query($sql) or die(mysql_error()); if(!mysql_num_rows($query)) die("Image not found"); $row = mysql_fetch_assoc($query); //Now you can echo the data from $row ?> it keeps give me the message of Image not found, btw, what is '%s' Quote Link to comment Share on other sites More sharing options...
justinh Posted November 14, 2008 Share Posted November 14, 2008 you should also have an auto_increment id number to help sort different housing Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 14, 2008 Author Share Posted November 14, 2008 you should also have an auto_increment id number to help sort different housing you mean give each house a number? and all images for this house have the same number? but why cant i use address to identify them? I do have auto_increment id, that is pid. Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 14, 2008 Author Share Posted November 14, 2008 seems I cant get this work, I dont know why, the image just cant be displayed on house.php. I have tryed all the methods people told me, there must be something wrong with my codes or the database. Quote Link to comment Share on other sites More sharing options...
waynew Posted November 14, 2008 Share Posted November 14, 2008 Post your code. Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 14, 2008 Author Share Posted November 14, 2008 Post your code. here is my database looks like: pid title address thumb imagedata 1 house carnegie 1 4352fdase234.jpg 2 farm carnegie 2 423fdsg43tghgh45.jpg 1 bed oakleigh 1 hytuy56retgy4.jpg 1 hotel springvale 1 554thser4fs.jpg because each house may have more than 1 image, so I use address to identify different houses. thumb = 1 mean this image will be used as a thumbnail for people to click, thumb = 2 will be the images on house.php if people click the carnegie house, on the house.php, 'house' and 'farm' will be displayed on house.php here is the changed codes: <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['address']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> house.php <?php require_once('db.php'); $sql = sprintf("SELECT * FROM image WHERE address = '%s'",mysql_real_escape_string($_GET['address'])); $query = mysql_query($sql) or die(mysql_error()); if(!mysql_num_rows($query)) die("Image not found"); $row = mysql_fetch_assoc($query); //Now you can echo the data from $row ?> it keeps give me the message of Image not found, btw, what is '%s' Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 15, 2008 Author Share Posted November 15, 2008 <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> What you need to learn about is GET values. Obviously, each image in your database has a unique primary key /index key? Well, when you're printing them out, place that id in a get variable so so: <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?image_id=<?php echp $row['image_id']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> Then on house.php. <?php if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])){ $id = mysql_real_escape_string($_GET['image_id']); $get_details_of_image = mysql_query("SELECT title FROM image WHERE image_id = $id") or die(mysql_error()); } ?> ?> <?php require_once('db.php'); $query = mysql_query("SELECT * FROM image WHERE thumb = '1'"); while($row = mysql_fetch_assoc($query)) { ?> <ul> <li><a href="house.php?id=<?php echo $row['pid']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li> </ul> <?php } ?> <?php if(isset($_GET['pid']) && is_numeric($_GET['pid'])){ $id = mysql_real_escape_string($_GET['pid']); $get_details_of_image = mysql_query("SELECT * FROM image WHERE pid = $id") or die(mysql_error());} while($row = mysql_fetch_assoc($get_details_of_image)) { ?> <ul> <li><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></li> </ul> <?php } ?> I have tried in this way, still not working... Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 15, 2008 Author Share Posted November 15, 2008 I tryed this in house.php <?php require_once('db.php'); $address=$_GET['address']; echo "this is $address!"; ?> the output is this is ! seems the address wasnt been passed to house.php Quote Link to comment Share on other sites More sharing options...
lukelee Posted November 15, 2008 Author Share Posted November 15, 2008 done it, i made a stupid mistake. thanks guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.