Jump to content

Using $GET to load sections.


spookztar

Recommended Posts

Hi guys,

 

I'm having a problem with $_GET coming up empty. I tried searching, but couldn't get to anything with the criteria I used.

 

I have a menu:

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=1">Product Handling</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=2">Misc. Parameters</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=3">Statistics</a><br />

<a href="<?php echo $_SERVER['PHP_SELF'] ?>?sectionid=4">Look 'n' Feel</a>

 

- and when a link is clicked I want the appropriate section of the index.php file to display to the user only using the one file. But when I try to pick up the value with: $sectionid = $_GET['sectionid']; I just get:

 

Notice: Undefined index: sectionid in /home/arioch/public_html/musiccenter.php on line 195

 

What do I need to do pick up the variable in the URL, so I can use it to load the correct section of the file to the user, using only the one page?

 

Bye,

Link to comment
Share on other sites

It looks to me like your error reporting is set very, very strictly....

 

Try doing:

 

$sectionid = (isset($_GET['sectionid']) && is_numeric($_GET['sectionid'])) ? $_GET['sectionid'] : 1;

 

isset doesn't flag an error if you try to check a non existant variable (or key) with it.  This will just check if it's set and numeric, and if it is, it will assign it to $sectionid, but if it's empty, it'll assign the default value of 1 to it.

Link to comment
Share on other sites

no it does matter semicolons are used for the compiling purposes.  It is what tells the complier hey this is the end of this echo or the end of a variable otherwise what it is doing is its using the question marks as a crude if statement i think or classes, just use proper punctuation

Link to comment
Share on other sites

try this

<?php
$links[1] = "Product Handling";
$links[2] =  "Misc. Paramenters";
$links[3] = "Statistics";
$links[4] = "Look \'n\' Feel";
foreach ($links as $key => $value){
echo "<a href=\"".$_SERVER['self']."?sectionid=".$key."\">".$value."</a>";
}
?>
//Page 2
<?php
if(ISSET($_GET['sectionid'])){$pageid = $_GET['sectionid'];}

?>

then you can add more by appending this array

Link to comment
Share on other sites

"are you sure $sectionid is being set before you're calling it?"

 

Isn't that the whole point of: sectionid = $_GET['sectionid'];? I'm not in doubt as to whether it's set or not, but why it isn't.

 

I included the semicolons, didn't do a thing. Strict error reporting obviously didn't care either... ;)

Link to comment
Share on other sites

When set very strictly, the PHP error handling will flag warnings when you try to use not-set variables....

 

<?php

$link = array();
$links[] = "Product Handling";
$links[] =  "Misc. Paramenters";
$links[] = "Statistics";
$links[] = "Look \'n\' Feel";
foreach ($links as $key => $value){
echo "<a href=\"page2.php?sectionid=".$key."\">".$value."</a>";
}
?>

 

Then page2:

 

<?php
$sectionid = (isset($_GET['sectionid'])) ? $_GET['sectionid'] : 0;
echo $sectionid;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.