Do not repeat, inside the while loop

A

Anonymous

Guest
Ok, everythign works. BUT, say rank = 1 I want it to print

President
NAMES HERE

All the names, but if I put
if (rank == 1) {
print $name ect ect
Every time it prints a name it prints President. How can I stop this?
 
what is it you are trying to refer...?

please continue with a same post...
 
Ok, I want all the names to print with the rank 1. But I also want it to say

Rank 1
NAME
NAME
NAME

See how it says Rank 1 only once?
 
Code:
While( $rows = MySQL_fetch_array($result2) ) {
$name = $rows['name'];
$email = $rows['email'];

RANK NAME HERE
print("<a href=\"mailto:{$email}\">$name</a><br>");

}

However if I put the rank name there, it'll repeat the rank name if there's more than 1 record with the same rank. >.<
 
Create a variable called $current_rank or similar. Now when you get the first row (I'm assuming you're sorting by rank here, so all of the records of each rank will be grouped together), assign the rank to $current_rank and print it out for your heading. Now in each row check to see if the rank is equal to $current_rank. If it is, then you don't need to print out another heading, but if it isn't, then you know you're at the next set of ranks, so assign the new rank to $current_rank and print it out. Repeat.
 
Code:
While( $rows = MySQL_fetch_array($result2) ) { 
if ($rank = $rows['rank']) {
  $name = $rows['name'];  
  $email = $rows['email']; 
  RANK NAME HERE  
  print("<a href=\"mailto:{$email}\">$name</a><br>"); 
} else {
 //new rank found
 /.more code
}
$rank = $rows['rank'];
}
[/code]
 
Here's my interpretation:

Code:
<?php
// don't forget to initialize your variables
$last_rank = ''; $this_rank = '';

while($rows = mysql_fetch_array($result2)) {
   $this_rank = $rows['rank'];

   if($last_rank != $this_rank) {
      $last_rank = $this_rank;

      echo $this_rank . "<br />\n";
   }

   echo('<a href="mailto:' . $email '">' . $name . "</a><br />\n"); 
}
?>
 
Woah, ok I understand what each one does. I'll put it to good use. Thank you!
 
Back
Top