Loop question.

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
User avatar
Goober
Forum Newbie
Posts: 9
Joined: Wed Jun 25, 2003 10:29 pm
Location: Little Elm, Texas

Loop question.

Post by Goober »

Hello.

I queried a database, received certain desired values. If there is an email address entry in a row, I echo it in such a manner:

Code: Select all

for( $count = 1; $all_rows = mysql_fetch_row( $get_all ); ++$count )
	  {
       if( $all_rowsї3] )
		     {
		   echo $all_rowsї3] . ", ";
          }
     }
Does anyone have any theories as to how I can code this loop so that if it's on the last entry, it won't include the comma in the echo?

Thanks,
Goober
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Code: Select all

<?php
 $comma = '';
 while ($row =  mysql_fetch_row($resultpointer))
 {
    if ($row[3])
    {
       echo $comma . $row[3];
       if (!$comma) $comma = ', ';
    }
 }
?>
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

1) try not to put the mysql_fetch_row in the "for". assign that value to a variable to use it in the "for" statement.

2) check mysql_num_rows
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post by PaTTeR »

Code: Select all

$records_count = mysql_num_rows($get_all)
$i = 0;
while ($row = mysql_fetch_array($get_all)) {
    $i++;
     if ($row[3]) {
         echo $row[3];
         echo ($record_count == $i) ? '' : ',  ';
     }
}
hmmm this should work corectly only if you have an e-mail seted in last row.

there is other idea :lol:

Code: Select all

<?php
while ($row = mysql_fetch_array($get_all)) {
    if ($row[3]) {
        $result_str .= $row[3].', ';
    }
}

echo substr($result_str, 0, strlen($result_str) - 2);
?>
Last edited by PaTTeR on Tue Jul 22, 2003 2:05 pm, edited 1 time in total.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post by kettle_drum »

Umm you could always do a query before hand to get the number of results that it will give you. And then use this data to know what the last result is, and then do an if result == last { dont print comma}

You could also maybe also print the comma before you print the result and have a counter that is incremented each time you post the data from the database, and then only print the comma if the counter <> 0.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

No point vasting resources on extra function calls and variables, as you can see my "solution" only uses one variable and no extra function calls..
User avatar
Goober
Forum Newbie
Posts: 9
Joined: Wed Jun 25, 2003 10:29 pm
Location: Little Elm, Texas

Post by Goober »

Thanks.
Post Reply