我想选择两列值并使用while,我试过但失败了。新手到php& Mysql ..
Mytable =类别↓
subcat1 | subcat2 | category |
=======================================
High school| Pvt School | Education |
---------------------------------------
Jr College | Voc College | Education |
=======================================
sql="select distinct subcat1, subcat2 from categories where category='Education'";
$category=mysql_query($sql);
<?php
while ($cat = mysql_fetch_assoc($category))
{
?>
<a href="Category.php"> <?php echo $cat['subcat']; ?></a>
高中 私立学校 Jr College Voc学院
请帮帮我..♥衷心的感谢♥
答案 0 :(得分:1)
在循环中回显两个值
<?php
while ($cat = mysql_fetch_assoc($category)) { ?>
<?php if($cat['subcat1']) { ?>
<a href="Category.php"> <?php echo $cat['subcat1']; ?></a>
<?php } if($cat['subcat1']) { ?>
<a href="Category.php"> <?php echo $cat['subcat2']; ?></a>
<?php } ?>
<?php } ?>
答案 1 :(得分:0)
像下面这样的东西 -
while ($cat = mysql_fetch_assoc($category))
{
?>
<?php
if($cat['subcat1']!="") {
?>
<a href="Category.php"> <?php echo $cat['subcat1']; ?></a>
<?php } ?>
<?php
if($cat['subcat2']!="") {
?>
<a href="Category.php"> <?php echo $cat['subcat2']; ?></a>
?php } ?>
<?php } //while close ?>
或最好仅将您的查询修改为
sql="select distinct subcat1, subcat2 from categories
where category='Education' AND subcat1<>'' AND subcat2<>''";