Emon Hoque
Emon Hoque

Reputation: 5

PHP if/elseif statement not working as intended

I am trying to make an if/elseif statement work in php and everything seems to work just fine, besides if the "locid" is empty, or not present, i want to display a different query. Could someone help me figure out whats wrong with the code?

Basically, the if(empty($locid)) statement does not work. The page appears as blank.

This is the code:

if (isset($_GET['locid'])) {
    $locid = $_GET['locid'];
    if (empty($locid)) {
        $selectloc = "SELECT * FROM locations;";
    } elseif ($locid == 'all') {
        $selectloc = "SELECT * FROM locations;";
    } elseif ($locid == 'veterinary') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Veterinary';";
    } elseif ($locid == 'store') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Store';";
    } elseif ($locid == 'shelter') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Shelter';";
    } elseif ($locid == 'other') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Other';";
    } else {
        $selectloc = "SELECT * FROM locations;";
    }
}

Upvotes: 0

Views: 61

Answers (3)

yolenoyer
yolenoyer

Reputation: 9465

If the ids differ only by the case, you can greatly simplify the code:

$locids = [ 'veterinary', 'store', 'shelter', 'other' ];
$whereClause = '';

$locid = $_GET['locid'] ?? null;
if (in_array($locid, $locids)) {
    $locationCate = ucfirst($locid);
    $whereClause = "WHERE location_cate='$locationCate'";
}

$selectloc = "SELECT * FROM locations $whereClause;";

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46660

You should enable error reporting.

Basically, the if(empty($locid)) statement does not work. The page appears as blank.

The page appears as blank should be sounding the logical alarm bells.

The problem is that if $_GET['locid'] is not set, then your query is still expecting $selectloc which contains an SQL string not undefined (having no error reporting, means no errors = white/blank page).

// default to this first, let the conditions overwrite it.
$selectloc = "SELECT * FROM locations;";

if (isset($_GET['locid'])) {
    $locid = $_GET['locid'];
    if ($locid == 'veterinary') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Veterinary';";
    } elseif ($locid == 'store') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Store';";
    } elseif ($locid == 'shelter') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Shelter';";
    } elseif ($locid == 'other') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Other';";
    }
}

could also be written like:

<?php

// default to this first, append the constraints
$selectloc = "SELECT * FROM locations WHERE 1=1";

if (isset($_GET['locid'])) {
    $locid = $_GET['locid'];
    
    if ($locid == 'veterinary') {
        $selectloc .= " AND location_cate='Veterinary'";
    } elseif ($locid == 'store') {
        $selectloc .= " AND location_cate='Store'";
    } elseif ($locid == 'shelter') {
        $selectloc .= " AND location_cate='Shelter'";
    } elseif ($locid == 'other') {
        $selectloc .= " AND location_cate='Other'";
    }
}

Upvotes: 3

Martin
Martin

Reputation: 2424

You have a logical flaw in your if else construction.

First you check if the GET variabe is set and enter an if statement if that's the case. However, checking for an empty variable inside that makes no sense as you know when you have entered the statement, the variable won't be empty.

Therefore, you should move your empty logic into an else statement supplementing your isset() as the logic would be that either the variable is set, or it is not (i.e. empty).

<?php
if (isset($_GET['locid'])) {
    $locid = $_GET['locid'];
    if ($locid == 'all') {
        $selectloc = "SELECT * FROM locations;";
    } elseif ($locid == 'veterinary') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Veterinary';";
    } elseif ($locid == 'store') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Store';";
    } elseif ($locid == 'shelter') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Shelter';";
    } elseif ($locid == 'other') {
        $selectloc = "SELECT * FROM locations WHERE location_cate='Other';";
    } else {
        $selectloc = "SELECT * FROM locations;";
    }
} else {
    $selectloc = "SELECT * FROM locations;";
}
?>

Upvotes: 0

Related Questions