【问题标题】:Selection Condition Array In PHPPHP中的选择条件数组
【发布时间】:2015-03-29 11:06:17
【问题描述】:

我有带循环的 PHP 代码:

$explodeResult=explode(", ",$serviceName);
        foreach($explodeResult as $value)
        {
              $sql = "SELECT id,parent,code,name,xs1 as DOWNLOAD, xs2 as UPLOAD
                       FROM sc_params
                      WHERE rfen = 'SERVICE_REQUESTED'
                      AND code = :param1 
                        OR code IN
                        ( 
                            SELECT code 
                             FROM sc_params 
                             WHERE rfen = 'SERVICE_REQUESTED'
                             AND id = (
                            SELECT parent 
                                 FROM sc_params 
                                 WHERE rfen = 'SERVICE_REQUESTED' 
                                 AND code = :param1 )
                        )
                    ";          
            $stmt = oci_parse($this->_conn,$sql);
            oci_bind_by_name($stmt, ":param1", $value);
            $rs = oci_execute($stmt);
            if (!$rs) {
                $error = oci_error();
                $this->_err = $error;
                return false;
            }

$product    = Array();
$idx        = $idx2 = 0;
    while ($data = oci_fetch_array($stmt, OCI_BOTH)) {
    if($data['PARENT'] == 0) {
        $idx++;
        $product[$idx]['id']        = $data['ID'];
        $product[$idx]['name']      = $data['NAME'];
    }
    else {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 
   }
    print_r ($test_array);

来自查询的数据:

ID NAME      PARENT DOWNLOAD UPLOAD
1  INTERNET   0      null     null 
10 SPEED      1      2048     512
2  VOICE      0      null     null
12 PSTN       2      null     null

数组结果为:

数组 ( [1] => 数组 ( [id] => 1 [名称] => 互联网 [0] => 数组 ( [属性名称] => 下载 [属性值] => 2048 )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 512
                )

        )

    [2] => Array
        (
            [id] => 2
            [name] => VOICE
            [0] => Array
                (
                    [attributeName] => DOWNLOAD
                    [attributeValue] => 
                )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 
                )
        )
)

但我只想为internet 显示attributeNameattributeValue。我试图在while 下面添加if($data['NAME'][0]='INTERNET'),但它不起作用,VOICE 仍然有attributeNameattributeValue。请帮忙。谢谢

【问题讨论】:

  • 您能提供vardump$data 吗?
  • 如果只需要INTERNET,为什么在查询数据库时不设置WHERE条件呢?所以你希望这个循环只返回一个节点?不是 INTERNET 节点数组?只有一个?
  • 查看 if($data['NAME'][0]=='INTERNET') 时我能想到的两件事 1) 你的条件不应该使用 '==' vs '=' 和 2) 你是在比较 $data['NAME'] 而不是 @987654336 @ ?

标签: php arrays if-statement conditional conditional-statements


【解决方案1】:

因此,对于您的数据,您有多种组织方式的可能性:

第一次检查null:

elseif (!($data['DOWNLOAD']==null && $data['UPLOAD']==null)) {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 

第二次检查当前父名:

if($data['PARENT'] == 0) {

    $idx++;
    $product[$idx]['id']        = $data['ID'];
    $product[$idx]['name']      = $data['NAME'];
    $curParent = $data['NAME'];
} elseif ($curParent == 'INTERNET') {

    $product[$idx][0]['attributeName']   = 'DOWNLOAD';
    $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
    $product[$idx][1]['attributeName']   = 'UPLOAD';
    $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
}

和第 3 - 您可以修复您的查询以获取 DOWNLOAD 和 UPLOAD 到 INTERNET 行。如果你真的需要,我可以帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2014-02-01
    相关资源
    最近更新 更多