Dat Posted February 12, 2009 Share Posted February 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/ Share on other sites More sharing options...
drisate Posted February 12, 2009 Share Posted February 12, 2009 Something like $alpha = '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'; $alpha = explode ('.', $alpha); foreach ($alpha as $letter){ $query = "SELECT * FROM entry WHERE title like '$letter%'"; [...] } Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/#findComment-760207 Share on other sites More sharing options...
Dat Posted February 12, 2009 Author Share Posted February 12, 2009 I found the solution thanks anyway. http://www.phpfreaks.com/forums/index.php/topic,218512.0.html Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/#findComment-760209 Share on other sites More sharing options...
Dat Posted February 12, 2009 Author Share Posted February 12, 2009 Oh I found a problem with this solution I have data entries that start with numbers and other stuff like _ I want to order these extra stuff under # Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/#findComment-760213 Share on other sites More sharing options...
.josh Posted February 12, 2009 Share Posted February 12, 2009 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/#findComment-760232 Share on other sites More sharing options...
sasa Posted February 12, 2009 Share Posted February 12, 2009 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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144866-alphabet-list-with-data-entries/#findComment-760246 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.