Forum Moderators: coopster
$array_letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
I have a mysql database with articles in it and I need an "alphabet index" which outputs me the results according to its starting letter.
Something like this:
<?php
$i = 0;
while($i < 26) {
$letter = $array_letters[$i];
if(mysql_num_rows(mysql_query("SELECT description FROM articles WHERE description LIKE '$letter%'"))!= 0) {?>
<a href="letter.php?letter=<?= $letter;?>"><?= $letter?></a>
<? } $i++;
}
?>
It works fine and only gives me the links to alphabet letters who really have results in the database. However it takes far too long for each array index to be queried, I suppose that's because there are 26 mysql queries to be made before the output starts.
Is there a faster way to do this? Since I have around 100.000 articles in the database the mysql_select checks all of them....
$array_letters = array(
"a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z"
);
$comma_separated = "'" . implode("','", $array_letters) . "'";
$query = "SELECT
DISTINCT(SUBSTRING(description , 1, 1)) AS alpha
FROM articles
WHERE SUBSTRING(description , 1, 1) IN($comma_separated)
ORDER BY alpha"
;