Jump to content

Recommended Posts

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>';
}	

?>

Link to comment
https://forums.phpfreaks.com/topic/200832-getting-variables-from-url/
Share on other sites

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>';
}
?>

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

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.