need help: if statment that changes content based on page

Discussion in 'PHP' started by saturn100, Apr 2, 2011.

  1. #1
    Hi
    I wondering if its posible to create an if statement that will change HTML content based on the page. That it will show one thing for the index page and one thing for other pages

    eg

    if page =="index" (html code)
    else
    (other html)

    thanks
     
    saturn100, Apr 2, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    say the following is a file named index.php
    
    <?php
    $page=$_GET['p'];
    if($page==1){
    ?>
    [first page's html...]
    <?php
    }
    elseif($page==2){
    ?>
    [second page's html...]
    <?php
    }
    elseif($page==3){
    ?>
    [third page's html...]
    <?php
    }
    .
    .
    .
    elseif($page==99){
    ?>
    [last page's html...]
    <?php
    }
    else{
    ?>
    [index's html...]
    <?php
    }
    ?>
    
    PHP:
    then you can access each page using the following format:
    http://domain.com/index.php?p=1
    http://domain.com/index.php?p=2
    ...
    http://domain.com/index.php?p=99

    obviously you need to change the [html for page X] part with the content you want to show for each page.

    also it's worth mentioning that you're not limited to numbers for the page names. but if you want to use something else you must put it in " " when placing it in the if statement:
    
    if($page="firstpage"){
    ...
    }
    
    PHP:
    also it's better if you don't use space or any character like &, # and such and stick to the alphabet, numbers and use "_" to separator words if needed:

    http://domain.com/index.php?p=firstpage
    or
    http://domain.com/index.php?p=first_page
     
    Sepehr, Apr 2, 2011 IP
  3. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #3
    Ok thanks
    I just need it show one of two pieces of code
    one for the index and one for all the other pages

    so what do i call the index page

    <?php
    $page=$_GET['p'];
    if($page==1){
    ?>
    [first page's html...]
    <?php
    PHP:
    can I just say

    if($page==index.php){
    PHP:
    or

    i
    f($page==domainname.com){
    PHP:
     
    saturn100, Apr 2, 2011 IP
  4. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    the last part is the index:

    
    else{
    ?>
    [index's html...]
    <?php
    }
    ?>
    
    PHP:
    say you have 3 pages (index+2) and you want to name them, this is how:

    
    <?php
    $page=$_GET['p'];
    if($page=="second_page"){
    ?>
    [second_page's html...]
    <?php
    }
    elseif($page=="contact"){
    ?>
    [contact page's html...]
    <?php
    }
    else{
    ?>
    [index's html...]
    <?php
    }
    ?>
    
    PHP:
    these are accessible through the following URLs:

    index:
    domain.com
    OR
    domain.com/index.php

    second_page:
    domain.com/index.php?p=second_page
    OR
    domain.com/?p=second_page

    contact:
    domain.com/index.php?p=contact
    OR
    domain.com/?p=contact
     
    Sepehr, Apr 2, 2011 IP
  5. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #5
    Ok
    I get that I think
    Just have to integrate it with what I have
     
    saturn100, Apr 2, 2011 IP