papillonstudios Posted May 5, 2010 Share Posted May 5, 2010 I have a site that passes variables through the url so the site can pull up the right page. My url looks like this index.php?page=products and when i go there it displays a page that says products when i go to index.php?page=products&id=1 it displays a page that says monitors but if i go here index.php?page=products&id=2 or any other number it keeps going to the page that says monitors what am i doing wrong <?php if (isset($_GET['id']) == 1) { echo '<h3>Monitors</h3>'; } elseif (isset($_GET['id']) == 2) { echo '<h3>Desktops</h3>'; } elseif (isset($_GET['id']) == 3) { echo '<h3>Notebooks</h3>'; } elseif (isset($_GET['id']) == 4) { echo '<h3>Netbooks</h3>'; } elseif (!isset($_GET['id'])) { echo '<h3>Products</h3>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200832-getting-variables-from-url/ Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 Wrong use of isset is your problem. Using isset checks if the variable has been set, not what is actualy in it. you're close, just tweek it to this and you should be fine <?php if (isset($_GET['id'])) { if ($_GET[id] == 1){ echo '<h3>Monitors</h3>'; } elseif ($_GET['id'] == 2) { echo '<h3>Desktops</h3>'; } elseif ($_GET['id'] == 3) { echo '<h3>Notebooks</h3>'; } elseif ($_GET['id'] == 4) { echo '<h3>Netbooks</h3>'; } } else { echo '<h3>Products</h3>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200832-getting-variables-from-url/#findComment-1053790 Share on other sites More sharing options...
papillonstudios Posted May 5, 2010 Author Share Posted May 5, 2010 i thought it would be something simple like that. thanx man Quote Link to comment https://forums.phpfreaks.com/topic/200832-getting-variables-from-url/#findComment-1053791 Share on other sites More sharing options...
kenrbnsn Posted May 5, 2010 Share Posted May 5, 2010 Your "if" statement is incorrect. It should be <?php if (isset($_GET['id']) && $_GET['id'] == 1) { echo '<h3>Monitors</h3>'; } elseif (isset($_GET['id']) && $_GET['id'] == 2) { echo '<h3>Desktops</h3>'; } elseif (isset($_GET['id']) && $_GET['id'] == 3) { echo '<h3>Notebooks</h3>'; } elseif (isset($_GET['id']) && $_GET['id'] == 4) { echo '<h3>Netbooks</h3>'; } elseif (!isset($_GET['id'])) { echo '<h3>Products</h3>'; } ?> A better way to do this (IMHO) would be to either use an array to store what is to be printed (good way to check for errors) or to use a switch statement: Using an array: <?php $val_id = array('Products','Monitors','Desktops','Notebooks','Netbooks'); $id = (isset($_GET['id']) && $_GET['id'] > 0 && $_GET['id'] < 5)?$_GET['id']:0; echo '<h3>' . $val_id[$id] . '</h3>'; ?> Using a switch: <?php $out = 'Products'; if (isset($_GET['id'])) { switch($_GET['id']) { case 1: $out = 'Monitors'; break; case 2: $out = 'Desktops'; break; case 3: $out = 'Notebooks'; break; case 4: $out = 'Netbooks'; break; default: $out = 'Products'; } } echo '<h3>' . $out . '</h3>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/200832-getting-variables-from-url/#findComment-1053794 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.