AchillesForce Posted October 22, 2009 Share Posted October 22, 2009 Hello, I've been learning PHP for about 3 weeks now and I've came across a problem. I'm fairly new at this PHP stuff, and I'd appreciate some help. What I got: Index.php <?php @$uct = $_GET['uct']; /* gets the variable $uct */ if (!empty($uct)) { $uct .= '.php'; include($uct); } /* if $uct has a value, include it */ else { include('home.php'); } /* otherwise, include the default uct */ ?> Home.php <?php $default = "This is the default code I want to show" $1 = "This is going to be The content if ID = 1" $2 = "This is going to be The content if ID = 2" $3 = "This is going to be The content if ID = 3" $4 = "This is going to be The content if ID = 4" ?> Profiles.php <?php $default = "This is the default code I want to show" $1 = "This is going to be The content if ID = 1" $2 = "This is going to be The content if ID = 2" $3 = "This is going to be The content if ID = 3" $4 = "This is going to be The content if ID = 4" ?> What I would like: I'd like to be able to access the data I want by sorting it in my navigation. Something like index.php?uct=home&id=2 Or even index.php?uct=profiles&id=2 Thanks in advanced!: I appreciate anyone's help or suggestions. If someone could show me how to write out the code so it works how I want it, that'd be great! Once again, thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/ Share on other sites More sharing options...
jonsjava Posted October 22, 2009 Share Posted October 22, 2009 You mean something like this: <?php if (is_int($_GET['id'])){ switch ($_GET['id']){ case 1: $output = "This is going to be The content if ID = 1"; break; case 2: $output = "This is going to be The content if ID = 2"; break; case 3: $output = "This is going to be The content if ID = 3"; break; case 4: $output = "This is going to be The content if ID = 4"; break; default: $output = "This is the default code I want to show"; } echo $output; } else{ exit("invalid ID number"); } ? If this isn't what you want, let me know. If you want to learn about switches, take a look here Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942254 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 @jonsjava: watch using is_int() .. vairables passed via forms and urls are passed as strings, not integers, therefore, is_int() will return false (invalid id number) in the example you provided. better to use is_numeric() (not much of a choice in this matter), and use is_int() if testing integers within your scripts. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942258 Share on other sites More sharing options...
salathe Posted October 22, 2009 Share Posted October 22, 2009 better to use is_numeric() (not much of a choice in this matter), and use is_int() if testing integers within your scripts. Or ctype_digit, or filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 4))) for a slightly (ahem) more verbose option. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942260 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 they all have their uses. one is straight integers, one is string or integer, and one is straight strings. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942265 Share on other sites More sharing options...
AchillesForce Posted October 22, 2009 Author Share Posted October 22, 2009 You mean something like this: <?php if (is_int($_GET['id'])){ switch ($_GET['id']){ case 1: $output = "This is going to be The content if ID = 1"; break; case 2: $output = "This is going to be The content if ID = 2"; break; case 3: $output = "This is going to be The content if ID = 3"; break; case 4: $output = "This is going to be The content if ID = 4"; break; default: $output = "This is the default code I want to show"; } echo $output; } else{ exit("invalid ID number"); } ? If this isn't what you want, let me know. If you want to learn about switches, take a look here Like I have stated before, I'm not too good at PHP, but I can read what you have posted, and I am looking for something like that. But I tried the code you gave me, with the link "index.php?uct=home&id=2" and I still got no luck Any more suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942292 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 switch out this line: if (is_int($_GET['id'])){ with: if (ctype_digit ($_GET['id'])){ Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942295 Share on other sites More sharing options...
AchillesForce Posted October 22, 2009 Author Share Posted October 22, 2009 switch out this line: if (is_int($_GET['id'])){ with: if (ctype_digit ($_GET['id'])){ That worked, but now if you dont have an ID with the page, it'll show an error. I fixed this by adding a @. Check it out <?php if (@ctype_digit ($_GET['id'])){ switch ($_GET['id']){ case 1: $output = "This is going to be The content if ID = 1"; break; case 2: $output = "This is going to be The content if ID = 2"; break; case 3: $output = "This is going to be The content if ID = 3"; break; case 4: $output = "This is going to be The content if ID = 4"; break; default: $output = "This is the default code I want to show"; } echo $output; } else{ echo("invalid ID number"); } ?> Deff worked. Will be using the forum more, so look out for me! Thanks once again for everything! Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942305 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 just lose the initial IF statement and use the 'default:' within the switch(): <?php switch (ctype_digit ($_GET['id'])){ case 1: $output = "This is going to be The content if ID = 1"; break; case 2: $output = "This is going to be The content if ID = 2"; break; case 3: $output = "This is going to be The content if ID = 3"; break; case 4: $output = "This is going to be The content if ID = 4"; break; default: $output = "This is the default code I want to show"; } echo $output; ?> what happens is that the switch() statement matches the condition to the appropriate case within the switch, and if there are no matches, it uses the default. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942308 Share on other sites More sharing options...
AchillesForce Posted October 22, 2009 Author Share Posted October 22, 2009 Even better! Heads up mrMarcus, you'll probably be getting PMed when I need PHP help! haha Thanks a lot bud. Quote Link to comment https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942313 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.