计算行中的空列
Counting Empty Columns in Rows
我有一个 属性 table,它有六列。用户上传的照片和图片名称存储在该列中。
现在我想计算每一行的空列数。
我已经可以做到了,但是代码看起来太长了,我想写出高效的代码,有没有办法高效地重写下面的代码。
while($data=$select->fetch()){
$imagecounter=0;
if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "" && $data['property_image6'] !== "") {
echo $imagecounter=6;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "") {
echo $imagecounter=5;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "") {
echo $imagecounter=4;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "") {
echo $imagecounter=3;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "") {
echo $imagecounter=2;
} else if ($data['property_image1'] !== "") {
echo $imagecounter=1;
}
}
你可以像下面那样做:-
while($data=$select->fetch()){
$data1 = array($data['property_image1'],$data['property_image2'],$data['property_image3'],$data['property_image4'],$data['property_image5'],$data['property_image6']);
$count = count($data1); // count of original array
$count1 = count(array_filter($data1)); // remove empty indexes and count the values
echo "empty columns number is :-".($count-$count1);
}
注意:- $count1
是 non-empty 个值的计数
试试这个代码
while($data=$select->fetch()):
$imagecounter = 0;
for($i=1; $i<=6; $i++)
if(!empty($data["property_image$i"]))
$imagecounter++;
echo $imagecounter;
endwhile;
列名有规律吗?
假设是 'property_image{number}'
while($row=$result->fetch()) {
$count = 0;
for($i=0; $i<6; $i++) {
if($row['property_image'.$i]==NULL)
$count++;
}
echo "empty columns number is :-".($count);
}
代码未经测试。
如果它不起作用请告诉我
我有一个 属性 table,它有六列。用户上传的照片和图片名称存储在该列中。
现在我想计算每一行的空列数。
我已经可以做到了,但是代码看起来太长了,我想写出高效的代码,有没有办法高效地重写下面的代码。
while($data=$select->fetch()){
$imagecounter=0;
if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "" && $data['property_image6'] !== "") {
echo $imagecounter=6;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "") {
echo $imagecounter=5;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "") {
echo $imagecounter=4;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "") {
echo $imagecounter=3;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "") {
echo $imagecounter=2;
} else if ($data['property_image1'] !== "") {
echo $imagecounter=1;
}
}
你可以像下面那样做:-
while($data=$select->fetch()){
$data1 = array($data['property_image1'],$data['property_image2'],$data['property_image3'],$data['property_image4'],$data['property_image5'],$data['property_image6']);
$count = count($data1); // count of original array
$count1 = count(array_filter($data1)); // remove empty indexes and count the values
echo "empty columns number is :-".($count-$count1);
}
注意:- $count1
是 non-empty 个值的计数
试试这个代码
while($data=$select->fetch()):
$imagecounter = 0;
for($i=1; $i<=6; $i++)
if(!empty($data["property_image$i"]))
$imagecounter++;
echo $imagecounter;
endwhile;
列名有规律吗?
假设是 'property_image{number}'
while($row=$result->fetch()) {
$count = 0;
for($i=0; $i<6; $i++) {
if($row['property_image'.$i]==NULL)
$count++;
}
echo "empty columns number is :-".($count);
}
代码未经测试。 如果它不起作用请告诉我