+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP Question

  1. #1
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    PHP Question

    Ok, I need some help on a little bit of PHP code.

    I got the code so that I can create the ids for each page e.g. index.php?id=1

    How would I go about modifying the code so I could get: index.php?id=1&category=2

    or something along those lines?

    The code I have for the current index page is as follows:

    PHP Code:
    <?php 


    if( isset($HTTP_GET_VARS['id']) || isset($HTTP_POST_VARS['id']) )
    {
        
    $id = ( isset($HTTP_GET_VARS['id']) ) ? $HTTP_GET_VARS['id'] : $HTTP_POST_VARS['id'];
        
    $id htmlspecialchars($id);
    }
    else 
    {
        
    $id "";
    }

    switch( 
    $id )
        {   
                
    // ?id=1          This leads to section 1   
             
    case "1":
            include(
    "inc/1.php");
           break;
            
                
    // ?id=2           This leads to section 2
             
    case "2":
            include(
    "inc/2.php");
           break;    
           
           
    // The default page to load, when website is started.
           
    default:
           include(
    "inc/main.php"); 
           break;


    }

    ?>
    obviously I have changed it to match the scenario above, just to make it simpler.

    How would I go about changing this code so that it would allow: index.php?id=1&category=2

    Your help is very muchly appreciated!

    Regards,
    Zenax
    Last edited by Zenax; 11-09-2006 at 08:25 AM.
    Regards,
    Zenax

  2. #2
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: PHP Question

    Just a little note: $HTTP_GET_VARS = $_GET

    just add the code $_GET["category"] (or $HTTP_GET_VARS["category"] ) where you wanna get category number. It can be freely put in included files, because included files source is added to php code when parsing.
    This post has been marked spam 52 times.


  3. #3
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP Question

    Hmm I am slightly confused? do I just repeat the code at the top at the bottom of the page, but change it to the different name?

    Totally confused
    Regards,
    Zenax

  4. #4
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: PHP Question

    actually, you can put code (about category's) inside the included files, because all variables in first code (index.php, etc...) are also in included files.

    example for a news system:

    index.php?id=news&category=5 is opened by browser / user
    index.php finds id, and so knows what page to include
    index.php includes news.php
    news.php looks for category
    news.php finds category because category's number was defined in URL
    news.php loads news from category...
    and etc....

    Point of this: If you have file included, included files can use same resources as the file that includes.

    Don't ask me to explain again, because i will paint it then.
    This post has been marked spam 52 times.


  5. #5
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP Question

    Sorry I am a bit of a noob when it comes to PHP. I got it to work though, thank you!
    Regards,
    Zenax

  6. #6
    jaygreentree is offline x10 Sophmore jaygreentree is an unknown quantity at this point
    Join Date
    Sep 2006
    Posts
    144

    Re: PHP Question

    There is a much better way to do this:
    PHP Code:
    <?
    if (isset($_GET['id']) && file_exists('./pages/'.$_GET[id].'.php')) {
    include 
    './pages/'.$_GET[id].'.php';
    } else {
    include(
    "pages/About.php");
    }
    ?>
    All of your files except for the index.php would go in the folder pages which on the server is /public_html/pages/

    and your links would be
    HTML Code:
    <a href="index.php?id=news">News</a> <a href="index.php?id=about">About</a> <a href="index.php?id=contact">Contact</a>

  7. #7
    dharmil's Avatar
    dharmil is offline x10 Elder dharmil is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    Avenel New Jersey
    Posts
    828

    Re: PHP Question

    do this
    PHP Code:
    //////////////   index.php
    <?php
    if( isset($HTTP_GET_VARS['id']) || isset($HTTP_POST_VARS['id']) )
    {
        
    $id = ( isset($HTTP_GET_VARS['id']) ) ? $HTTP_GET_VARS['id'] : $HTTP_POST_VARS['id'];
        
    $id htmlspecialchars($id);
    }
    else 
    {
        
    $id "";
    }

    switch( 
    $id )
        {   
                
    // ?id=1          This leads to section 1   
             
    case "1":
            include(
    "inc/1.php");
           break;
            
                
    // ?id=2           This leads to section 2
             
    case "2":
            include(
    "inc/2.php");
           break;    
           
           
    // The default page to load, when website is started.
           
    default:
           include(
    "inc/main.php"); 
           break;


    }
    ?>

    ////////////////    1.php
    <?php
    if (isset($_GET['category']) && file_exists('./category1/'.$_GET[category].'.php')) {
    include 
    './category1/'.$_GET[category].'.php';
    } else {
    echo 
    "select category";

    ?>
    ////////////////    2.php
    <?php
    if (isset($_GET['category']) && file_exists('./category2/'.$_GET[category].'.php')) {
    include 
    './category2/'.$_GET[category].'.php';
    } else {
    echo 
    "select category";

    ?>

+ Reply to Thread

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  4. Qucik Question on PHP
    By Zenax in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 10-18-2006, 07:47 AM
  5. http access and php read/write permissions.... one question !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 8
    Last Post: 06-18-2006, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers