<?php
if (!isset($_GET['category_id']) || !isset($_GET['offset']))
{
die('invalid input');
}
require_once('class_apartment.php');
require_once('class_category.php');
require_once('class_database.php');
require_once('class_page.php');
require_once('globals.php');
$page = null;
$db = null;
$category = null;
$total_rows = null;
$loop_page_count = null;
$apartments = null;
$page = new Page();
$db = new Database();
if (!$db->connect())
{
die('Fatal error: could not connect to database.');
}
// set title
$category = new Category();
$category->set_id($_GET['category_id']);
$category->retrieve($db->get_handle());
$page->append_title('Browsing ' . $category->get_title() . ' Apartments ');
// apartments
$row_count = 0;
$apartments = array();
Apartment::search_category($apartments, $_GET['category_id'], $_GET['offset'], $db->get_handle());
$page->append_content('<table class="data_table" cellspacing="0" cellpadding="0" width="100%">
<tr><td class="field_name" style="width: 70%">Apartment</td><td class="field_name" style="width: 30%">Date Posted</td>');
for($i = 0; $i < count($apartments); $i++)
{
$page->append_content('<tr'.(Page::row_shade($row_count)).'>
<td>
<a href="apartment_view.php?apartment_id='.$apartments[$i]->get_id().'">'.$apartments[$i]->get_title().'</a><br>
<small>'.$apartments[$i]->get_location().'</small>
</td>
<td>'.$apartments[$i]->get_timestamp().'</td>
</tr>');
}
$page->append_content('</table>');
// pages
$page->append_content('<p> </p>Pages: ');
$total_rows = Apartment::search_category_total_rows($_GET['category_id'], $db->get_handle());
$loop_page_count = 1;
$current_page = $_GET['offset'] / RESULTS_PER_PAGE;
// determine starting page, put dots if more than 5 exist before current page
if ($current_page > 5)
{
$starting_page = $current_page - 5;
$page->append_content('... ');
}
else
{
$starting_page = 0;
}
for ($i = 0; $i <= $total_rows; $i++)
{
if (($i == 0 || $i % RESULTS_PER_PAGE == 0))
{
if ($loop_page_count >= $starting_page)
{
if ($loop_page_count > ($current_page+5))
{
// if loop page count is > 5 away from current page, break
$page->append_content(' ...');
break;
}
else
{
if ($current_page+1 == $loop_page_count)
{
// put text if current
$page->append_content('<b>[ '.$loop_page_count.' ]</b> ');
}
else
{
// put a link to the offset
$page->append_content('<a href="apartment_search.php?category_id='.
$_GET['category_id'].'&offset='.$i.'">[ '.$loop_page_count.' ]</a> ');
}
}
}
$loop_page_count++;
}
}
$page->display();
?>