order by length of field

I have a result set where I want to order the result by the length of field, then display the length as a heading displayed only once.

Here is my query:
SELECT field CHAR_LENGTH(word) as lng FROM table ORDER BY lng ASC";

Here is how i loop
while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)) {            
                  $echo .= '<li >'.$row["word"].'-'.$row["lng"].'</li>';      
            }
            
And my result is:

abc - 3
def - 3
ghi - 3
word -4
word1 - 5

This works fine but here I am stuck. So my question is: how can I loop the data so they are grouped like this:

3
abc
def
ghi

4
word

5
word1
redpencilmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Might be useful to use a PHP script that recognizes the length and the changes.  I don't have time to write it now, but you might start with $old_lng = -1 and upon each change in length, do something like this inside your while() loop...

if ($old_lng != $row['lng'])
{
    echo PHP_EOL . '<br>';
    echo $row['lng'];
    $old_lng = $row['lng'];
}
echo PHP_EOL . $row['field'];

Open in new window

0
hieloCommented:
Try:
$counter=-1;
while ($row = mysqli_fetch_array($result,MYSQL_ASSOC))
{
	$str='<li>';
	
	if( $counter!=$row['lng'] )
	{
		$counter = $row['lng'];
		$str.=$counter.'<br/>';
	}
	$str .= $row['word'].'</li>';
	echo $str;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Or use GROUP_CONCAT in your query.
0
Julian HansenCommented:
Some code that will do what you want
<?php
// EMULATE THE INPUT
$input = array (
  array('word' => 'abc', 'lng' => 3),
  array('word' => 'def', 'lng' => 3),
  array('word' => 'ghi', 'lng' => 3),
  array('word' => 'abc', 'lng' => 3),
  array('word' => 'word', 'lng' => 4),
  array('word' => 'word1', 'lng' => 5),
);

// ARRAY TO STORE OUR OUTPUT TO
$rows = array();
// THIS WOULD BE YOUR 
// while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)) {           
foreach($input as $i) {
  // CREATE COUNT NODE IF IT DOES NOT EXIST
  if (!isset($rows[$i['lng']])) {
    $rows[$i['lng']] = array();
  }
  // ADD ROW TO COUNT NODE
  $rows[$i['lng']][] = $i['word'];
}
// CHECKING TO SEE WHAT WE HAVE
echo "<pre>" . print_r($rows, true) . "</pre>";

// OUTPUT THE RESULT
foreach($rows as $k => $r) {
  echo "{$k}<br/>";
  echo '<ul><li>' . implode('</li><li>', $r) . '</li></ul>';
}
?>

Open in new window

Sample here
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.