dongsao8279 2015-10-09 16:31
浏览 72
已采纳

那么如果第二个块在Codeigniter中嵌套了数据库查询,如何在视图上打印条件特定结果?

So I have this existing snippet on custom php.. I am trying to check whether there are existing painting details in the db.. if painting details r found i need to get details of the latest added ones upto count 6 & print detail accordingly. Here is my code->

<?php
    $pData=mysql_query("select * from painting_data");
    if($pData === FALSE) { 
        echo "<div class='well'>No painting available.</div>";
    }
    else{
        $pDet=mysql_query("Select * from painting_data order by padd desc limit 6");
        while($pD=mysql_fetch_assoc($pDet)){
            $pId=$pD['pid'];
            $pTitle=$pD['ptitle'];
            $pImg=$pD['pimg'];
?>
<div class="col-lg-2 col-md-4 col-sm-6 img-recent-container">
    <img role="button" class="img-thumbnail img-responsive img-recent" onclick="location.href='?p=<?php echo $pId; ?>'" src="..<?php echo $pImg;?>" alt="<?php echo $pTitle;?>"/>
</div>  
<?php   }
    } ?>

Now I am new to CI.. & i am confused on how to separate the data retrieval properly. Since the else block has a query in it as well.. I am confused how not to include it in the view.

  • 写回答

2条回答 默认 最新

  • dongza6247 2015-10-09 17:28
    关注

    You should work with DB data in model only and pass returned results to controller. It is MVC paradigm.

    P_m.php (model)

    P_m extends CI_Model
    {
        public $return = [];
    
        public function __construct()
        {
            parent::__construct();
            $this->load->database();//or load it in APPPATH . 'config/autoload.php'
        }
    
        public function p()
        {
            $q = $this->db->query("select * from painting_data");
            $return['error'] = $q->num_rows() > 0 ? FALSE : TRUE;
    
            $q->free_result();
    
            if ($return['error'] === FALSE)
            {
                $q = $this->db->query("Select * from painting_data order by padd desc limit 6");
                $this->return = $q->result();
            }
    
            return $return;
        }
    }
    

    P_c.php (controller):

    P_c extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('p_m');//if used more often, load it in APPPATH . 'config/config.php'
        }
    
        public function index()
        {
            $output_data['data_from_p_m'] = $this->p_m->p();
            $this->load->view('p_v', $output_data);
        }
    }
    

    p_v.php (view):

    if ( $data_from_p_m['error'] === FALSE ) 
    {
        var_dump($data_from_p_m);
    }
    else
    {
        echo "<div class='well'>No painting available.</div>";
    }
    

    You have to declare array before while loop to be able to get all 6 $pId, $ptitle and $pimg.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图