brown2005 Posted February 20, 2008 Share Posted February 20, 2008 $sql = mysql_query("SELECT * FROM test ORDER by test_name ASC;"); while($array = mysql_fetch_array($sql)) { $text = $array['test_name']; echo"$text<br>"; } i have this code which generates results like this apple andrew bath cabbage carrot rabbits but i want a apple , andrew b bath c cabbage , carrot r rabbits any help please? Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/ Share on other sites More sharing options...
trq Posted February 20, 2008 Share Posted February 20, 2008 <?php if ($result = mysql_query("SELECT test_name FROM test ORDER by test_name ASC;")) { if (mysql_num_rows($result)) { $letter = ''; while ($array = mysql_fetch_array($result)) { if ($letter != substr($array['test_name'],0,1)) { $letter = substr($array['test_name'],0,1); echo "<h1>$letter</h1>"; } echo "<p>" . $array['test_name'] . "</p>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472205 Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 try <?php $sql = "SELECT SUBSTRING(test_name, 1, 1) as initial, GROUP_CONCAT(test_name ORDER BY test_name SEPARATOR ', ') as items FROM test GROUP BY initial"; $res = mysql_query($sql) or die (mysql_error()); while (list($init, $items) = mysql_fetch_row($res)) { echo "<h3>$init</h3>"; echo $items; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472222 Share on other sites More sharing options...
brown2005 Posted February 20, 2008 Author Share Posted February 20, 2008 thanks very much to both of you they are both helpful. Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472240 Share on other sites More sharing options...
brown2005 Posted February 20, 2008 Author Share Posted February 20, 2008 sorry to be a pain but i have just realised what I needed to do.. I am using Barand's code. but i want each item to be <a href='$items'>$items</a> Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472241 Share on other sites More sharing options...
cooldude832 Posted February 20, 2008 Share Posted February 20, 2008 so then write it as that Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472257 Share on other sites More sharing options...
brown2005 Posted February 20, 2008 Author Share Posted February 20, 2008 while(list($init, $items) = mysql_fetch_row($res)) { echo "<h3>$init</h3>"; echo "<a href='$items'>$items</a>"; } ive tried that.. but it doesn't work. puts a link on the whole list for each letter Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472264 Share on other sites More sharing options...
cooldude832 Posted February 20, 2008 Share Posted February 20, 2008 well of course that will cause that is how you wrote it <?php $sql = "SELECT SUBSTRING(test_name, 1, 1) as initial, GROUP_CONCAT(test_name ORDER BY test_name SEPARATOR ', ') as items FROM test GROUP BY initial"; $res = mysql_query($sql) or die (mysql_error()); $letter = ""; echo "<ul>"; while ($row = mysql_fetch_row($res)){ if($row['inital'] != $letter){ if($letter != ''){ echo "</ul></li>"; } echo "<li><h3>$init</h3><ol>"; $letter = $row['inital']; } echo "<li><a href=\"".$row['items']."\">".row['items']."</a></li>\n"; } echo "</li></ul>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472269 Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 <?php mysql_connect ('localhost'); mysql_select_db('test3'); $sql = "SELECT SUBSTRING(test_name, 1, 1) as initial, GROUP_CONCAT('<a href=\"',test_name, '\">', test_name, '</a>' ORDER BY test_name SEPARATOR ', ') as items FROM test GROUP BY initial"; $res = mysql_query($sql) or die (mysql_error()); while (list($init, $items) = mysql_fetch_row($res)) { echo "<h3>$init</h3>"; echo $items; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472279 Share on other sites More sharing options...
brown2005 Posted February 20, 2008 Author Share Posted February 20, 2008 thank you very much.. Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472283 Share on other sites More sharing options...
cooldude832 Posted February 20, 2008 Share Posted February 20, 2008 barand you still aren't giving that heading they want I think. I see you have Group BY initial so each row should have 1 initial with a bunch of items that have said initial right? and then if that is the case how do they get all the results of the row in 1 variable. I see what u saying now the SQL is doing it, my way works as well with php driving it, but you will probably want to select items differently my query should be <?php $q = "SELECT SUBSTRING(test_name, 1, 1) as initial, test_name as items FROM test ORDER BY test_name"; ?> As all I care about is the inital and test_name and not organizing via mysql Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472286 Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 http://dev.mysql.com/doc/refman/4.1/en/group-by-functions.html#function_group-concat Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472291 Share on other sites More sharing options...
cooldude832 Posted February 20, 2008 Share Posted February 20, 2008 that is where mysql always seems to berak down for me, I see what you are doing now. Its returning a single formatted string for each initial. So at most they should be 26+a blank start + 0-9 so 35 different rows. I should get better at mysql, but I see it easier to let php handle the work in case I want to alter them later. so I don't have to query twice or the same data twice in 1 query Quote Link to comment https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472294 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.