我试图将一个函数添加到帮助文件以从Db收集额外的信息。这有错误,因为我试图用来调用函数的文档id作为一个数组而不是我想要的...
我的模型如下:
public function get_categoryads()
{
$this->db->select('ads.id AS id, ads.userid AS userid, ads.adnr AS adnr,
ads.location AS town, search_town.townFileName AS townlink, search_prov.provLabel AS province,
search_prov.subLink AS provlink,text, ad_image.image AS image,addate, r_option, R_rand,
adcat.name AS catname, adcat.clinkname AS clinkname, adsubcat.name AS subcatname, adsubcat.linkname AS subcatlink,
adcat.id AS catid, adsubcat.id AS subcatid, ads.r_option AS r_option, ads.R_rand AS R_rand,
adcat.catcol1 AS catcol1, adcat.catcol2 AS catcol2, search_town.townId,ads.townId');
$this->db->from('ads');
$this->db->join('adsubcat', 'adsubcat.id=ads.subcatid');
$this->db->join('adcat', 'adcat.id=ads.catid');
$this->db->join('search_town', 'search_town.townId=ads.townId');
$this->db->join('search_region', 'search_region.regionId=search_town.relRegionId');
$this->db->join('search_prov', 'search_prov.provId=search_region.relProvId');
$this->db->join('ad_image', 'ad_image.adid=ads.id');
$array = array(
'ad_image.picorder'=> 1,
'ad_image.aproved'=> 1,
'adcat.id' => 9,
'scam' => 0,
'adactive' => 1
);
$this->db->where($array);
$this->db->group_by("ads.id");
$this->db->order_by('addate','desc');
$this->db->limit(5);
$query = $this->db->get();
return $query->result_array();
}
我的控制器如下所示:
class Categories extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('categorylist_model');
$this->load->helper('text');
}
public function index()
{
$this->load->helper('url');
$data = array('sitename'=>'Title', 'page_title' => 'Categories');
$data['categories'] = $this->categorylist_model->get_categoryads();
$this->load->view('templates/header', $data);
$this->load->view('templates/navbar');
$this->load->view('templates/breadcrumbs');
//$this->load->view('templates/category_selector');
$this->load->view('content_categories');
$this->load->view('templates/footer');
}
}
我现在不确定如何将额外的查询添加到Controller?或者我应该在模型本身内添加它以及如何添加?
我尝试将该功能添加到控制器中,如下所示(不正确):
class Categories extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('categorylist_model');
$this->load->helper('text');
}
public function index()
{
$this->load->helper('url');
$data = array('sitename'=>'Title', 'page_title' => 'Categories');
$data['categories'] = $this->categorylist_model->get_categoryads();
$this->load->view('templates/header', $data);
$this->load->view('templates/navbar');
$this->load->view('templates/breadcrumbs');
//$this->load->view('templates/category_selector');
$this->load->view('content_categories');
$this->load->view('templates/footer');
}
public function tLink( $aid ) {
$ci=& get_instance();
$ci->load->database();
$sql ="SELECT adfields.f_value AS fvalue FROM adfields
JOIN field_name ON field_name.id=adfields.f_id
WHERE adid='$aid' GROUP BY adfields.f_id ORDER BY field_name.keyf ASC";
$query = $ci->db->query($sql);
$row = $query->row();
return $row->fvalue;
}
}
并使用tLink($ adid)调用它,但是给了我致命错误:调用未定义的函数tLink()
答案 0 :(得分:0)
评论中提到的PHP错误是正确的,您要么:
通过数组而不是$aid
的字符串
例如,您正在通过array( 0 => 45 )
而不是45
。
尝试echo
结果,array
/ object
。相反,使用Code Igniter的row()
命令,然后选择您需要的结果。
function tLink( $aid ) {
$ci=& get_instance();
$ci->load->database();
$sql ="SELECT adfields.f_value AS fvalue FROM adfields
JOIN field_name ON field_name.id=adfields.f_id
WHERE adid='$aid' GROUP BY adfields.f_id ORDER BY field_name.keyf ASC";
$query = $ci->db->query($sql);
$row = $query->row();
return $row->fvalue;
}
最后,最好在Controller中加载此模型,而不是在helper
(通常用于视图函数)中需要它
/**
*
**/
class Link_model extends CI_Model {
public function getLink( $aid ) {
$sql ="SELECT adfields.f_value AS fvalue FROM adfields
JOIN field_name ON field_name.id=adfields.f_id
WHERE adid='$aid' GROUP BY adfields.f_id ORDER BY
field_name.keyf ASC";
$query = $ci->db->query($sql);
$row = $query->row();
return $row->fvalue;
}
}
/**
*
**/
class Categories extends CI_Controller {
public function index() {
$this->load->model( 'Link_model' );
$categories = array();
$getCategoryIds = $this->categorylist_model->get_categoryads();
foreach( $getCategoryIds as $aid ){
$categories = array(
"id" => $aid,
"link" => $this->link_model->getLink( $aid )
);
}
$data = array(
"categories" => $categories
);
$this->load->view('templates/header', $data );
}
}
现在终于在你看来了:
<div class="container">
<?php if ( isset( $categories ) && count( $categories ) > 0 ): ?>
<?php foreach( $categories as $category ): ?>
<?=( $category["link"] )?>
<?php endforeach; ?>
<?php endif; ?>
</div>