creating a classified - stuck
I am a beginner in web designing (php, mysql). I am creating a classified similar to Craigslist but not as complex.
I managed to get the user ad input as well as pics inserted into mysql databases and everything is working perfectly.
I also managed to fetch the data when a user searches a specific category like cars
$query="SELECT city, title, price FROM md_post WHERE catego开发者_如何转开发ry='cars'";
$result= mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo "<a href='XXXX.php'>". $row['title']. " - ". $row['price']." - ".$row['city']."</a>";
echo "<br /><br />";
OUTPUT IS: Charlotte - NISSAN - $9000 Charlotte - BMW - $11000
what i want now is, when a user clicks on a title ad, the user is redirected to the ad page itself. ( just the like what Craigslist or any classified would do)
how can I do this? I know it is a general questions and i am not expecting anybody to provide a whole code but i need hints and ideas please.
I will be very grateful. Thanks
You would need the records in your database to have a unique field that is auto_incrementing (counting up automatically when a new row is inserted).
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
When you select your ads from the database, you can take the ID for that ad out of the database at the same time
$query="SELECT ad_id, city, title, price FROM md_post WHERE category='cars'";
Then get the ad_id in the same way $row['ad_id']
.
You could then post this straight in to your HTML tag to form a URL like: ad.php?id={ID OF AD HERE}
echo "<a href='ad.php?id=' . $row['ad_id'] . '>". $row['title']. " - ". $row['price']." - ".$row['city']."</a>";
Then in your ad.php you can pic this ID up using
echo $_GET['id'];
You can use this ID in a query to select the correct ad... but make sure if you're selecting from your database to use mysql_real_escape_string($_GET['id'])
to prevent SQL injection.
If you know the address of the ad_page just put it inside your href
$query="SELECT city, title, price FROM md_post WHERE category='cars'";
$result= mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)){
$ad_page = ..;
echo "<a href="'.$ad_page.'">". $row['title']. " - ". $row['price']." - ".$row['city']."</a>";
echo "<br /><br />";
}
if it comes from your db, just put it into your query.. or what else..
精彩评论