简体   繁体   English

PHP if/elseif 语句未按预期工作

[英]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.我正在尝试使 if/elseif 语句在 php 中工作,并且一切似乎都工作得很好,除了如果“locid”为空或不存在,我想显示不同的查询。 Could someone help me figure out whats wrong with the code?有人可以帮我弄清楚代码有什么问题吗?

Basically, the if(empty($locid)) statement does not work.基本上, if(empty($locid)) 语句不起作用。 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;";
    }
}

You should enable error reporting.您应该启用错误报告。

Basically, the if(empty($locid)) statement does not work.基本上, if(empty($locid))语句不起作用。 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).问题是,如果$_GET['locid']未设置,那么您的查询仍然期望$selectloc包含未定义的 SQL 字符串(没有错误报告,意味着没有错误 = 白/空白页)。

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

You have a logical flaw in your if else construction.您的if else构造存在逻辑缺陷。

First you check if the GET variabe is set and enter an if statement if that's the case.首先,您检查是否设置了GET变量,如果是,请输入if语句。 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 (ie empty).因此,您应该将您的空逻辑移动到补充您的isset()的 else 语句中,因为逻辑将是变量已设置或未设置(即空)。

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

If the ids differ only by the case, you can greatly simplify the code:如果 id 仅因大小写而异,则可以大大简化代码:

$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;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM