Jump to content

Alphabet List with data entries


Dat

Recommended Posts

I have an array retrieved from a database called say $row

$query = "SELECT * FROM entry WHERE type='$type' ORDER BY title ASC";

 

 

I have a loop set up already but Im stuck:

while($row = mysql_fetch_array($result))

{

echo $row['title'];

}

 

and I know how to make an alphabet

 

PHP Code:

<?php        for($i = 97; $i < 97 + 26; $i++) {

            $letter[] = chr($i);

        }

        foreach($letter as $value)

        {

            ?>

            <li id="<?php echo $value; ?>">

            </li>

            <?php

        }?>

How do I make an alphabet with the title names to match with their respected letters?

 

Like a list

 

A

apple

ankle

B

battle

bait

burn

C

cookie

 

and so on.

Link to comment
https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/
Share on other sites

Seems a bit bloated, but, here's my off the top of my head take

 

<?php
// example data...list and sorting would come from query
$list = array('apple','angry','banana','carrot','fickle','8track','_blah','wtf','first','9ball','?something');
sort($list);

// example loop...should use while loop for db data
// foreach item in the list...	 
foreach($list as $item) {
   // get first char and capitalize
   $currentItem = strtoupper(substr($item,0,1));
   // if first letter is not alpha...
   if (preg_match('~[^a-z]~i',$currentItem)) {
      // if we haven't made # header...
      if ($nonAlpha == false) {
         // echo header
         echo "#<br />";
         // set header bool to true
         $nonAlpha = true;
      } // end if to make #
   // else, if first letter alpha...
   } else {
      // if current first letter is not the same as previous one...
      if ($currentItem != $prevItem)
         // echo header
         echo "$currentItem<br />";
   } // end else Alpha start
   // assign current first char to prevItem
   $prevItem = strtoupper(substr($item,0,1));
   // echo current item
   echo "$item<br />";
} // end foreach item
?>

or

<?php
// example data...list and sorting would come from query
$list = array('apple','angry','banana','carrot','fickle','8track','_blah','wtf','first','9ball','?something');
sort($list);

// example loop...should use while loop for db data
// foreach item in the list...
$letter = 'A'; 
echo "#<br />\n";
foreach($list as $item) {
   // get first char and capitalize
   $currentItem = strtoupper(substr($item,0,1));
   while ($letter <= $currentItem and $currentItem <= 'Z'){
   	    echo $letter, "<br />\n";
   	    $letter++;
   }
   echo " - $item<br />\n";
} // end foreach item
while (strlen($letter) < 2){
   	    echo $letter, "<br />\n";
   	    $letter++;
   }
?>

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.