Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew McCombs
4,309 PointsPHP Get function
I dont understand why I keep getting redirected to the index.php. Anyone see my error. It should be setting the var, and not redirecting.
This is the information that is being passed into the get.
http://www.mywebsite.com/new_topic.php?id=2&title=Welcome
if (isset($_GET['id']) && $_GET['title']) {
$section_id = $_GET['id'];
$section_title = $_GET['title'];
} else {
header('Location: index.php');
}
?>
1 Answer
thomascawthorn
22,986 PointsCharlie Thomas answer should help with your problem :-) If you use multiple conditionals in an if statement, each individual conditional is totally separate from all the others so you have to rewrite similar checks.
Your current code:
<?php
if (isset($_GET['id']) && $_GET['title']) {
?>
is actually checking if id key is set in the $_GET variable and if the value of $_GET['title'] evaluates to true. Remember that $_GET is just an array and this ?id=2&title=Welcome is something like:
<?php
array (
'id' => '2',
'title' => 'Welcome'
);
?>
BUT, your code should technically work (I know, because I've tried your code myself!). All $_GET values are passed as strings. An empty string returns bool false because it's described as empty. A non-empty string should return bool true.
This means your current conditional should return true if the value for 'title' is not empty. It's similar to isset, but it's always best to use the most appropriate functions. Otherwise anyone with OCD checking your code would simply die.
this url: ?id=2&title=Welcome
and this check: if (isset($_GET['id']) && $_GET['title']) {
should return bool true, and both of your statements should pass. If this isn't working, I would look back a step and make sure all your spelling is correct when you're passing into the url, and you're actually sending values in etc...
Your conditional is also "&&", meaning both statements must pass as true before the if block can be executed. If you're only passing in one variable or only one variable is true, the 'else' block will be executed because only one of the statements is true. If this is the case, you could use an OR or || conditional.
Hope this helps!
Charlie Thomas
40,856 PointsCharlie Thomas
40,856 PointsI think it may be because you are only calling the isset method on your $_GET['id'] and not on t $_GET['title'] so trying changing your code to: