I hope the subject isn’t too confusing. I’ll try explaining better below.
I’m working on a site where people can upload ads/articles. They go to advertise.php, fill in all the forms, choose a picture to upload and submit… The ad gets stored in the database and I’ve later made a search site where people can search & select from the list of ads which ad they would like to browse. This is for search.php.
When you find what you’re looking for from the result, you just go ahead click the link to the ad and it opens up in showad.php
In the page where the ad is shown, all forms from which was previously filled out get echoed, with the belonging picture. If you would click the picture, it would open up in show.php, which is connected to the AId(articles ID).
Now. At the table on search.php I’ve chosen to show the result with a table, containing: Object, Category, Price, Date, City. I’ve later come up with the brilliant idea of adding the picture from the ads corresponding showad.php-id as a thumbnail and add it to the table.
I will share the codes for Search.php, Showad.php and Show.php, because I feel the answer is in either showad or show…
Search.php
[php]
$sql = "SELECT * FROM category";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo "<option value=\"" . $row["catID"] . "\">" . $row["catName"] . "</option>\n";
}
$sql="";
?>
</select></td>
<th align="right">City:</th><br />
<td><select name="city">
<option value="">Whole country</option>
<?php
$sql = "SELECT * FROM city";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo "<option value=\"" . $row["cityID"] . "\">" . $row["cityName"] . "</option>\n";
}
$sql="";
?>
</select></td>
<td align="right"></td>
<td> <input type="submit" value="Submit" name="searchbutton" /></td>
</form></tr></table>
<table class="borderall">
<tr>
<th align="center" width="200" class="borderright">Object</th>
<th align="center" width="100" class="borderright">Category</th>
<th align="center" width="40" class="borderright">Price</th>
<th align="center" width="100" class="borderright">Date</th>
<th align="center" width="100">City</th>
</tr>
<?php
$search = !isset($_GET["search"]) ? '' : $_GET['search'];
$cat = !isset($_GET["cat"]) ? '' : $_GET['cat'];
$city = !isset($_GET["city"]) ? '' : $_GET['city'];
$sql = "SELECT * FROM tblad, city, category WHERE tblad.catID=category.catID AND tblad.cityID = city.cityID";
if ($search!="")
{
$sql .= " AND Object LIKE '%{$search}%' ";
}
if ($cat!="")
{
$sql .= " AND tblad.catID={$cat} ";
}
if ($city !="")
{
$sql .= " AND city.cityID={$city} ";
}
$sql .= " ORDER BY DateTime DESC";
echo “
”;
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo “
echo “
echo “
echo “
echo “
echo “
echo “
}
//echo $sql;
?>[/php]
Showad.php
[php]<?php
include(“connect.php”);
error_reporting(E_ALL ^ E_NOTICE);
$aid = $_GET[“id”];
if (is_numeric($aid)){
//header (“location:search.php”);
}
$sql= “SELECT * FROM tblad, category, city, tblpic
where tblad.AId=tblpic.AId
AND tblad.catID=category.catID
AND tblad.cityID=city.cityID
and tblad.AId=$aid”;
/*
Old code for joining tables and columns. Leaving it here in case something was forgotten when the code above was written. In that case, we have the original code below.
$sql= "SELECT tblad.*, category.catName, tblpic.picFile FROM tblad INNER JOIN category ON tblad.catID=category.catID LEFT JOIN tblpic ON tblad.AId=tblpic.AId WHERE tblad.AId=$aid
";
*/
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_array($result);
?>[/php]
Show.php
[php] <?php
$conn = mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“database”);
$imagedir = “pics/”;
$id = $_GET[‘id’];
if(!is_numeric($id)) {
die(“Cheater!!! stop!”);
}
$sql = mysql_query(“SELECT tblpic.* FROM tblpic WHERE PID=$id”);
if(!$sql) {
die(“Databasal error”);
}
else {
$result = mysql_fetch_array($sql);
if(!$result) {
die("Cannot find that particular file");
exit();
} else {
$length = filesize($imagedir. $result['PicFile']);
header('Content-Length: ' . $length);
header("Content-Type: " . $result['PicType']);
readfile($imagedir.$result['PicFile']);
}
}
//echo $sql;
exit();
[/php]