jasonc Posted October 14, 2011 Share Posted October 14, 2011 I have got this far that my code shows the list but say nothing starts with the letter 'y' then all of the 'x' and 'z' are grouped in the list. can anyone suggest a better way to have the list show correctly. the 'multiItemBoxes' css class is a rounded box that shows around each letter group. here is me code. // $letters = array(); $letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "; $L = 1; ?><div class="multiItemBoxes"><? // place first part of DIV. $previousLetter = "/"; // set previousLetter to the letter before '0' zero. used as a dummy string. while($results = mysql_fetch_assoc($getResults)) { if ($previousLetter == "/") { ?><strong style="font-size: 15pt;"><? echo(strtoupper($results['productTitle'][0])); ?></strong><br /><? } if (strtoupper($results['productTitle'][0]) == $letters[$L]) { ?></div><? // this is the ending of the DIV. If needed. ?><br class="clearfloat" /> <? if (strtoupper($results['productTitle'][0]) != " ") { ?><div class="multiItemBoxes"><strong style="font-size: 15pt;"><? echo(strtoupper($results['productTitle'][0])); ?></strong><br /><? $L++; } // this is the extra DIV start if not at end of DB list. } ?><a href="<? echo("cart.php?query=" . $results['Pid']); ?><? echo("&"); ?>i=y"><? echo(htmlspecialchars($results['productTitle'])); ?></a><br /><? $previousLetter = $results['productTitle'][0]; } // end of while ?></div><br class="clearfloat" /><? // Quote Link to comment https://forums.phpfreaks.com/topic/249102-show-alphanumric-list-of-db-entries-fails-if-nothing-starts-with-one-letter/ Share on other sites More sharing options...
Nodral Posted October 14, 2011 Share Posted October 14, 2011 Why not use ORDER BY in your SELECT statement to ensure they come out alphabetically. Then run a test on the 1st letter of the result. If it is the same as the previous one, just echo out the result, if not then echo out the letter to show a new group, then any entries for this group. $sql="SELECT producttitle FROM table ORDER BY producttitle ASC"; $sql=mysql_query($sql); while($row=mysql_fetch_array($sql)){ if(substr($row['producttitle'],0,1)==$letter){ echo $row['producttitle']; $letter=substr($row['producttitle'],0,1); } else { $letter=substr($row['producttitle'],0,1); echo $letter; echo $row['producttitle']; } This way if a letter doesn't exist it will skip it. Quote Link to comment https://forums.phpfreaks.com/topic/249102-show-alphanumric-list-of-db-entries-fails-if-nothing-starts-with-one-letter/#findComment-1279273 Share on other sites More sharing options...
jasonc Posted October 14, 2011 Author Share Posted October 14, 2011 when i used my code if there was nothing starting with a Y then x and z showed in the same section. i did sort alphnumric when doing the query. what i am after is something like this A a1 a2 a3 B b1 b2 b4 D d2 d3 d4 in the above if nothing starts with C then it goes to the next one. but in my code it showed something like this A a1 a2 a3 B b1 b2 b4 d2 d3 d4 joining both B and D what other method could i use instead ? Quote Link to comment https://forums.phpfreaks.com/topic/249102-show-alphanumric-list-of-db-entries-fails-if-nothing-starts-with-one-letter/#findComment-1279283 Share on other sites More sharing options...
Nodral Posted October 14, 2011 Share Posted October 14, 2011 The method described in my post would do this. It would output the first letter as a title then any records which match below it. Have you tried the code I gave? You would need to amend the details in the SELECT statement but it should acheive what you want. Quote Link to comment https://forums.phpfreaks.com/topic/249102-show-alphanumric-list-of-db-entries-fails-if-nothing-starts-with-one-letter/#findComment-1279284 Share on other sites More sharing options...
jasonc Posted October 14, 2011 Author Share Posted October 14, 2011 hey thank for your help on this, I played around with my original codde and added a load of if statements, eventually got it to work after removing the letter array i was using. Quote Link to comment https://forums.phpfreaks.com/topic/249102-show-alphanumric-list-of-db-entries-fails-if-nothing-starts-with-one-letter/#findComment-1279288 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.