Search code examples
phpmysqlstringalphabetized

Save loop result to a string


Is it possible to save loop results to a string?

$sql = "SELECT SUBSTR(a.`title`, 1,1) FROM articles a WHERE a.`tag` = 'human_resources'";
$results = db_query($sql);
  while ($fields = db_fetch_array($results)) {
     foreach($fields as $key => $value) {
       echo $value;
     }
  }

The code above outputs titles of articles with a tag human_resources. I want to make an alphabar for a catalog, so I'm using this:

if(stripos($string, "A") !== false) {
    echo ('<a href="http://www.mysite.com/articles/A">A</a>');
}
else echo '<span class="inactive">A</span>';

if(stripos($string, "B") !== false) {
    echo ('<a href="http://www.mysite.com/articles/B">B</a>');
}
else echo '<span class="inactive">B</span>';

...etc

But I don't know how to get that $string from the loop for the second part of code.

Any suggestions or better approach to this problem are greatly appreciated.


Solution

  • Change this:

    echo $value;
    

    to this:

    $string .= $value;