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
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
?>

Link to comment
Share on other sites

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++;
   }
?>

Link to comment
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.