【发布时间】:2014-07-31 17:06:43
【问题描述】:
我有这个代码 - 这不是我做的 :s 并且没有时间替换已弃用的调用。
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
$db=mysql_connect('localhost','carros_mil1','K-Jw)ureB.}M');
mysql_select_db('carros_mil0',$db);
$nombreListModelo = 'modelo_'.str_replace(" ","_",strtolower($_REQUEST['nombreMarca']));
$sql = "SELECT DISTINCT (identificador) AS id
FROM modelos
WHERE nombre = '".$nombreListModelo."'";
$rs=mysql_query($sql);
$i=0;
while ($row = mysql_fetch_array($rs)) {
$vectorModelo[$i] = $row['id'];
$i++;
}
$sql = "SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = '".$nombreListModelo."'";
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);
$id1 = $row['attribute_id'];
$sql= "SELECT eao.option_id AS option_id
FROM eav_attribute_option eao, eav_attribute_option_value eaov
WHERE eao.option_id=eaov.option_id AND attribute_id = ".$id1." ORDER BY eaov.value";
$rs=mysql_query($sql);
$retorno = array();
$bandera = true;
while ($row = mysql_fetch_array($rs)) {
$bandera = false;
$sql2= "SELECT value, option_id
FROM eav_attribute_option_value
WHERE option_id = ".$row['option_id']."
ORDER BY value";
$rs2=mysql_query($sql2);
$row2= mysql_fetch_array($rs2);
if ($_REQUEST['bandera']=='vende') {
if (!is_null($row2['value'])) $retorno[$row2['value']] = $row2['value'];
} else {
$existe=false;
for($k=0;$k<count($vectorModelo);$k++) {
if($row2['option_id'] == $vectorModelo[$k]) {
$existe=true;
break;
}
}
if($existe) {
if($_REQUEST['tipo']=='contactenos')
if (!is_null($row2['value'])) $retorno[$row2['value']] = $row2['value'];
else
if (!is_null($row2['option_id'])) $retorno[$row2['option_id']] = $row2['value'];
}
}
}
if($bandera)
$retorno[''] = 'Todos';
header('Content-Type: application/json');
//echo json_encode($retorno);
foreach($retorno as $k => $v) {
printf("%s => %s\n", $k, $v);
}
print_r($retorno);
echo json_encode($retorno);
由于我注意到生成的 json 中有一些奇怪的地方,我添加了 2 种不同的方式来打印数组的内容。
JSON 方式为我添加了一个 null:null 条目。奇怪的情况是我每次添加元素时都会检查!is_null($row2['value'])。
当我在某些参数下点击 url 时,我得到:
Elantra => Elantra
Getz => Getz
H1 => H1
i-10 => i-10
New Accent => New Accent
Santa Fé => Santa Fé
Tucson => Tucson
Array
(
[Elantra] => Elantra
[Getz] => Getz
[H1] => H1
[i-10] => i-10
[New Accent] => New Accent
[Santa Fé] => Santa Fé
[Tucson] => Tucson
)
{"Elantra":"Elantra","Getz":"Getz","H1":"H1","i-10":"i-10","New Accent":"New Accent",null:null,"Tucson":"Tucson"}
前 6 行对应于自定义打印 foreach 循环。还有一个 print_r 调用,它只显示 6 个元素。第三种形式——我真正需要的形式——显示一个空值。
那么:为什么$retorno 在我事先检查空条件时接受空条目? 考虑此脚本已满 - 这里没有丢失代码。
【问题讨论】:
-
您能否调查一下减少此脚本 - 如果您删除与问题无关的代码,答案会容易得多。
-
显示具有指定输出的脚本。
-
这是脚本。这是完整的脚本。最重要的部分是相关的——循环。
-
圣达菲是您的答案。与UTF编码有关。前一周我遇到了这个问题。stackoverflow.com/questions/21688653/…
-
老兄! :D 你(@Hammerstein)应该在你的每月支票中加薪。不幸的是,我不是你的老板:p。把它作为一个答案。