$_GET and isset issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

$_GET and isset issues

Post by Sephern »

I'm having a small issue with a website I'm coding. It uses $_GET to find the content for the page that's supposed to be displaying. For example, if the URL is http://www.mysite.com?content=index, it would search the database, find the content for index, and display it. This is all working fine.

However, when a user first visits a page, it doesn't have a $_GET variable, since the URL is just the standard domain. To solve this, I made some default page content (which matched the index text), set it as a variable in config.php, and displayed that if there was no value set.

The specific code is

Code: Select all

 
if (isset($_GET['content'])); { //fetches information for content from url
    $page = $_GET['content'];
and

Code: Select all

 
if (isset($content)) { //Actual article content, place in page as appropriate
    echo $content;}
else {
    echo $defaultcontent; }
And a few SQL queries to find the data in the database. If the data doesn't exist, it doesn't set the content variable, and therefore displays the default page.

However, I'd like to have a 404 page. The problem with this is, the variable isn't defined if the database record doesn't exist, but it doesn't exist the first time that the user enters the page either.

I've tried doing an else clause on the $_GET['content'] isset in the first code snippet, but keep getting a syntax error, unexpected T_ELSE. If this works, I could use a mysql_num_rows to determine whether the page exists, and if it does show it, if not show a 404. Since the isset doesn't work, it would display the same as if the user just went to the first page though.

Any ideas? =3

Thanks
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: $_GET and isset issues

Post by Eric! »

Can you post the code you would like to use but generates an error?
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: $_GET and isset issues

Post by Sephern »

I was basically going to define a variable in my config.php, like $text404.

Then do like

Code: Select all

 
if (isset($_GET['content'])){
$page = $_GET['content'];
}
else {
$page = $defaulttext //defined already in config.php;
}
 
Then use $page to carry out mysql_ stuff, and then use num_rows to find out if mysql_num_rows > 0. If it is, then show the page, otherwise show $text404.

I haven't gotten far enough to actually write that yet, since the original else won't work. =[
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: $_GET and isset issues

Post by pickle »

Something like this perhaps (really rough)

Code: Select all

# This will default to 'index' when there's no content defined - no need to duplicate the index content
$target_page = (isset($_GET['content'])) ? $_GET['content'] : 'index';
 
# Have this function return boolean FALSE if no content was found
$content = getContent($target_page);
 
if($content === FALSE)
  echo '404';
else
  echo 'content';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: $_GET and isset issues

Post by Eric! »

Sephern wrote: Then use $page to carry out mysql_ stuff, and then use num_rows to find out if mysql_num_rows > 0. If it is, then show the page, otherwise show $text404.

I haven't gotten far enough to actually write that yet, since the original else won't work. =[
This sounds fine. Why don't you post the part with the error and maybe we can help you continue? Also pickle's line 2 will save you some work.
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: $_GET and isset issues

Post by Sephern »

My errors are literally variables.

It's like, if $_GET isset, and the page exists in the database (isset is true, and num_rows > 0) then show the content

If $_GET isset and the page doesn't exist in the db (isset is true, num_rows < 0) then echo $404

If $_GET isn't set then the default to the index page.

Code: Select all

 
$page = $_GET['content'];
if(isset($page)) {
#mysql nums stuff
}
else {
$page = 'index'; }
Is, in essence, what I need to do, but it doesn't seem to work.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: $_GET and isset issues

Post by pickle »

You can't check if $page is set, because it always will be - you're setting it the line above. What you need to do is check if $_GET['content'] is set - because sometimes it won't be.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: $_GET and isset issues

Post by Sephern »

Sorry, a slight error there. My code checks the $_GET, but still comes up with an unexpected else. >.<
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: $_GET and isset issues

Post by pickle »

We need to see your actual code, without any parts of it commented away. There's no way that little script can fail if it actually does what you've described.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: $_GET and isset issues

Post by Sephern »

Ah, I think I've solved the problem.

Rather than trying to do it at the start of index.php, I moved the mysql statement to the part where the content is dealt with. This meant it checked if the $_GET existed in the database. If not, it set the content variable to 404.

Within the actual index.php, I then went to the part where the content was inserted, and did another if statement.

I basically checked if the $_GET variable was defined, if it was then I echo'd the content variable, otherwise I echo'd default text.
If the record wasn't in the database, $content was the 404 information, so it outputted that.

Thanks for the help. =]
Post Reply