Forum Moderators: coopster

Message Too Old, No Replies

Do While Loop With Counter Problem

Counter Counts even if statement returns false.

         

Sherif

12:54 pm on Nov 21, 2010 (gmt 0)

10+ Year Member



Hey Guys,

I am making a counter to loop through the results as follows.

Since I already did a do while loop, I used the "mysql_data_seek($apartments,0)" to reset the pointer to the first point.


<?php
$emptycounter=0;

if($totalRows_apartments>0){
//Reset Counter of pointer.
mysql_data_seek($apartments,0);

do{

if(empty($row_apartments['apartment_no'])){

echo $row_apartments['contract_id2'] . "<br />";
$emptycounter++;

}


}while($row_apartments = mysql_fetch_assoc($apartments));
}
if ($emptycounter==0){

echo "No Empty Contracts Available";

}
?>


The problem with this is that the counter counts an additional row that does not exist meaning that it will always output an empty value at the beginning of the loop, and then output the correct values. When i set the mysql_data_seek to 1 instead of 0, the loop outputs an empty value at the beginning, and then the remaining correct outputs missing one of the required outputs.


How can the counter work if the conditional statement is false?

The query that i have only has 2 rows, how can the output/counter show 3. Even if i advance the mysql_data_seek to 1 instead of 0, the counter prints 2 and not 1.

if the Conditional statement in the empty is false, and not empty values were found, the counter returns as 1 which means that the statement was executed although it shouldn't have.

Thank you for your cooperation.

Sincerely,
Sherif

jatar_k

10:10 pm on Nov 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



given that
"do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning."
[php.net...]

then your first iteration doesn't have what it needs, which should only be grabed in the while, after he first iteration

switch it to a plain old while loop and it will be fine