Jump to content

Should I Use Cases of If-Statements?


Levr0x0rz
Go to solution Solved by kicken,

Recommended Posts

Hello,

 

Getting back into web development, I'm finding myself quire removed from what I once remember.  I"ve recently updated the PHP/MySQL association coding with mysqli instead of mysql as I see that is a way things are heading.

 

In a nut shell, I'm going through Lynda.com training videos on PHP, but I'm working on a mock website on the side, where I'm trying to put a fully functioning site together.  I've done it before, but for the life of me my memory is not what it used to be.

 

This "mock site" is a gaming team website (helps keep interest, I enjoy gaming).  I'm looking to make the site off 5 primary pages:

  • index
  • about
  • matches
  • roster
  • news
From there, I want to create the pages dynamically (eg. roster.php?id=10) and it would display content for whomever id 10 is in the roster table.  Easy stuff, I thought.

 

I've looked into using switch statements to do this, but I cannot get the $_GET['id'] to read in the coding, so the URL will pull the proper content.  My structure looks like this:

<?php
$id = $_GET['id'];
switch ($id)
  {
    case 1:
      echo "ID 1";
    break;
    case 2:
      echo "ID 2";
    break;
    default:
      echo "No ID Selected";
  }
?>
There is no MySQL here yet, as I'm trying to create a base to build off (I'm testing this by going to index.php?id=1 for example), and that's where it's not displaying properly.

When visiting index.php?id=1 & ?id=2, the proper content shows.  When I enter something like $id=3, it says No ID Selected, great!  But when I go just to index.php, I receive the following error:

Notice: Undefined index: id in /home/user/public_html/index.php on line 23
No ID Selected
Line 23 is

$id = $_GET['id'];
Is this a syntax problem, where I've just coded this wrong, or a PHP.ini issue?  I'm trying to nail this down before I start inputing the MySQL details and pull from my database.

 

Thank you for the guidance,

Jon

Aspiring Web Developer

@JonEdney1

Edited by Levr0x0rz
Link to comment
Share on other sites

  • Solution

Any of your variables that come from the request ($_GET, $_POST, etc) you need to verify that they exist before trying to use them. Failing to do that will result in that Notice message.

 

$id = isset($_GET['id'])?$_GET['id']:null;
That uses the ternary operator to do a quick inline if/else statement. It checks if that get variable exists, and if so returns it's value. If it does not exist, it returns null.
Link to comment
Share on other sites

I don't recall doing this back about 5-6 years ago, is this something new?

No, errors such as that have always occured. Many places will merely disable their display by adjusting the error_reporting php.ini directive to exclude E_NOTICE level errors. Under such a configuration the error still happens (php reports throws it, runs error handlers, etc) but the default error handler doesn't output any messages about it.

 

For your development work, you should leave your error_reporting set as high as possible (E_ALL for >=5.4, E_ALL&E_STRICT for <5.4) so that you can identify and properly handle such errors.

 

Regarding request variables such as the above, one way to avoid having to constantly check isset() (which gets annoying with large forms) is to access the values via a function such as:

function _GET($n, $d=null){
   return isset($_GET[$n])?$_GET[$n]:$d;
}
Then the previous code could be written as:

$id = _GET('id');
The second parameter is used to specify a default, for instance:

$action = _GET('action', 'list');
if $_GET action was set you'd get that, if not you'd get 'list'.
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.