<?php session_start();
//Check if the session is set. If not, this means visitor is most likey a new one or session expired.
if( !isset($_SESSION['ad_id']) )
{
//Try to get ad details of the very first ad
$result = mysql_query("SELECT ad_id, ad_title, ad_image FROM ads WHERE is_enabled = 1 ORDER BY ad_id ASC LIMIT 1");
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 )
{
//No ads. You can't display ads if you don't have them. Can you???
}
else
{
//Oh yes, ad is present. So get it's details & store it's id in session var.
$row = mysql_fetch_array($result);
$_SESSION['ad_id'] = $row['ad_id'];
}
}
elseif( isset($_SESSION['ad_id']) )
{
//Because the session ad_id exists, we try to get the ad information of the next ad which is present in our ads table.
$result = mysql_query("SELECT ad_id, ad_title, ad_image FROM ads
WHERE is_enabled = 1 AND ad_id > ".$_SESSION['ad_id']." LIMIT 1");
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 )
{
//No ads. greater than the ad_id. It's possible that there are no more ads after this. So start from beginning of ads table
$result = mysql_query("SELECT ad_id, ad_title, ad_image FROM ads WHERE is_enabled = 1 ORDER BY ad_id ASC LIMIT 1");
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 )
{
//No ads. Add some, stranger!
}
else
{
//If ad is present, then process it.
$row = mysql_fetch_array($result);
$_SESSION['ad_id'] = $row['ad_id'];
}
}
else
{
//If ad is present, then process it & store the id in session.
$row = mysql_fetch_array($result);
$_SESSION['ad_id'] = $row['ad_id'];
}
}
?>
|