PHP Question

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
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:
<?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:

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
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.
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
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
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
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.
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Sorry I am a bit of a noob when it comes to PHP. I got it to work though, thank you!
 

jaygreentree

Member
Messages
148
Reaction score
1
Points
16
There is a much better way to do this:
PHP:
<?
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:
<a href="index.php?id=news">News</a> <a href="index.php?id=about">About</a> <a href="index.php?id=contact">Contact</a>
 

dharmil

New Member
Messages
1,656
Reaction score
0
Points
0
do this
PHP:
//////////////   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";
} 
?>
 
Top