I am noob in php and I have spent many hours trying to understand why my else block is not getting called. I have tried number of things but just not sure why it is not getting called. I am sure its something stupid i am doing.
<?php
$base_url = 'http://localhost/mine/';
$per_page = 3; //number of results to shown per page
$num_links = 8;
$cur_page = 1; // set default current page to 1
if(isset($_REQUEST['string'])<>'')
{
$search_string = " title LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR description LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'";
$query = mysql_query("SELECT * FROM products WHERE ".$search_string);
$row = mysql_num_rows($query) or die(mysql_error());
$row = $row;
$cur_page = $_GET['page'];
$cur_page = ($cur_page < 1)? 1 : $cur_page;
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($row/$per_page); // no of page to be created
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
$res = mysql_query("SELECT * FROM products WHERE ".$search_string." ORDER BY title LIMIT ".$per_page." OFFSET ".$offset);
while($row=mysql_fetch_array($res))
{
include ('form.php');
}
}
else
include ('alert.php');
?>
i forgot to add that i am using the get method for the url's . So i think it might be possible that even when there is no search term it is still showing index.php?string= which would mean that it is still taking the if statement as true and not going to the else statement. Please correct me if i am wrong.
here is a snippet from my pagination.php
if (strstr($_SERVER['REQUEST_URI'], "?string="))
{
echo '<span id="prev"> <a href="' . $_SERVER['PHP_SELF'] . '?string=' . $_GET['string'] . '&page=' . (1) . '">' . $dir . '</a> </span>';
}
else
echo '<span id="prev"> <a href="' . $_SERVER['PHP_SELF'] . '?page=' . (1) . '">' . $dir . '</a> </span>';