【问题标题】:multiple if in if with get url多个 if in if with get url
【发布时间】:2015-04-21 10:38:35
【问题描述】:

我想通过 url 从数据库中进行选择,为此我会这样做:

if (isset($_GET['class']) == 'dw')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [dw] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'bk')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [bk] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'fe')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [fe] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'mf')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [mf] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'dl')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [dl] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'sum')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [sum] > 0 order by credits asc");
}
if (isset($_GET['class']) == 'rf')
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [rf] > 0 order by credits asc");
}
else
{
    $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' order by credits asc");
}

网址为:index.php?sy=items&class=dw

但是当我访问这个 url 时 index.php?sy=items&class=dw 是最后选择的 if :( : index.php?sy=items&class=rf

可以帮我解决每个 if 的问题吗?

【问题讨论】:

  • 总是会评估最后一个 else。您可能应该尝试使用 case 语句、elseif 或嵌套 if 结构。

标签: if-statement geturl


【解决方案1】:

在此尝试以这种模式更改代码的格式...

$class=$_GET['class']
switch($class) {
    case 'dw':
        $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [dw] > 0 order by credits asc");
        break;
    case 'bk':
        $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [bk] > 0 order by credits asc");
        break;
    case 'fe':
         $query  = mssql_query("select * from [WebShop] where [category]='".secure($string)."' and [fe] > 0 order by credits asc");
        break;
    default:
    code to be executed if n is different from all labels;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多