Help me to solve this if..else issue

Discussion in 'PHP' started by ceylongeek, Nov 13, 2010.

  1. #1
    Guys please help me to solve the bug of below script.

    <?php 
      //Check page incomes
      session_start(); 
      if(isset($_SESSION['page'])){
      $page1=$_SESSION['page'];      
      } 
      //If no data show as error 1
      else{
        echo '<br/>Error 1';
      }   
      
      //Start selection
      if($page1==2){
          $title=('Testing title 1');
          $description=('Testing description 1'); 
      }  
      elseif($page1==3){
          $title=('Testing title 2');
          $description=('Testing description 2');
      }    
      
      //If not in above range echo error 2
      else{
          echo $page1;
          echo '<br/>Error 2';
      }    
      
      //Register new sessions
      if(isset($title)){ 
          $_SESSION['title']=$title;
          $_SESSION['description']=$description;       
          header("location:about.php");
         echo $title;
      }  
      
    ?>
    PHP:

    This is written to select the correct title with description for the registered session parameter numbers.When $page1=2 or 3 this gives the following error(I need to register correct title session and description session to send to another page)

     Error 1
    Notice: Undefined variable: page1 in C:\wamp\www\Kingmax\page_check.php on line 13
    
    Notice: Undefined variable: page1 in C:\wamp\www\Kingmax\page_check.php on line 17
    
    Notice: Undefined variable: page1 in C:\wamp\www\Kingmax\page_check.php on line 24
    
    Error 2
    HTML:
    But if those parameters out of selection range this prints incoming parameter and error correctly like below.

    5
    Error 2
    HTML:
    So I commented the last session registration section and tell apache to print my title and description.In that case it returns the correct title with correct description.I think the problem is with my session section.This is what I did.

    //If not in above range echo error 2
    else{
    echo $page1;
    echo '<br/>Error 2';
    }

      //Register new sessions
     /* if(isset($title)){ 
          $_SESSION['title']=$title;
          $_SESSION['description']=$description;       
          header("location:about.php");
         echo $title;      
      }                 */
      
        echo $title;
        echo $description;
      
    ?>
    PHP:
    Then it returns

    Testing title 1Testing description 1
    HTML:
    I can't understand the error guys please help me to solve this.


    Thank you
     
    ceylongeek, Nov 13, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    declare page1 first

    
    <?php 
      //Check page incomes
      session_start(); 
    
    $page1 = -1;
    
      if(isset($_SESSION['page'])){
      $page1=$_SESSION['page'];      
      } 
      //If no data show as error 1
      else{
        echo '<br/>Error 1';
      }   
      
      //Start selection
      if($page1==2){
          $title=('Testing title 1');
          $description=('Testing description 1'); 
      }  
      elseif($page1==3){
          $title=('Testing title 2');
          $description=('Testing description 2');
      }    
      
      //If not in above range echo error 2
      else{
          echo $page1;
          echo '<br/>Error 2';
      }    
      
      //Register new sessions
      if(isset($title)){ 
          $_SESSION['title']=$title;
          $_SESSION['description']=$description;       
          header("location:about.php");
         echo $title;
      }  
      
    ?>
    
    PHP:
     
    gapz101, Nov 13, 2010 IP
  3. ceylongeek

    ceylongeek Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx for your reply dude. :)

    I tried that then it shows this on the browser

    Error 1-1
    Error 2
    Notice: Undefined variable: title in C:\wamp\www\Kingmax\page_check.php on line 38
    
    Notice: Undefined variable: description in C:\wamp\www\Kingmax\page_check.php on line 39
    HTML:
     
    ceylongeek, Nov 13, 2010 IP
  4. Senix

    Senix Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Anyway, you can disable notices by only enabling some error reports.

    Try adding this on top of your codes. (Somewhere after <?php)

    error_reporting(E_ERROR | E_PARSE);
    Code (markup):
     
    Senix, Nov 14, 2010 IP
  5. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Add somewhere at the beginning of the script: $title = '';$description = '';
     
    jazzcho, Nov 14, 2010 IP
  6. ceylongeek

    ceylongeek Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ya guys it had so many issues I rewrote it with a free mind now it's working.


    Thank you very much for all your responses
     
    ceylongeek, Nov 14, 2010 IP