Codeigniter passing data from model to controller - php

I am new to CodeIgniter .Can some one please help me by telling how to pass $row['main_products_cat_id']; in model to 'id'=> 'my id here', in controller. I tried so many ways but I failed.
Thanks!!!
This is my model
function cart_product_records($id){
$this->db->from('tbl_sub_products');
$this->db->where('tbl_sub_products.sub_product_id', $id);
$query = $this->db->get();
foreach ($query->result_array() as $row){
echo $row['main_products_cat_id'];
echo $row['sub_product_id'];
echo $row['sub_product_name'];
echo $row['sub_product_description'];
echo $row['sub_product_image'];
echo $row['sub_product_price'];
}
return $query->result();
}
this is my controller
function shopping_cart(){
$id = $this->input->get('id');
if($query = $this->mod_products->cart_product_records($id)){
$data['result'] = array(
'id' => 'my id here',
'qty' => my qty here,
'price' => my price here,
'name' => 'my product name here'
);
}
}

First - you should not echo data in your model - you should really only echo data in your view file.
The way that your data is flowing is first you are passing $id from your controller to your cart_product_records function in the mod_products model. The model then runs a query and gets data from your database. This data is represented by $query->result_array() or $query->result() (you're using both of these but you should only use one, once).
So you then have return $query->result() which sends your data back to your controller. The data will be stored in the $query variable, because you have $query = $this->mod_products->cart_product_records($id).
If I were you I would add an !empty() check to the if statement, so it would look like this:
if( !empty($query = $this->mod_products->cart_product_records($id)) ){
Then, depending on what data you want to get from $query, you're either going to want to construct a loop (which would enable you to extract multiple ids, prices, etc), or you could retrieve specific values from $query, like $query[0][main_products_cat_id] (assuming $query is an array) or $query[0]->main_products_cat_id (if it's an object).
I hope that helps!

Your $query holds the database results object. So, you'll do this in your $data array in your controller.
$data['result'] = array(
'id' => $query->main_products_cat_id,
// carry on...
);

I dosen't make any sense to write
echo $row['main_products_cat_id'];
....
In your loop create array as
foreach ($query->result_array() as $row){
$return_array[] = $row;
}
return $return_array
and in controller, you can access as
$query['your_id'];

Related

Codeigniter DB with slug

I'm in trouble when trying to get DB data passing slug in URL.
public function news($slug = "")
{
$query = $this->db->get_where('news', array("slug", $slug));
$row = $query->row_array();
}
If i try to 'echo' information, e.g echo $row["message"]; I see blank page. =/
What I'm doing wrong?
$query = $this->db->get('news', array("slug", $slug))->row_array();
or
$query = $this->db->where( "slug", $slug)->get('news')->row_array();
var_dump($query);
in my nsh opinion this is not a good idea
public function news($slug = "")
the only way its acceptable to say that the method parameter can be blank is if the method still works without it. which in this case it won't.
anyway lets write this out
// the fields you want
$this->db->select( 'id,and,oh,here,are,some,fields' );
// the where condition
$this->db->where( 'slug', $slug);
// the table name and create the query
$query = $this->db->get( 'news' );
now before sending $query out into the world, make sure it has a record for you by using num_rows()
if ( $query->num_rows() == 1 ) { return $query->row_array(); }
// so you also have the option to check for and return a group of records
// but the rules this time are one article only, so return false
else{ return false ; }
now in your controller, assuming your model is called something like articles, do it something like this
$slug = 'something that has already been validated' ;
// if no article came back, do something else
if( ! $article = $this->articles->news($slug) )
{
// no article came back
// show appropriate view
$this->_showNoArticleFor($slug) ;
}
else{ $this->_showFound($article) ; }
in codeigniter an underscore in the controller method names automatically makes them private so they can't be accessed publicly. in general if a form or search fails - call a separate method that clearly states that something has not been found or its failed. that way you keep your views separate because views change often. otherwise you are piling completely different types of view files inside if conditions and its a mess.

Codeigniter - selecting a database row based on one field

I'm building a codeigniter site and I have a database table of books - one record for each book with title, author, etc etc fields. I know how to get the db table contents and pass an object to the view and then do a foreach loop to get the values. I could print a table of all the data. However I then get a bit muddled. The page has areas for each book and the data for a book will be one row of the data array. So what I want to do is, if the author is 'this' value, find the correct row in which the author appears and then get the other fields. How can I do a foreach loop that finds the one row with the author's name?
I can't think how to do it - I seem to go round and round in circles.
Help!
Edit: Code:
OK so here's the controller method:
public function index()
{
$books = $this->Books_model->getbooks();
$data = array(
'body_id'=>'home',
'main'=>'home_view',
'books'=>$books
);
$this->load->view('templates/template_main_view', $data);
}
and here's the model method:
function getbooks(){
$query = $this->db->get('books');
return $query->result();
}
so I end up with the variable $books (which is of course an object, not an array) in the view and a var_dump() shows that it has all the data. So far so good. I can then use a foreach loop to get values.
Now I want to extract a single row/record conditional on the fact that it has a given value for 'author' and assign each field of that row to a variable that I can use them in the view. And then I want to repeat that for the other rows. I can't seem to work out how to do that.
Afternote:
I found a way of doing this but not sure if it's the best or neatest:
I do this:
if(isset($books)){
foreach($books as $row){
if($row->author == 'authorname'){
$title = $row->title;
}
}
}
it works but seems a bit clumsy/overkill??
How about creating a specific function for getting the book/s that matches the author name?
For example:
In your controller you can do something like this:
public function index()
{
$data['books'] = $this->Books_model->get_each_book();
$data = array(
'body_id'=>'home',
'main'=>'home_view',
'books'=>$books
);
$this->load->view('templates/template_main_view', $data);
}
The in your model, do something like this:
function get_each_book(){
$this->db->where('author','authorname');
$query = $this->db->get('books');
return $query->result_array();
}
This way, you are gonna filter the results in the model already.
Then in your view, access the variable 'books' through a foreach loop:
Example:
foreach($books as $row)
{
$title = $row['title'];
echo $title;
}

How to use "where in" method in codeigniter?

Hello Everybody i have problem with my program i need check multi number in my database but when i test it just show only one result my code :
/*in mt View*/
$data = array(
'name' => 'search_id',
'id' => 'search_id',
'placeholder' => 'numbers_test',
'autofocus' =>"autofocus",
'rows' => '20'
);
echo form_textarea($data,set_value('search_id'));
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
return $this->db->get();
i waiting your help for this problem
If you are getting input as comma separated ids like in string 1,5,4,8 etc from $this->input->post('search_id') then update your code like this
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
// Explode string into array to make where_in clause work
$this->db->where_in('p_id', explode(',', $this->input->post('search_id')));
return $this->db->get();
as official docs suggest you need to provide array of options in IN clause
You have to return the result of the query. Make changes,
/* In my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
$query = $this->db->get();
return $query->result_array(); // You've to return the result of the query
Also as #Saqueib said in comments, try some debugging when in doubt,
echo $this->db->last_query(); exit;
OR
echo '<pre>'; print_r($query->result_array()); exit;

Variables Won't Extract - Controller To View Codeigniter

I'm passing data from my controller:
$data = $this->post->get_post($postID);
$data['errors'];
$this->load->view('register_user.php', $data);
But For some reason, when trying to extract the array in the view, like so:
extract($data);
foreach ($data as $result)
{
echo $result,'<br>';
}
endforeach;
I get $data is null error.
What's the reason for the null array?
A couple things:
'post' is a model which i construct into the class,which pulls a certain row in a certain table that contains all of the details for a specific posts. When trying to echo the array in the controller, it shows.
could inserting a new key and value into the array ('errors'=> 0 ) the cause for the error?
You fetch your data in view like this.
$errors
Whatever you put in $data variable array and you pass $data variable to view, is "converted" in a such way that every element (index) is a variable in view.
So in controller we have $data['news'] = array(); $data['errors'] = array(); But in views we have only 2 variables that we can work with $news and $errors.
please make adjustment to your code as follows
$data['post'] = $this->post->get_post($postID);
var_dump($data['post']);
$this->load->view('register_user.php', $data);
and in view
foreach ($post as $result)...
You can access your data with array keys directly in view
For example:
if
$data = array(
'test1' => 'test1_data',
'test2' => 'test1_data'
);
In this case you can access $test1 directly like
<?php echo $test1; ?>

CodeIgniter going from passing a single row to multiple rows

I'm building a site using CodeIgniter. I have lots of experience using SQL with PHP but having some trouble working through CodeIgniter.
I'm using the following code to set userdata to hold a logo from my database.
Currently I only have 1 logo and it works fine but I need to edit it to handle multiple logos.
$query3 = $this->db->query('SELECT file FROM ohes_flyer_logos');
$row3 = $query3->row();
$data = array( 'userlogo' => $row3->file );
$this->session->set_userdata($data);
Where the Logos are listed...
echo $this->session->userdata('userlogo');
How can I edit this to pass through a full list of logos, and echo them all out?
Calling row() grabs only a single record. You probably want to use result() or result_array() and pass that to the view where you can loop through them. For example:
$query3 = $this->db
->query('SELECT file FROM ohes_flyer_logos')
->result();
$data = array('userlogo' => $query3);
$this->session->set_userdata($data);
Then in your view, you can do this:
foreach ($userlogo as $logo)
{
echo $logo->file; // do your processing here
}

Categories