PHP: Dynamically find how many variables aren't empty -
i have list of 14 variables may or may not empty. let's example have many individual variables holds select ingredient recipe. number of ingredients in recipe differ recipe, obviously. there way, using php, can see how many of 14 variables not empty (i.e. contain ingredient)?
example (using 7 ingredients):
$ing_1 = "carrots" $ing_2 = "apples" $ing_3 = "sugar" $ing_4 = "celery" $ing_5 = "" $ing_6 = "" $ing_7 = ""
how awnser 4
?
further info:
i using mysqli_fetch_array
load variables.
given current variable structure, loop on variable like-
$count=0; for($i=1;$i<=7;$i++){ if(!empty(${"ing_$i"})) $count++; } echo $count;
with $i<=7
increased $i<=14
if have 14 variable/ingredients.
Comments
Post a Comment