如何将Tags
值显示为example这样的帖子标记?
产品表
+----+--------+--------------------------+
| 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 } ?>
我的代码已经提供了result。
答案 0 :(得分:5)
试试这个:
<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>
答案 1 :(得分:1)
这样的事情:
<?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 } ?>
请参阅文档here
答案 2 :(得分:1)
你可以这样使用explode()
:
<?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
}
?>
<强>解释强>
我用逗号分隔$rows['tags']
并制作一系列标记,然后使用foreach
循环打印单独的<li>
答案 3 :(得分:1)
如果您不想使用循环,只需使用implode
和explode
功能,如
echo "<li><a href='#'>".implode("</a></li><li><a href='#'>",explode(",",$rows['tags']))."</a></li>";
答案 4 :(得分:0)
请善用这些元素
.. <table>
.. <td></td>
.. <th></th>
.. <li></li>
.. </table>
尝试所有这些