Jump to content

a to z listing


brown2005

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/
Share on other sites

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

    }
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472205
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472222
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472269
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472279
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472286
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/92185-a-to-z-listing/#findComment-472294
Share on other sites

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.