Vintage Beef
Vintage Beef

Reputation: 475

Display multiple values without comma

How do I display my Tags values as post tags like this example?

PRODUCT TABLE

+----+--------+--------------------------+
| ID |  Name  |           Tags           |
+----+--------+--------------------------+
|  1 | Shirt  | Shirt,Clothes,Blue-Shirt |
|  2 | Shoes  | Shoes,Red-Shoes          |
|  3 | Jacket | Jacket,Clothes           |
+----+--------+--------------------------+

CODE

<?php
$result= mysql_query("SELECT * FROM product") or die (mysql_error());
while ($rows = mysql_fetch_array ($result))
{ 
?>
    <div class="post">
        Product name : <?php echo $rows['name']; ?>
        <ul class="tags">
           <li>Tagged :</li>
           <li><a href="#"><?php echo $rows['tags']; ?></a></li>
       </ul>
    </div>
<?php } ?>

My code has given this result.

Upvotes: 0

Views: 419

Answers (5)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

If you don't want to use loop simply use implode and explode function like as

echo "<li><a href='#'>".implode("</a></li><li><a href='#'>",explode(",",$rows['tags']))."</a></li>";

Demo

Upvotes: 1

user5866589
user5866589

Reputation:

Just kindly use this elements ..<table> ..<td></td> ..<th></th> ..<li></li> ..</table> try all of this

Upvotes: 0

devpro
devpro

Reputation: 16117

You can use explode() like that:

<?php
$result= mysql_query("SELECT * FROM product") or die (mysql_error());
while ($rows = mysql_fetch_array ($result))
{ 
    $tags = explode(",",$rows['tags']);
?>
    <div class="post">
        Product name : <?php echo $rows['name']; ?>
        <ul class="tags">
           <li>Tagged :</li>
           <?
           foreach ($tags as $key => $value) {
           ?>
              <li><a href="#"><?php echo $value; ?></a></li>
           <?
           }
           ?>
       </ul>
    </div>
<?php 
} 
?>

Explanation:

I have explode $rows['tags'] with commas and making an array of tags and than use foreach loop for print each tags inside the separate <li>

Upvotes: 1

Drudge Rajen
Drudge Rajen

Reputation: 7987

Something like this :

<?php
$result= mysql_query("SELECT * FROM product") or die (mysql_error());
while ($rows = mysql_fetch_array ($result))
{ 
?>
    <div class="post">
        Product name : <?php echo $rows['name']; ?>
       <?php  $tags = explode(",", $rows['tags']);?>
        <ul class="tags">
           <li>Tagged :</li>
           <?php foreach($tags as $tag) :?>
           <li><a href="#"><?php echo $tag ?></a></li>
          <?php endforeach;?>
       </ul>
    </div>
<?php } ?>

Please see documentation here

Upvotes: 1

Amit Visodiya
Amit Visodiya

Reputation: 813

try this:

<div class="post">
    Product name : <?php echo $rows['name']; ?>
    <ul class="tags">
        <li>Tagged :</li>
       <?php $tg = explode(",", $rows['tags']);
        foreach($tg as $t){
          echo '<li><a href="#">'.$t.'</a></li>';
        } ?>
    </ul>
</div>

Upvotes: 5

Related Questions