I was trying to add a function to a helper file to collect extra info from the Db. This got errors as the the document id I am trying to use to call the function is put out as an array and not what I want...
My Model looks as follow:
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();
}
My controller looks as follow:
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');
}
}
I am now not to sure how to add the extra query to the Controller? Or should I add it inside the model itself and how?
I tried to add the function to the controller as follow (Not correct):
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;
}
}
and calling it in the with tLink($adid) but gives me Fatal error: Call to undefined function tLink()
Answer:
Your PHP Error mentioned in comments is correct, either you're:
- Passing through an array instead of a string for
$aid
For example, you're passing througharray( 0 => 45 )
instead of45
. - Attempting to
echo
the results, which is a anarray
/object
. Instead, use Code Igniter'srow()
command, then cherry pick the result you require.
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;
}
Lastly, it's better to load this model within the Controller rather than having to require it at the
helper
(Normally used for the view functions)/**
*
**/
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 );
}
}
Now finally in your view:
<div class="container">
<?php if ( isset( $categories ) && count( $categories ) > 0 ): ?>
<?php foreach( $categories as $category ): ?>
<?=( $category["link"] )?>
<?php endforeach; ?>
<?php endif; ?>
</div>
0 nhận xét:
Đăng nhận xét