okay it is resolved...thanks to all...i chose to pursue nedal's suggestion, since I already had the array, and consequently here is the code:
//array is coming from the table by fetch array.
$kkk = array(0,$q1,$q2,$q3,$q4,$q5,$q6,$q7,$q8);
for($i=1; $kkk[$i]!=null; $i++)
some other code
//qtotal represents the number of non empty $q in that row.