Welcome to WebmasterWorld Guest from 54.159.214.27

Forum Moderators: coopster & jatar k

url help

how do I get it to say index.php?do=home:1

   
9:45 pm on May 25, 2008 (gmt 0)

5+ Year Member



Say this is a file named index.php:

<?php
function viewdoSection($do)
{
switch($do) {
case 'home':
$section = 'home';
break;
default:
$section = false;
break;
}
return $section;
}

if(!empty($_GET['do']))
$section = viewdoSection($_GET['do']);

if (isset($section) && $section == 'home') {
print "<p>Print out some home section text.</p>";

} else {
print "<p>Just Information that gets shown if all other do checks fail.</p>";
}

?>

I can get the index.php?do=home to work but then is it possible to go even further so the url might look something like this: index.php?do=home:1 or index.php?do=home:2 or something like that? Cause I'm trying to draw information from the sql database and I have like 3 pages in one already but I want to be able to display different things one each the home:1 and home:2 page. I think this is called urlencode(); but I don't really understand it.

If anyone understands this can you please help me!

9:50 pm on May 25, 2008 (gmt 0)

5+ Year Member



I don't think you can use colons in urls outside of the http:// part, firefox filters it out of the href. Anyway, why don't you just use another variable? Like ?do=home&sub=1?
9:54 pm on May 25, 2008 (gmt 0)

5+ Year Member



how do you do that?

EDIT: examples greatly appreciated

[edited by: PokeTech at 9:55 pm (utc) on May 25, 2008]

10:33 pm on May 25, 2008 (gmt 0)

5+ Year Member



If you had the url given as index.php?do=home&sub=1 you could use the variables $_GET['home'] and $_GET['sub'] in your script.

<?php 

function viewdoSection($do) {
switch ($do) {
case 'home':
$section = 'home';
break;
default:
$section = FALSE;
break;
}
return $section;
}

if (isset($_GET['do']) && !empty($_GET['do'])) {
viewdoSection($_GET['do']);
if ($section == 'home') {
if (isset($_GET['sub']) && !empty($_GET['sub'])) {
switch ($_GET['sub']) {
// handle $_GET['sub'] however you wish
}
} else {
echo('some error message');
}
} else {
echo('some error message');
}
}

?>

I didn't see anything in your code that would handle the other part of the URL, but this should give you a general idea.

If you absolutely need the type of URL your originally asked about, a comma instead of a colon would work very well. I personally prefer slashes for the search-engine-friendliness.

10:57 pm on May 25, 2008 (gmt 0)

5+ Year Member



For some reason I keep getting the "some error message" why is that? I really don't know much about $_GET so thats why I'm asking.
11:41 pm on May 25, 2008 (gmt 0)

5+ Year Member



A stupid mistake on my part. Change "viewdoSection($_GET['do']);" to "$section = viewdoSection($_GET['do']);" If everything works correctly, you should see a blank page (since there is no logic processing $_GET['sub']: all that is there is a comment.

Anyway, that was just an example; it probably won't help you much in the way of getting some working code. $_GET is quite simple. If you give the URL as index.php?foo=bar the variable $_GET['foo'] will be set to "bar" in your script.

For multiple values: index.php?foo=bar&yes=no would set $_GET['foo'] to "bar" and $_GET['yes'] to "no".

You can use a $_GET variable as you would any other variable. Knowing that, you should be able to apply your knowledge of control structures to getting exactly the effect you want.

11:59 am on May 31, 2008 (gmt 0)

5+ Year Member



$do = $_GET['do'];
$do =explode(":",$do);
$do = $do[1];
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month