5

I'm using below code to display Image & Name of webisites form database.

<fieldset>  
    <h1>A</h1>          
    <ul>
        <?php foreach ($records as $key) { ?>
        <li class="siteli"> <a href="#" class="add">        
            <div id="site-icon"><img src="<?php echo $key->site_img; ?>" width="16" height=""></div>
            <p id="text-site"> <?php echo $key->site_name; ?></p>
        </li>
        <?php } ?>
    </ul>
</fieldset>

Now I'm trying to group this results alphabetically by adding A, B, C etc as title.

Example,

A    
Amazon     
Aol    
Aol Mail

B    
Bing     
Bogger
j0k
  • 22,600
  • 28
  • 79
  • 90
Vin_fugen
  • 581
  • 3
  • 19
  • 38

3 Answers3

8

You can use array sorting to sort the array. In your case I would choose sort()

Now to show the links with a preceding Letter I would use:

<?php
$records = ['Aaaaa', 'Aaaa2', 'bbb', 'bbb2', 'Dddd'];
$lastChar = '';
sort($records, SORT_STRING | SORT_FLAG_CASE); //the flags are needed. Without the `Ddd` will come before `bbb`.
//Available from version 5.4. If you have an earlier version (4+) you can try natcasesort()

foreach($records as $val) {
  $char = $val[0]; //first char

  if ($char !== $lastChar) {
    if ($lastChar !== '')
      echo '<br>';

    echo strtoupper($char).'<br>'; //print A / B / C etc
    $lastChar = $char;
  }

 echo $val.'<br>';
}
?>

This will output something like

A
Aaaa2
Aaaaa

B
bbb
bbb2

D
Dddd

Notice that the C is missing, because there are no words starting with C.

Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • It was just a proof of concept. It also has no other formatting :) But updated it to add the extra BR – Hugo Delsing Jan 08 '13 at 08:08
  • @HugoDelsing I have implemented your code. Just one minor thing. I have some brands that have a lowercase letter at the beginning of the first word and it is putting those into their own group instead of adding it to the existing group? – Nick Darley Sep 16 '13 at 17:15
  • @NickDarley: If you use the natural sort order as explained with the flags, then the same lower and upper case charactors are mixed in the results. If you want them to show as a single header charactor, all you need to do is change `$char = $val[0];` to `$char = strtolower($val[0]);`. – Hugo Delsing Sep 16 '13 at 20:24
3

This is a modification of @Hugo Delsing's answer.

I needed to be able to group my brands together like this, but one bug in the code was the fact that if I had say iForce Nutrition and Inner Armour it would group these into two separate groups because of the letter case.

Changes I Made
Because it was fully alphabetical, it was only going by first letter I changes SORT_STRING to SORT_NATURAL

Changed: if ($char !== $lastChar) to if (strtolower($char) !== strtolower($lastChar))

<?php
$records = ['FinaFlex', 'iForce Nutrition', 'Inner Armour', 'Dymatize', 'Ultimate    Nutrition'];
$lastChar = '';
sort($records, SORT_NATURAL | SORT_FLAG_CASE); //Allows for a full comparison and will alphabetize correctly.

foreach($records as $val) {
 $char = $val[0]; //first char

  if (strtolower($char) !== strtolower($lastChar)) {
    if ($lastChar !== '')
     echo '<br>';

     echo strtoupper($char).'<br>'; //print A / B / C etc
     $lastChar = $char;
    }

    echo $val.'<br>';
  }
?>

The final output will be like this

D
Dymatize

F
FinaFlex

I
iForce Nutrition
Inner Armour

U
Ultimate Nutrition

I hope this helps everyone, and a big thank you to Hugo for providing the code that got me exactly where I needed to be.

You can see my example here https://hyperionsupps.com/brand-index

Nick Darley
  • 341
  • 1
  • 3
  • 17
0
$records = ['FinaFlex', 'iForce Nutrition', 'Inner Armour', 'Dymatize', 'Ultimate    Nutrition'];

    $temp=array();    
    $first_char="";
    for($i=0;$i<count($records);$i++)
    {
        $first_char= strtoupper ($records[$i][0]);             
             if(!in_array($first_char, $temp))
             {
                 echo strtoupper($first_char).'<br>'; //print A / B / C etc                      

             }
             $temp[]=  $first_char;
            echo $records[$i]."<br>";
    }
vaibhav
  • 331
  • 1
  • 4
  • 15