i have model and defined 2 function
1.getMember()
2. getMemberCommunity($member_id)
Controller code
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMember();
$this->load->view('backend/project',$data);
}
View
foreach($query as $row) {
// i want to call my another model function ?
//please give me any solutions ya tips
}
You could do something like this by joining the members table where the member id is equal to the member id in the member community table, I think this is what you want:
Model
public function getMembers(){
$this->db->from('members')
->join('member_community', 'members.id = member_community.member_id', 'left');
$members = $this->db->get();
return $members;
}
Controller
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMembers();
$this->load->view('backend/project',$data);
}
View
foreach($query->result_array() as $row) {
// do whatever in here
}
Related
I am new to codeigniter, so please help me to solve my problem.
I want to call my two model function in one function controller. Heres my two model function:
public function getposts($postid){
$this->db->select('*');
$this->db->from('tbl_endangered');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
//$this->db->join('tbl_images');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
And here is my function controller:
public function viewAnimals($postid){
if(!$this->session->userdata("id") )
return redirect("AuthCon/login");
$this->load->model('Show');
$posts = $this->Show->getSinglePost($postid);
//$posts = $this->Show->getposts($postid);
$this->load->view('Showimage',['posts'=>$posts]);
}
In that code above I always get the first one that I declared. I want to fetch the getposts and getSinglePost inside the function viewAnimals controller.
Hopeyou can help me. Thanks in advance
I think using same variable name is creating a mess.
you can try assigning to posts array with output in it.
$this->load->model('Show');
$posts['getSinglePostData'] = $this->Show->getSinglePost($postid);
$posts['getPostsData'] = $this->Show->getposts($postid);
$this->load->view('Showimage',$posts);
Do like this in the controller
public function viewAnimals($postid){
if(!$this->session->userdata("id"))
return redirect("AuthCon/login");
$this->load->model('Show');
$data['images'] = $this->Show->getSinglePost($postid);
$data['endangered'] = $this->Show->getposts($postid);
$this->load->view('Showimage', $data);
}
Another way is to use join() and only one model function
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
$this->db->join('tbl_endangered', 'tbl_endangered.endangeredid = tbl_images.endangeredid');
$this->db->where('tbl_images.endangeredid', $postid);
return $this->db->get()->result();
}
Sorry for posting such a noob question, but I've had hard times when working with returning a single
row from database and pass it to model. Here's my method from my model:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'");
return $query->first_row('array');
}
Here's an example of my controller with some other returned value from my model:
class MY_Controller extends CI_Controller
{
public $layout;
public $id;
public $data = array();
public function __construct()
{
parent::__construct();
$this->output->nocache();
$this->load->model('subject_model');
$this->load->model('user_model');
$this->load->model('survey_model');
$this->id = $this->session->userdata('user_id');
$this->data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
$this->data['check_if_already_posted_lvis_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('lvis_survey', $this->id);
$this->data['test']= $this->survey_model->test($this->id);
$this->layout = 'layout/dashboard';
}
I can pass all the values from that data array to my view except "test". I've basically tried everything.
CheckIfAlreadyPostedSurvey method will return
number of rows with num_rows and I can easily print the value from them in my view by writing:
<?=$check_if_already_posted_it_survey?>
What should I do to print out that "test" in my view?
Thanks in advance and apologizes...
I probably got the question wrong, but have you tried
echo $this->survey_model->test($this->id);
Try this:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '".$user_id."'");
return $query->row()->test;
}
This will return a single row as an object and then the referenced row name, which in this case is test.
The following will also work.
public function test($user_id)
{
$query = $this->db->query("SELECT * FROM test WHERE user_id = '".$user_id."'");
return count($query->result());
}
You also messed up the quoting in your SQL statement.
How can i pass a parameter (that is a returned value of another model function) to a function of a model class.
here's my model class functions
public function get_products_categories()
{
$query = $this->db->query('SELECT DISTINCT category
FROM products');
return $query->result_array();
}
public function get_products_names($category)
{
$query = $this->db->query('SELECT name
FROM products
WHERE category=$category');
return $query->result_array();
}
and here's how I am doing in the controller class
$this->load->model('products_model');
$data['products_categories'] = $this->products_model->get_products_categories();
$data['products_names'] = $this->products_model->get_products_names();
Now what I want is to pass $products_categories to the function $this->products_model->get_products_names(); but this is not possible $products_categories is not variable here in the controller class but in the view file, it can be used as a variable
Any Ideas ?
What do you want to do? For example, if you want to list all of your products in the categories, you can do this:
$data['products_categories'] = $this->products_model->get_products_categories();
$data['products_names'] = array();
foreach($data['products_categories'] as $product){
$data['products_names'][$product['category']] = $this->products_model->get_products_names($product['category']);
}
Try like:
$this->load->model('main_model');
$data1 = $this->main_model->function1();
//if u wnt to pass data1 to fun2() then
$data2 = $this->main_model->function2($data1);
if the second fun is in another model try to load it and then give as:
$data2 = $this->another_model->function2($data1);
damn sure it wotrks.....
In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>
What I want to do is I want to count the total records from the table called "songs_tbl" from my database. So I wrote this function in controller.
private function getHeaderInfo()
{
$total_songs = $songs->count('distinct songs_tbl.song_id');
$this->mysmarty->assign('total_songs',$total_songs);
}
I got this error
Fatal error: Call to a member function count() on a non-object in
Any suggestion ? Thank you.
With Regards,
I think you are looking for:
$this->db->count_all('songs_tbl');
or if you want the distinct in there you will need to do something like this:
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
As there is/was? an issue with using count_all_results() function and DISTINCT
EDIT
I have never used smarty but based on the code in the question I imagine something like this might work, please correct me if I am wrong:
private function getHeaderInfo()
{
$total_songs = get_all_songs();// This function should be called through a model
$this->mysmarty->assign('total_songs',$total_songs);
}
function get_all_songs(){ //THIS SHOULD BE IN A MODEL
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
Edit 2
My suggested layout would be something along these lines (UNTESTED) using CodeIgniter WITHOUT smarty:
Model Song.php
class Song extends CI_Model {
//Constructor and other functions
function count_all_songs(){
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
}
Controller Songs.php
class Song extends CI_Controller {
//Constructor and other functions
function index(){ //This could be any page
$this->load->model('Song'); //Could be in constructor
$total_songs = $this->Song->count_all_songs();
$this->load->view('songs_index.html', array('total_songs' => $total_songs));
}
}
View songs_index.html
<html><head></head><body>
Total Songs: <?php echo $total_songs ?>
</body></html>
You could query the table and request a count from the table itself, like this:
$result = mysql_query(SELECT count(*) FROM songs_tbl);
Try this
echo $this->db->count_all('songs_tbl');
It permits you to determine the number of rows in a particular table.
you can use this
$query = $this->db->get('distinct');
if($query->num_rows())
{
return $query->num_rows();
}else{
return 0;
}