<?php
if (session_id() === ''){
session_start();
}
?>
<head>
<style type="text/css">
.pages {width: 200px; border: 1px red solid; padding-bottom:20px;}
.page {border:1px black dotted; height: 20px; width: 15px; background-color:#CCFF99; text-align:center; margin: 2px;}
.curDay {background-color:#CC3300;}
a {text-decoration:none; color:#0000FF;}
a:visited {text-decoration:none; color:#0000FF;}
</style>
<?
$host = "localhost";
$user = "root";
$pass = "root";
error_reporting(E_ALL);
mysql_connect($host, $user, $pass);
if (isset($_GET['install'])){
mysql_query("create database if not exists TEKTIPSTEST") or die (mysql_error());
mysql_select_db('tektipstest') or die (mysql_error());
$tabledef = "
CREATE TABLE IF NOT EXISTS `tektipshotels` (
`hotelID` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`hotelName` VARCHAR( 255 ) NOT NULL
) TYPE = MYISAM";
mysql_query($tabledef) or die (mysql_error()); //create table
//dummy some data
$iQ = '';
if (mysql_result(mysql_query("select count(*) from tektipshotels"),0,0) == 0){
$insertQuery = "Insert into tektipshotels (hotelID, hotelName) values ";
for($i=0; $i<20; $i++){
$iQ []= "(null, 'hotel Number $i')";
}
$insertQuery .= implode (',', $iQ); //get rid of the trailing comma
//insert the data
mysql_query($insertQuery) or die(mysql_error());
}
} else {
mysql_select_db('tektipstest') or die (mysql_error());
}
if (isset($_GET['refresh'])){
unset($_SESSION['hotelIDs']);
}
if (isset($_GET['DEBUG'])){
$get = print_r($_GET, true);
$post = print_r($_POST, true);
$session = print_r($_SESSION, true);
echo <<<HTML
<hr/>
Get variables
$get
<hr/>
Post variables
$post
<hr/>
Session variables
$session
<hr/>
</pre>
HTML;
}
$numHpP = 3; //number of hotels per page
$page = isset($_GET['page']) ? trim ($_GET['page']) : 1;
//display the hotels
displayHotels($page);
function displayHotels($page){
$hotelIDs = array();
global $numHpP;
if (!isset($_SESSION['hotelIDs'])){
loadHotelID();
}
$cnt = 0;
$i = ($page-1) * $numHpP;
while ($cnt < $numHpP){
if (isset($_SESSION['hotelIDs'][$i+$cnt])){
$hotelIDs[] = $_SESSION['hotelIDs'][$i+$cnt];
}
$cnt++;
}
if (!empty($_SESSION['where'])){
$where = " WHERE (".$_SESSION['where'].") AND hotelID IN (".implode(',', $hotelIDs). ")";
} else {
$where = " WHERE hotelID IN (".implode(',', $hotelIDs). ")";
}
$query = "Select hotelID, hotelName from tektipshotels $where";
$result = mysql_query($query) or die(mysql_error());
echo "Currently selected hotels: <br/>";
while ($row = mysql_fetch_assoc($result)){
echo $row['hotelName'] . "<br/>";
}
//do some paging
$numRows = count ($_SESSION['hotelIDs']);
$numPages = ceil($numRows/$numHpP);
$pages='';
$pref = isset($_GET['DEBUG']) ? '&DEBUG' : '';
for ($p=1; $p<=$numPages; $p++){
$class = ($p == $page) ? "page curDay" : "page";
$pages .= "<span class=\"$class\"><a href=\"$_SERVER[PHP_SELF]?page=$p$pref\">$p</a></span>";
}
echo <<<HTML
<div class="pages">
Pages
<div class="pageNumbers">
$pages
</div>
</div>
HTML;
}
function constructWhereClause(){
$where = '';
//add in the conditional provisions
if (empty($where)){
$_SESSION['where'] = $where;
} else {
$_SESSION['where'] = " WHERE $where";
}
}
function loadHotelID(){
if (!isset($_SESSION['where'])){
constructWhereClause();
}
$sql = "select hotelID from tektipshotels $_SESSION[where] ORDER BY rand()";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($result)){
$_SESSION['hotelIDs'][]=$row['hotelID'];
}
}
echo "<br/>click <a href=\"$_SERVER[PHP_SELF]?install\">here</a> to install the dummy database";
echo "<br/>click <a href=\"$_SERVER[PHP_SELF]?refresh\">here</a> to regenerate the random list";