Count empty columns in rows

I have a property table and it has six columns. Users upload the name of the photo and the images stored in the column.

Now I want to count the number of columns for each empty row.

I can already do this, but the code is too long, I want to write efficient code, is there any way to effectively rewrite the following.

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; } } 
+5
source share
3 answers

You can do this as below: -

 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); } 

Note: - $count1 - the number of non-empty values

+2
source

try this code

 while($data=$select->fetch()): $imagecounter = 0; for($i=1; $i<=6; $i++) if(!empty($data["property_image$i"])) $imagecounter++; echo $imagecounter; endwhile; 
+1
source

Does the column name have its own rule?

assuming this property is 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); } 

code not verified. let me know if it doesn't work

+1
source

Source: https://habr.com/ru/post/1264061/


All Articles