Jump to content

[SOLVED] Sql asc


dizzy1

Recommended Posts

Ive got a list of names b-w and i want to display all the names so it looks like:

 

B

Bob

Bill

Bret

C

Conan

Chris

D

David

Donald

E

Effy

Elisa

F

Frank

......... and so on

 

so far i got it to display all the names in asc like

 

Bob

Bill

Bret

Conan

Chris

David

Donald

Effy

Elisa

Frank

 

by using:

 

$mysql  = " Select Name from  ";								
$mysql .= " Person ";		
$mysql .= " ORDER BY Person ASC  ";

$temp = mysql_query($mysql)or die(mysql_error()); 			
$NumRows = mysql_num_rows($temp); 	


if ($NumRows  != 0 ){						

while($SqlStatement = mysql_fetch_array($temp)){


$PersonName = $SqlStatement['Name'];						


?>
<h2><a href="#"><?=$PersonName ?></a></h2>
<?

}

}


 

 

is it possible to do without having to make a sql call for each letter of the alphabet.

 

 

Thanks in advance

 

Dizzy

 

Link to comment
https://forums.phpfreaks.com/topic/141674-solved-sql-asc/
Share on other sites

Misunderstood what was being asked...

 

Not sure if others do it other ways, but I use array(range(A, Z)) to get all the items... and then loop through selecting and printing the results for each letter like you seem to be doing...

 

I will be interested to see if others know a better way :)

Link to comment
https://forums.phpfreaks.com/topic/141674-solved-sql-asc/#findComment-741626
Share on other sites

$currentLetter = '';
while($SqlStatement = mysql_fetch_array($temp)) {
   $PersonName = $SqlStatement['Name'];
   if ($PersonName{0} != $currentLetter) {
      $currentLetter = $PersonName{0};
      echo '<h3>'.$currentLetter.'</h3>'
   }
   echo $PersonName.'<br />'
}

Link to comment
https://forums.phpfreaks.com/topic/141674-solved-sql-asc/#findComment-741628
Share on other sites

$currentLetter = '';
while($SqlStatement = mysql_fetch_array($temp)) {
   $PersonName = $SqlStatement['Name'];
   if ($PersonName{0} != $currentLetter) {
      $currentLetter = $PersonName{0};
      echo '<h3>'.$currentLetter.'</h3>'
   }
   echo $PersonName.'<br />'
}

 

 

Pardon my french, but bloody brilliant , Cheers.

 

Link to comment
https://forums.phpfreaks.com/topic/141674-solved-sql-asc/#findComment-741702
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.