2015-08-16 57 views
1

林与自定义的PHP MVC应用程序的工作如何创建功能和使用自定义的PHP MVC

我检索使用这种方法的帖子列表的变量传递给它:

机型: videos_model.php

class Index_Model extends Model { 
    public function fetchVideos(){  
     $stmt = $this->db->prepare("SELECT * FROM bv_videos"); 
     $stmt->setFetchMode(PDO::FETCH_OBJ); 
     return $stmt->fetchAll(); 

    } 
} 

控制器: video.php 类指数扩展控制器{

public function index(){  

     $this->view->render('../../public/templates/header');  
     $this->view->Videos   = $this->model->fetchVideos();  
     $this->view->render('index/index'); 
     $this->view->render('../../public/templates/footer'); 
    } 
} 

浏览:

<div class="entry-header video_frame_description"> 
    <h3><?php echo $value->title; ?></h3> 

    <p class="pull-left post-info">   
     <span class="cat-links hidden-xs"> 
      <a href="<?php echo URL; ?>?cat=<?php echo $value->cat_id; ?>"> 
      Category: 
      ** HERE I WANT TO ADD CATEGORY NAME UNDER EACH POST ** 
      </a> 
    </p> 
</div> 

在视图中,我想说明的视频的名称类别在每个职位下。

我在指数类

public function get_video_category($cat_id){   

     $stmt = $this->db->prepare("SELECT * FROM bv_categories WHERE cat_id=?"); 
     $stmt->execute(array($cat_id)); 
     $stmt->setFetchMode(PDO::FETCH_OBJ);  
     $data = $stmt->fetch(); 
     return $data->category; 
} 

创造了这个功能,然后在控制器添加此行:

$this->view->video_category = $this->model->get_video_category();  

而且这里的问题。我应该传递什么参数get_video_category()函数?

以下是使用的表结构IM:

bv_videos

CREATE TABLE IF NOT EXISTS bv_videosvideo_id INT(11)NOT NULL AUTO_INCREMENT,
title VARCHAR(255)NOT NULL, url text NOT NULL,
cat_id int(11)NOT NULL,
date_added INT(11)NOT NULL, status INT(11)NOT NULL,
PRIMARY KEY(video_id) )ENGINE = InnoDB的默认字符集= UTF8 AUTO_INCREMENT = 14;

bv_categories

CREATE TABLE IF NOT EXISTS bv_categoriescat_id INT(11)NOT NULL AUTO_INCREMENT, category VARCHAR(100)NOT NULL, order_cat INT(11)NOT NULL DEFAULT主键(cat_id) )ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 6;

回答

1

我想这个查询

"SELECT * FROM bv_videos" 

延伸到

SELECT v.*, c.category as category_name FROM bv_videos v, bv_categories c WHERE v.cat_id = b.cat_id and v.cat_id = " . $cat_id; 

或使用左加入

,然后在视图只是调用类相同的方式,您使用类ID

<a href="<?php echo URL; ?>?cat=<?php echo $value->cat_id; ?>"> 
Category name is: $value->category_name; 

你的道:

$this->view->video_category = $this->model->get_video_category(); 

您需要输入类别ID,这似乎是$值 - > CAT_ID;如果你打电话从视图

+0

非常感谢,它完美的使用左连接:) – Devstar

+0

高兴地帮助你:)感谢您的投票 –

相关问题