search

Home  >  Q&A  >  body text

php如何判断数据库字段内容

这个是模块代码

        if($article_info['grouptime']){
    $otherTitle = $this->model('publish')->getall("article","grouptime='".$article_info['grouptime']."' AND grouptime!=''");
}else{
    $otherTitle =  array();
}

我想通过php中的if语句来判断,当当前文章的grouptime这个字段值的相同数据大于1时就显示
这个代码

<p class="detail-mod J_floor">
          <p class="mod-title sp"><span></span>相关百科</p><ul class="extend-ctn">
                    <?php foreach ($this->otherTitle as $key => $v): ?>
                    
                <?php if($_GET['id']!=$v['id']){ ?>
                 <li><a href="/article/<?php echo $v['id']; ?>" title="<?php echo $v['title']; ?>"><?php echo $v['xtitle']; ?></a></li>
                <?php } ?><?php endforeach ?></ul>
            
        </p>

,否则就显示【没有百科】,要怎么写if 跟 else呢?

阿神阿神2778 days ago654

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-11 09:21:43

    select * from table group by grouptime having count(grouptime) > 1

    这样可以查出数据库中grouptime重复数大于1的数据

    php代码

    $muysqli = new mysqli($host,$user,$pwd,$dbname);
    $sql = "select * from table group by grouptime having count(grouptime) > 1";
    $res = $mysqli->query($sql);
    if(count($res) > 1){//grouptime数量大于1
        //do something
    }else{//grouptime不大于1
        //do something
    }

    ==============================================================

    在第二段代码前面加上if判断

    <?php
    if(count($this->otherTitle) > 1){
    ?>
    <p class="detail-mod J_floor">
        <p class="mod-title sp"><span></span>相关百科</p>
            <ul class="extend-ctn">
                <?php foreach ($this->otherTitle as $key => $v): ?>
                <?php if($_GET['id']!=$v['id']){ ?>
                 <li><a href="/article/<?php echo $v['id']; ?>" title="<?php echo $v['title']; ?>"><?php echo $v['xtitle']; ?></a></li>
                <?php }
                 endforeach 
                 ?>
         </ul>
    </p>
    <?php  
    }else{
        echo "【没有百科】";
    }
    ?>

    reply
    0
  • PHP中文网

    PHP中文网2017-04-11 09:21:43

    $res = $db->query("select count(id) sum,grouptime from table group by grouptime having sum > 1");
    if($res){
    
    } else {
     
    }

    reply
    0
  • Cancelreply