...

View Full Version : Resolved mysql_fetch_assoc() && $i<10 (for loop)



djh101
07-17-2011, 02:28 AM
for($i=0; $utopicArray=mysql_fetch_assoc($utopicQuery) && $i<10; $i++){
echo '<li><a href="/?utopic='.$utopicArray['id'].'>'.$utopicArray['topic'].'</a></li>\n';
}

I want to echo the content of the array but only for 10 rows. The problem is that the $utopic[] variables don't output anything with && $i<10 in the statement. Why is this and how might I go about fixing it?

abduraooft
07-17-2011, 03:50 PM
for($i=0; $utopicArray=mysql_fetch_assoc($utopicQuery) and $i<10; $i++){
echo '<li><a href="/?utopic='.$utopicArray['id'].'>'.$utopicArray['topic'].'</a></li>\n';
}
should work.

In your case, the row returned by the function mysql_fetch_assoc() will get ANDed with the result of the immediate comparison operation. At the end, $utopicArray will get a true/false. Check the order of precedence at http://php.net/manual/en/language.operators.precedence.php

PS: That's why we always use or die() and not || die()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum