Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.