SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use if else to control which page to load up.

    I am new to php. I want to use if elseif statement to control which page to load depends on what was enter in the field name state. I am having trouble with the code. please help. thanks.

    Example. if state == 'ny' go to ny.htm, elsif state='nj' go to nj.htm

    please help. thanks.

  2. #2
    SitePoint Addict psyon's Avatar
    Join Date
    May 2004
    Location
    Cedar Rapids
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I would just do
    Code:
    $state_file = $state . '.html';
    if(file_exists($state_file)) {
      include($state_file);
    } else {
      include('default.html');  
    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, thank you for quick reply. I want to check what is enter in the form field name state the load the apporiate page. I am not sure if the code provided above will do that. Thanks.

  4. #4
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    there are a couple of ways you can do this, but I think the most effecient method would be as such:

    PHP Code:
      // We setup the default value for state
     
    $l_state null;

      
    $l_pages = array();
       
    $l_pages['nj'] = 'nj.html';
       
    $l_pages['ny'] = 'ny.html';
       
    // Continue with however many states you have
      
       // Check that the value of state is set
       // You would call the page with page.php?state=ny or similar
      
    if ( isset($_GET['state']) )
      {
         
    $l_state $_GET['state'];
       }

      
    // If state wasn't passed, or the state could not be found in the pages array
      // then we go to the default page.
      
    if ( $l_state == null || ! in_array($l_state$l_pages) )
      {
         require (
    'default.html');

      
    // Otherwise we include the page for that state which is stored in the $l_pages hash.
      
    } else
      {
         require (
    $l_pages[$l_state]);
       } 
    When you say 'states' I presume you mean new york, florida etc. because in that case it would be a pain to have a pre defined list of states defined and it would be much easier just to look on the file system etc.

    So you could take another approach to it like such:
    PHP Code:
      $l_state null;
      
    $l_state_default 'default';

       if ( isset(
    $_GET['state']) )
       {
          
    $l_state strtolower($_GET['state']);
       }

       
    // If the state was passed in the query string we check
       // to see if it's a two character string containing only a-z
       
    if ( $l_state != null && ! ereg('^[a-z]{2}$'$l_state) )
       {
           
    // Check if the state exists with a '.htm' extension
           
    if ( ! file_exists($l_state '.htm') )
           {
                
    // And if it doesn't we set the state to default
                
    $l_state $l_state_default;
           }
       } else
       {
          
    $l_state $l_state_default;
       }
     
       
    // And finally include the page
       
    require ($l_state '.htm'); 
    Hope i've given you enough pointers to be on your way, but just ask if you need a clearer explanation.

    Regards,
    Harry

  5. #5
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much. I will try the code. You guys are awesome.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •