Forum Moderators: coopster

Message Too Old, No Replies

Help with simple query string redirection..

         

RalphyB

12:30 pm on May 1, 2003 (gmt 0)

10+ Year Member



Hello, im new to these forums and i've only just started using PHP in my websites.

Im trying to create a simple script that will "include" a differnt .php page, into another .php page based on the query string that is sent.
Im trying to acheive this so i can better organise my site that has several Sections, and Sub Sections.

For example:

[site.com...] - Includes/Requires the main section, (/pages/index_main.php).
[site.com...] - Includes/Requires the "About us" section, (/pages/index_about.php)
[site.com...] - Includes/Requires the "FAQ" section, (/pages/index_faqs.php)
[site.com...] - Includes/Requires the main Downloads section, (/pages/downloads_main.php).
[site.com...] - Includes/Requires the "screensaver downloads" section, (/pages/downloads_screensavers.php).

So far i achived this easily using a simple script, but... heres where the problem is...

[site.com...] - produces no require, so it doesnt include anything inside the main .php page (index.php for example) and the result is a messed up page layout with nothing in it.

Here's the code im using:

**START CODE**
<?
if ($go == 'about') { //if string = "about": load about subsection
require ('/home/jsnweb/public/html/pages/index_aboutus.php');
}
if ($go == 'faq') { //if string = "faq" load faqs subsection
require ('/home/jsnweb/public/html/pages/index_faqs.php');
}
else if ($go == '') { //if string = "BLANK" load MAIN section
require ('/home/jsnweb/public/html/pages/index_main.php');
}
?>
**END CODE**

So as you can see, i either need a way to tell it if the string equals anything other than "about","faq", etc it inserts the main section into the page....

.. or ...

Perhaps another way, even a better way of doing this and acheveing the same simple result?

Any help would be much appreciated, Thanks. =)

Paul in South Africa

12:52 pm on May 1, 2003 (gmt 0)

10+ Year Member



Have you thought about using the switch statement.

switch ($go)
{
case "about":
do something;
break;
case "faq":
do something else;
break;
default:
execute this if no break has been encountered;
}

ShawnR

12:53 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi RalphyB, and welcome to WebmasterWorld!

Instead of

else if ($go == '') { //if string = "BLANK" load MAIN section
require ('/home/jsnweb/public/html/pages/index_main.php');
}

how about:

else { //if string = "BLANK" or anything else load MAIN section
require ('/home/jsnweb/public/html/pages/index_main.php');
}

Shawn

<edit> Oh, I see I am too late </edit>

RalphyB

10:09 pm on May 1, 2003 (gmt 0)

10+ Year Member



Oh i never thought about that!

Such a simple fix!, thanks guys.

RalphyB

10:22 pm on May 1, 2003 (gmt 0)

10+ Year Member



Adding "else" instead of "else if ('do something'); doesnt quite work, it loads both pages if the correct string is passed.

But, Paul_in_South_Africa's idea for using switch has worked like a charm!
Thanks.

daisho

12:17 am on May 2, 2003 (gmt 0)

10+ Year Member



You could also do this:

<?
if( ""==$_REQUEST['go'] ) $_REQUEST['go']='default';
require "/pages/index_${_REQUEST['go']}.php";
?>

That way you just add a new page in the "pages" directory and then send a new "go" parameter that matches.

daisho.

ShawnR

6:32 am on May 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"...Adding "else" instead of "else if ('do something'); doesnt quite work, it loads both pages if the correct string is passed..."

Yes it does. You probably made an error when you edited... You probably added a semicolon somewhere after the 'else', and before the 'require', so it is going: else {do nothing};, and the require is outside the else construct.

But I see that you have solved it using the 'case' construct, so well done for getting it going!

Shawn