Page 1 of 1

iterating $i++ within db query while loop

Posted: Thu Aug 06, 2009 7:31 pm
by johnd
Hi All,

I'm attempting to use a for statement to dynamically change <div> and <a> id and class names via based on the number of records returned from a query. I'm running into a problem and they are not iterating, both are remaining at 0. Thank you in advance!

For statement:
$limit = mysql_num_rows($result);
if (mysql_num_rows($result) >= '1')
{
for ($i = 0; $i <= $limit; $i++)


Here are the id and class I'm trying to dynamically change.
<a id='link$i'
<div class=\"div$i programDesc\">

both are remaining through every iteration:
<a id='link0'
<div class="div0 programDesc">


Code below:

Code: Select all

 
Query left out...
 
 
$result=mysql_query($query);
echo mysql_error();
 
$color="1";
//begin iterations
$limit = mysql_num_rows($result); 
if (mysql_num_rows($result) >= '1')
    {
        for ($i = 0; $i <= $limit; $i++)
//start db query while loop
while ($row = mysql_fetch_assoc($result))
            { 
    
$title = $row['title'];
$node = $row['nid'];
$shortDesc = $row['field_short_desc_value'];
$college = $row['field_college_value'];
        
if($color==1){
        echo"</tr>";
        echo"<tr class='even' onMouseOver='this.style.backgroundColor='#e0ddda'' onMouseOut='this.style.backgroundColor='''>";
        echo"<td><a id='link$i' href='#' title='Click for program details'>$title</a></td>";
        echo"<td>$college</td>";
        echo"</tr>";
        echo"<tr><td>
        <div class=\"div$i programDesc\">
        <p>title: $title </p>
        <p>short desc: $shortDesc</p>
        </div>
        </td></tr>";
 
 // Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
 
        echo"<tr class='odd' onMouseOver='this.style.backgroundColor='#e0ddda'' onMouseOut='this.style.backgroundColor='''>";
        echo"<td><a id='link$i' class='basic' href='#' title='Click for program details'>$title</a></td>";
        echo" <td>$college</td>";
        echo"</tr>";
        echo"<tr><td>
        <div class=\"div$i programDesc\">
        <p>title: $title </p>
        <p>short desc: $shortDesc</p>
        </div>
        </td></tr>";
        // Set $color back to 1
$color="1";
} //close else
} // close for 
} // close while
    
?>
        </tr>
    </tbody>
</table>
</div>
 
 
 

Re: iterating $i++ within db query while loop

Posted: Thu Aug 06, 2009 8:32 pm
by redmonkey
You are iterating over your database result set within your while loop, although the while loop is nested within your for loop your entire result set is iterated over in the first pass of the for loop. Use while or for there is no need to use both in this case.

Re: iterating $i++ within db query while loop

Posted: Thu Aug 06, 2009 8:54 pm
by aceconcepts
You must remember that while and for are both used for conditional looping. These are the fundamentals and redmonkey is correct in saying use one out of the two.

What you are trying to do is rather straightforward and like JD says, "the gratification comes in the doing, not in the results" - James Dean.

Re: iterating $i++ within db query while loop

Posted: Thu Aug 06, 2009 10:54 pm
by johnd
Thank you both, I fixed it with the following. Cheers!

Code: Select all

 
 
$i = 0; 
 
while($row = mysql_fetch_array($result))
{
if (mysql_num_rows($result) >= '1')
{
$i++;