user3077454
user3077454

Reputation: 9

php else block not getting called

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>';

Upvotes: 0

Views: 115

Answers (7)

Emilio Gort
Emilio Gort

Reputation: 3470

Could be in this way, your say you'w using GET method

$string = isset($_GET['string']) ? $_GET['string'] : NULL;

if(!empty($string ){
   //your stuff
}

Side Note: read this link How can I prevent SQL injection in PHP?

Upvotes: 0

Mike
Mike

Reputation: 24383

To understand why it's not working, we need to break it apart into parts.

First, you are comparing the value of isset($_REQUEST['string']) (which will always return a boolean) to an empty string.

If you do

var_dump(isset($_REQUEST['string']));

You should see either bool(true) or bool(false) as the output depending on if it's set.

However when you compare a boolean to a string, it will be type cast the boolean into a string so that both types are the same.

var_dump( (string) false ); // Outputs string(0) ""
var_dump( (string) true ); // Outputs string(1) "1"

So you are essentially doing the following:

// When $_REQUEST['string'] is unset
if ("" <> "") { ... } // Will always be false because "" === ""

// When $_REQUEST['string'] is set
if ("1" <> "") { ... } // Will always be *true* because "1" != ""

So therefore, the reason must be that $_REQUEST['string'] is, in fact, unset. I have tested this myself in PHP 5.5.3:

$set_var = 'i_am_set';
var_dump( isset($set_var) <> '' ); // Outputs bool(true)
var_dump(  isset($unset_var) <> '' ); // Outputs bool(false)

However, I think that what you're looking for is empty() which will return false if the variable is either unset or empty.

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

OP's condition could also be written as if(!isset($_REQUEST['string']))


EDIT : I was wrong earlier about:

<> is SQL syntax

The PHP equivalent would be != therefore your if(isset($_REQUEST['string'])<>'') should be if(isset($_REQUEST['string'])!='')

Upvotes: 2

jsgrosman
jsgrosman

Reputation: 1

isset() has a boolean return value.

See http://www.php.net/isset.

You should just be able to use it in the if clause.

if(isset($_REQUEST['string'])) 
{

}
else
{

}

You can also use array_key_exists(). See http://www.php.net/function.array-key-exists. This doesn't actually check to make sure that the request parameter has a useful value.

if(array_key_exists('string', $_REQUEST)) 
{

}
else
{

}

empty() will check for an array key, null, and no value. That's what I'd use.

if(!empty($_REQUEST['string'])) 
{

}
else
{

}

Upvotes: -2

Ali
Ali

Reputation: 3461

Your IF-condition is wrong

isset returns true or false it does not return a value that could be compare to empty or what is between your quotes

if(!empty($_REQUEST['string'])) 

The above if statement should be enough to check if the value is set and if it's not empty\

Some things you should read

isset() vs empty() vs is_null()

empty()'s documentation

Upvotes: 2

Catalin
Catalin

Reputation: 803

Try using this as the condition for the IF:

if(isset($_REQUEST['string']) && trim($_REQUEST['string'])!=''){ ... 

First it will check if the variable exists, and if it does, it will check if it has some text in it - just in case

Upvotes: 0

Te Ko
Te Ko

Reputation: 768

Use

if(isset($_REQUEST['string'])) { /*...*/ } else { /*...*/ }

or

if($_REQUEST['string'] != '') { /*...*/ } else { /*....*/ } (while the latter probably throwing a warning in case of the parameter string was not passed at all)

Not both at the same time like you did.

Upvotes: 0

Related Questions