【发布时间】: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 显示attributeName 和attributeValue。我试图在while 下面添加if($data['NAME'][0]='INTERNET'),但它不起作用,VOICE 仍然有attributeName和attributeValue。请帮忙。谢谢
【问题讨论】:
-
您能提供
vardump的$data吗? -
如果只需要INTERNET,为什么在查询数据库时不设置WHERE条件呢?所以你希望这个循环只返回一个节点?不是 INTERNET 节点数组?只有一个?
-
查看
if($data['NAME'][0]=='INTERNET')时我能想到的两件事 1) 你的条件不应该使用 '==' vs '=' 和 2) 你是在比较$data['NAME']而不是 @987654336 @ ?
标签: php arrays if-statement conditional conditional-statements