Jump to content

A to Z list.


brown2005

Recommended Posts

http://www.allinthissite.co.uk/

 

I have done the code to show you the layout I am looking for.

 

What I want to do is take some results from mysql;

 

topics_name

 

and use this to set the layout as seen on the website above. It will only display the letter if there is a result in the table that has that letter, so topics_name first letter sets this header.

 

any help please on the code I would need to achieve this layout.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/157945-a-to-z-list/
Share on other sites

This may take some processing becase of the number of queries needed. I suggest displaying all letters even if there a 0 rows. I would then display 0 results found after a search. However here is how you would do the initial question:

 

<?php
$range = range('a','z');
for($x = 0; $x < count($range); $x++) {
$result = mysql_query("SELECT id FROM table WHERE topics_name LIKE '".$range[$x]."%'");
// destroy letter if no results found
if(!mysql_num_rows($result)) {
	unset($range[$x]);
}
}

// now loop your letters out
foreach($range as $letter) {
print $letter."<br />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/157945-a-to-z-list/#findComment-833136
Share on other sites

26 queries? Sounds nasty. Think i'd go with something like:

 

<?php
$alphabet = range('a','z');
$lettersFound = array[];
$sql = "SELECT SUBSTRING(`field`,0,1) AS firstLetter GROUP BY firstLetter";
$result = mysql_query($sql) or trigger_error(mysql_error());
while($row = mysql_fetch_assoc($result){
$lettersFound[] = $row['firstLetter'];
}

foreach($alphabet as $letter){
if(in_array($letter,$lettersFound){
	//there are fields with this starting letter, so display a link
}else{
	//no fields starting with this letter, just display the letter with no link
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/157945-a-to-z-list/#findComment-833172
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.