我有我的PHP代码:从SQL查询之后...
$pieData = array();
}
while($row = mysqli_fetch_array($result))
{
$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#222222');
现在这有效,当我通过json_encode()
时是成功的.我想动态更改颜色。
从我的数据库中$result我访问$row['Delaytype']
并希望做这样的事情。
while($row = mysqli_fetch_array($result)){
if($row['Delaytype'] === "engineering"){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#222222');
}elseif($row['Delaytype'] === "something else"){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#888888');
}
代码有效,但它接受第一个 true 语句并继续,我所有的颜色都具有相同的值,我生成的饼图也是如此。我已经尝试了一个switch语句,如果并倾向于遍历数组,但我对此的理解是有限的。
任何帮助真的非常感谢。
该表称为延迟:
It is like
Delaytype | Delayhours
engineering | 3
engineering | 2
human error | 4
该 SQL 查询:
"SELECT Delaytype, SUM('Delayhours') as cnt FROM delays GROUP BY
Delaytype;"
If I echo a table with
<tr><td>$row[delaytype]</td><td>$row[cnt]</td></tr> //forgive formatting
我将从上表中得到:
engineering 5
human error 4
strcasecmp也不起作用。
如果我这样做 if(1=1) 它可以工作,所以它与比较有关,但它是正确的,输出是小写工程,但在测试时看不到它?
我现在有这个工作。奇怪的是,我所做的只是将"人为错误"改为"人为错误",将"工程"改为"工程"。我查看了我的 html 输入值,没有发现前导空格?不知道为什么它适用于空间。我将搜索我的代码是有原因的。感谢您的帮助和其他建议。strcasecmp 既有效,也无需工作。我标记的遮阳篷正确,因为您所说的一切都有效... + 它会显示遮阳篷,并且更有可能因类似问题而被查看。谢谢你的帮助
使用==
而不是===
来使用比较条件,因为后面的比较和匹配数据类型,所以尝试这种方式,并查看此处以获取更多信息 http://php.net/manual/en/language.operators.comparison.php。
while($row = mysqli_fetch_array($result))
{
if($row['Delaytype'] == "engineering"){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' =>
'#222222');
}elseif($row['Delaytype'] == "human error"){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' =>
'#888888');
}
其他方式:
while($row = mysqli_fetch_array($result))
{
if(strcasecmp($row['Delaytype'],"engineering")==0){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' =>
'#222222');
}elseif(strcasecmp($row['Delaytype'],"human error")==0){
$pieData[] = array('value' =>(int)$row['cnt'], 'color' =>
'#888888');
}
注意:此函数返回的字段名称区分大小写。
循环的一个稍微健壮的版本是:
if ($row['Delaytype'] === 'engineering') {
$color = '#222222';
} elseif ($row['Delaytype'] === 'human error') {
$color = '#888888';
} else {
$color = '#ffffff';
}
$pieData[] = array(
'value' => (int)$row['cnt'],
'color' => $color,
);
这将检查样本中的每个值,但也有一个回退值,以防它不匹配。它将$pieData
赋值移出该循环,这样您就不会在那里复制代码。