1

I've got a query that loops through some product names and puts them down on the page. As part of the loop, it adds a comma to the end, so it looks like this:

Products: Shirts, Pants, Ties, Jackets,

Notice that I'm getting a comma after the last product. Also, they are all links, so I can't use some strreplace fx or similar:

Here's my code:

<?php
 $product_query = mysql_query("select * from products_table);
 $row_product_query = mysql_fetch_assoc($product_query);
 $totalRows_product_query = mysql_num_rows($product_query);
 ?>
 <strong>Products:  </strong>
 <?php if ($totalRows_product_query > 0) {  ?>
 <?php do { ?>
 <span><a href="link"><?php echo $row_product_query['products_name']; ?></a></span>
<strong>,&nbsp;</strong>
 <?php } while ($row_product_query = mysql_fetch_assoc($product_query)); ?>
 <?php } ?><br />

What do I need to do to make that last comma not appear?

Thanks in advance as always.

5
  • 1
    Add it to a variable, and do substr to remove the last comma. Commented Feb 15, 2013 at 19:34
  • 2
    Take a look at the php implode function: Commented Feb 15, 2013 at 19:36
  • Print the first link outside of the loop, then each additional link inside the loop with a comma proceeding it Commented Feb 15, 2013 at 19:37
  • @Tucker, perhaps post that as an answer since it is probably the "correct" solution Commented Feb 15, 2013 at 19:40
  • i think you only need check if is the last row , do not print comma , no need to change all your code , i write my answer Commented Feb 15, 2013 at 20:00

3 Answers 3

2

using the php implode function

<?php
    $str = "";
    $product_query = mysql_query("select * from products_table");
    $row_product_query = mysql_fetch_assoc($product_query);
    $totalRows_product_query = mysql_num_rows($product_query);
    $cnt = 0;
?>
<strong>Products:  </strong>
<?php if ($totalRows_product_query > 0) {  ?>
<?php do { 
    $arr[$cnt] = '<span><a href="link">'.$row_product_query['products_name'].'</a></span>';
    $cnt++;
<?php } while ($row_product_query = mysql_fetch_assoc($product_query)); ?>
<?php } 
    echo implode("<strong>,</strong>",$arr);
?><br />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tucker (and everybody else)
0

Fix how they are being stored/entered into the database? otherwise, <?php echo str_replace(",", "", $row_product_query['products_name']); ?> should work

1 Comment

Commas are added during the loop, they're not stored in DB puts them down on the page. As part of the loop, it adds a comma to the end,
0

As @tucker said, you could use the implode function. Implode Function. You would use it with an array like so

$a_string = implode(",",$the_result_array);

This would give you your desired results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.