Jump to content

how to auto generate contents on a page ?


lukelee

Recommended Posts

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

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

<?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());
}

?>

?>

Link to comment
Share on other sites

<?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

 

 

 

Link to comment
Share on other sites

<?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 :)

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

<?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...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.