iterating $i++ within db query while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnd
Forum Newbie
Posts: 2
Joined: Thu Aug 06, 2009 7:08 pm

iterating $i++ within db query while loop

Post 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>
 
 
 
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

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

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post 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.
johnd
Forum Newbie
Posts: 2
Joined: Thu Aug 06, 2009 7:08 pm

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

Post 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++;
 
 
Post Reply