<?php
session_start();
include("Admin/MySQL_Connection.php");
#----------------[ Showing Advertisements ]-----------------
if(isset($_GET['Cat_Key']))
{
$Cat_Key = $_GET['Cat_Key'];
$sql = mysqli_query($con, "SELECT COUNT(ad_id) FROM ads WHERE ad_cat='$Cat_Key'");
}
else $sql = mysqli_query($con, "SELECT COUNT(ad_id) FROM ads");
$query = $sql;
$row=mysqli_fetch_row($query);
$rows = $row[0];
$page_rows = 6;
$last = ceil($rows/$page_rows);
if($last < 1)
{
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn']))
{
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1)
{
$pagenum = 1;
}
else if ($pagenum > $last)
{
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$list = '';
$sort = "ad_id DESC";
if(isset($_POST['Sort_Ads']))
{
$sortit = $_POST['Sort_Ads'];
$_SESSION['sortorder'] = $sortit;
if($sortit == "Most Recent Ads") $sort = "ad_id DESC";
else if($sortit == "Price: Low to High") $sort = "ad_price ASC";
else if($sortit == "Price: High to Low") $sort = "ad_price DESC";
}
elseif(isset($_SESSION['sortorder']))
{
$sortit = $_SESSION['sortorder'];
if($sortit == "Most Recent Ads") $sort = "ad_id DESC";
else if($sortit == "Price: Low to High") $sort = "ad_price ASC";
else if($sortit == "Price: High to Low") $sort = "ad_price DESC";
}
if(isset($_GET['Cat_Key']))
{
$Cat_Key1 = $_GET['Cat_Key'];
$_SESSION['Cat_Key_Ses'] = $Cat_Key1;
$Cat_Key = $_SESSION['Cat_Key_Ses'];
$sql = mysqli_query($con, "SELECT * FROM ads WHERE ad_cat='$Cat_Key' ORDER BY $sort $limit");
if(mysqli_num_rows($sql) == 0) $list = "<h2 style='text-align:center; background-color:#FFF;'>No results found in Category $Cat_Key!</h2>";
}
else $sql = mysqli_query($con, "SELECT * FROM ads ORDER BY $sort $limit");
$query = $sql;
$textline1 = "Ads (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
if($last != 1)
{
if ($pagenum==1) $paginationCtrls .= '<';
else if ($pagenum > 1)
{
$previous = $pagenum - 1;
if(isset($_GET['Cat_Key'])) $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?Cat_key='.$Cat_Key.'?pn='.$previous.'"><</a>';
else $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'"><</a>';
}
if ($pagenum==$last) $paginationCtrls .= '>';
if ($pagenum != $last)
{
$next = $pagenum + 1;
if(isset($_GET['Cat_Key'])) $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?Cat_key='.$Cat_Key.'?pn='.$next.'">></a> ';
else $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">></a> ';
}
}
if(isset($_POST['Show_Ad_Bar']))
{
$_SESSION['Display'] = 'BAR';
}
else if(isset($_POST['Show_Ad_Box']))
{
$_SESSION['Display'] = 'BOX';
}
if (!isset($_SESSION['Display'])) $_SESSION['Display'] = 'BAR';
while($row_ads = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
$ad_id = $row_ads['ad_id'];
$ad_title = $row_ads['ad_title'];
$ad_phone = $row_ads['ad_phone'];
$ad_email = $row_ads['ad_email'];
$ad_cat = $row_ads['ad_cat'];
$ad_pro = $row_ads['ad_pro'];
$ad_type = $row_ads['ad_type'];
$ad_address = $row_ads['ad_address'];
$ad_date = $row_ads['ad_date'];
$ad_time = $row_ads['ad_time'];
$ad_details = $row_ads['ad_details'];
$ad_price = $row_ads['ad_price'];
$ad_currency = $row_ads['ad_currency'];
$ad_img = $row_ads['ad_img'];
$today = date('Y/m/d', time());
$yesterday = date("Y/m/d", strtotime("-1 day"));
if($ad_date==$today) $ad_date = "Today";
else if($ad_date==$yesterday) $ad_date = "Yesterday";
if ($_SESSION['Display'] == 'BAR') $list .= "
<table id='Ad_Bar'>
<tr>
<td width='80'>$ad_date</td>
<td width='110' rowspan='3' align='center'><a href='Index.php?ad=$ad_id'><img width='100' height='80' border='0' src='User/Ad_Images/$ad_img'></img></a></td>
<td width='300'><a href='Index.php?ad=$ad_id'>$ad_title</a></td>
<td width='80'>$ad_pro</td>
</tr>
<tr>
<td width='80'>$ad_time</td>
<td width='300'>$ad_currency</td>
<td width='80'>$ad_price</td>
</tr>
<tr>
<td width='80'></td>
<td width='300'>$ad_type</td>
<td width='80'>$ad_cat</td>
</tr>
</table>
";
else if ($_SESSION['Display'] == 'BOX') $list .= "
<table id='Ad_Box'>
<tr>
<td colspan='2' align='left'>$ad_type</td>
</tr>
<tr>
<td align='center' colspan='2'><a href='Index.php?ad=$ad_id'>$ad_title</a></td>
</tr>
<tr>
<td colspan='2' align='center'><a href='Index.php?ad=$ad_id'><img width='160' height='130' border='0' src='User/Ad_Images/$ad_img'></img></a></td>
</tr>
<tr>
<td align='left'>$ad_date</td>
<td align='right'>$ad_time</td>
</tr>
<tr>
<td align='left'>$ad_price</td>
<td align='right'>$ad_currency</td>
</tr>
<tr>
<td align='left'>$ad_pro</td>
<td align='right'>$ad_cat</td>
</tr>
</table>
";
}
?>
//And the html area
<form id="formName" action="Index.php" method="POST">
<input type="submit" name="Show_Ad_Bar" onchange="document.getElementById('formName').submit()" value="bar"></input>
<input type="submit" name="Show_Ad_Box" onchange="document.getElementById('formName').submit()" value="block"></input>
<select name="Sort_Ads" id="Sort_Ads" onchange="document.getElementById('formName').submit()">
<option>Most Recent Ads</option>
<option>Price: High to Low</option>
<option>Price: Low to High</option>
</select>
</form>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<div id="pagination_controls"><?php echo $textline2; ?><?php echo $paginationCtrls; ?></div>
<p><?php echo $list; ?></p>
</div>