0

how would I go about adding a comma after each echo'ed out variable but not after the last one?

At current I display the list like so:

    while ($row = mysqli_fetch_array($query3)) {
        $tag = $row['tag'];  
        $tagformat = eregi_replace("_", " ", $tag);
$blogDisplay .= $tagformat.' ';
        }

This displays: testtag1 testtag2 testtag3

I would like it to display: testtag1, testtag2, testtag3

Any help would be great!

Edit for SQL Query:

$sqlCommand3 = "SELECT tag FROM blogtags WHERE blogid='$blogid'";
        $query3 = mysqli_query($myConnection, $sqlCommand3) or die (mysqli_error());

Also $blogDisplay has other values:

$blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' &#124;  Category: ' . $category . ' &#124;  Tags: ';
    while ($row = mysqli_fetch_array($query3)) {
        $tag = $row['tag'];  
        $tag = str_replace("_", "&nbsp;", $tag);
$blogDisplay .= $tag.' ';
        }
$blogDisplay .= ' &#124; <a href="/blog/'. $blogseourl .'#disqus_thread"></a>';
Newcastlefan
  • 39
  • 3
  • 10
  • 2
    ereg functions are deprecated. Use `preg_replace()` in their stead. See [How can I convert ereg expressions to preg in PHP?](http://stackoverflow.com/q/6270004) – Madara's Ghost Apr 22 '13 at 10:46
  • What's the query that you've used? You can do most of this inside MySQL itself if you're just iterating over tags. – Ja͢ck Apr 22 '13 at 10:50
  • Added the sql query and more information about $blogDisplay – Newcastlefan Apr 22 '13 at 11:03

6 Answers6

2

You could add them to an array, then implode the array later:

$tags = array();
while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", "&nbsp;", $tag);
    $tags[] = $tagformat;
}
echo implode(', ', $tags);

This separates the logic from the output, which wherever possible you should strive for. It'll make your life easier 6 months down the track when you come back to this code!

duellsy
  • 8,497
  • 2
  • 36
  • 60
1
$blogDisplay = "";
while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", "&nbsp;", $tag);
    if ($blogDisplay!="") {
           $blogDisplay.=",";
     }
     if (trim($tagformat)!=""){
         $blogDisplay .= $tagformat;
     }
    }
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
1

Try using implode.

$blogDisplay = array();
while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", "&nbsp;", $tag); 
    $blogDisplay[] = $tagformat;
}

echo implode(', ', $blogDisplay);
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
1

Your code needs some modifications only to achieve mentioned task,

while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", "&nbsp;", $tag);
    $blogDisplay .= $tagformat.', '; //Here is the "," 
    }

Hope it helps.

Hiren Pandya
  • 989
  • 1
  • 7
  • 20
1

I don't see your echo, but I'm going to assume it's outside the while loop (please consider using a foreach loop instead).

Simply put the comma before the space in the eregi_replace.

while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", ",&nbsp;", $tag);
    $blogDisplay .= $tagformat.' ';
}

Or:

while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", "&#44;&nbsp;", $tag);
    $blogDisplay .= $tagformat.' ';
}

Or:

while ($row = mysqli_fetch_array($query3)) {
    $tag = $row['tag'];  
    $tagformat = eregi_replace("_", ", ", $tag);
    $blogDisplay .= $tagformat.' ';
}
obmon
  • 347
  • 2
  • 12
0

If you're only iterating over a set of tags from the database it would be easier to use this query:

SELECT GROUP_CONCAT(`tag` DELIMITER ', ') AS `tag`
FROM tags
WHERE ...

See also: GROUP_CONCAT()

The tags would already be nicely joined together by MySQL.

Also, don't use eregi_replace; in fact, don't use regular expressions:

str_replace('_', '&nbsp;', $tags);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309