Jump to content


Photo

Counting As You Loop


  • Please log in to reply
3 replies to this topic

#1 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 28 June 2006 - 02:47 AM

Hey guys,

Another question. I need to count the number of loops I have made in while statement as I go through them. Unfortunately I have no idea where to start, just that I want the variable $count to be what it translates to.

The objective is to output the number of the loop as I loop through them. Here is the code...

                        $select_team = "SELECT *";
                        $from_team   = " FROM mc_base";
                        $where_team = " WHERE mc_sponsor_id = '$myid'";
                        $sname_team = mysql_query($select_team . $from_team . $where_team, $dbcnx);

                while ($sname_view_list = mysql_fetch_array($sname_team)) {
                $member_fname = $sname_view_list["mc_fname"];

                                        echo("<tr><td>$count</td>");
                                        echo("<td>$member_fname</td></tr>");
                                        echo("</table>
}

The output would look like an HTML table that would have...

1 John Doe
2 Jane Doe
3 Matt Smith
4 George Bush
Etc

Any advice appreciated.

Rab

#2 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 28 June 2006 - 02:56 AM

                       $select_team = "SELECT *";
                        $from_team   = " FROM mc_base";
                        $where_team = " WHERE mc_sponsor_id = '$myid'";
                        $sname_team = mysql_query($select_team . $from_team . $where_team, $dbcnx);
                               
                $count = 0;
                while ($sname_view_list = mysql_fetch_array($sname_team)) {
                $count++;
                $member_fname = $sname_view_list["mc_fname"];

                                        echo("<tr><td>$count</td>");
                                        echo("<td>$member_fname</td></tr>");
                                        echo("</table>
}


#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 June 2006 - 02:56 AM

$count = 0;
while() {
  // do something
  $count++
}


#4 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 28 June 2006 - 03:12 AM

Perfect! Thank you much, I didn't realize it was that easy.

This was the last piece of a major project. Thanks!

Rab




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users