Jump to content

[SOLVED] Displaying first letter and only once


9three

Recommended Posts

Hey,

 

I'm running this:

 

while($row = db_fetch_array($results))
{
  $tempLetter = substr($row['products_description'], 0, 1);
  echo '<a href="' . link_url(PAGENAME_PRODUCTS_DESCRIP, 'part_number=' . $row['part_number']) . '">'.$tempLetter.'</a>';
}
echo '</div>';

 

What this does is pull the information from my DB and then I grab the first letter from each product description.

 

What I'm trying to do is pull only the first letter AND display it ONCE.

 

For example if the information pulled three letter Bs and 10 letter As, it should only display each letter once.

Can someone lend a hand please?

$used = Array();
while($row = db_fetch_array($results))
{
  $tempLetter = substr($row['products_description'], 0, 1);
  if(!in_array($tempLetter, $used))
  {
    $used[] = $tempLetter;
    echo '<a href="' . link_url(PAGENAME_PRODUCTS_DESCRIP, 'part_number=' . $row['part_number']) . '">'.$tempLetter.'</a>';
  }
}
echo '</div>';

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.