Variables Won't Extract - Controller To View Codeigniter - php

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; ?>

Related

PHP - iterate trough arrays and set different keys

I have wrote PHP function in my Symfony project returnAllPosts() which returns all entity objects from table that are found.
I want to assign new array structure with different keys and put those values of every specific post after that and return new array result with predefined post key-value pairs.
But this specific code returns just first object not all of them? How I can get them all from foreach loop?
$posts = $this->myService->returnAllPosts();
foreach ($posts as $post) {
$post = [
'valueOne' => $post->getValueOne(),
'valueTwo' => $post->getValueTwo(),
];
$posts[] = $post;
}
return $posts;

How to view an arrays result inside controller in codeigniter

I am stuck in getting a value from model and process it in controller. I am new to codeigniter. My model code is this:
public function display_cat_courses($value)
{
$this->db->select('*');
$this->db->from('courses_tbl');
$this->db->join('course_category_tbl', 'courses_tbl.course_category_id = course_category_tbl.id');
$this->db->where('course_category_id', $value);
$query = $this->db->get();
return $query->result();
}
And my controller is this:
public function view()
{
$this->load->model('Category_model');
$cat_id = $this->uri->segment(3);
$data['category']=$this->Category_model->display_cat_courses($cat_id);
}
I just want to get a column value from course_category_tbl and store that inside an array as array element to process that later.Model works fine but in my controller I want to store "course_type" which is a column in course_category_tbl. How to get it.
As you are succesfully getting all datas in array format from model in controller and you only want one field data from array that you need to use for some purpose.For that,you can make use of
$field_values_array = array_column($whole_array, 'field_name');
print_r($field_values_array );
From this code,you will get all values of that column field in array Format,and later you can pass it to view for further proccessing.
You can select required column only:
$this->db->select('course_category_tbl.course_type');
If there is only one row that you'll get by passing the category ID then in the model in place of $query = $this->db->get(); you can add put
$this->db->row_array() and this will give you a one dimensional array and then from the result $query you can access the desired elemnt by passign the index.
E.g
$query['course_type'] will give you course type

How to grab a value from multi dimensional array

I want to grab one field of database from an array that contains all the data fields here is m code:
public function show($id)
{
$data['product'] = $this->products_model->get_product_by_id($id);
foreach ($data['product'] as $row) {
$seller = $row['seller_id'];
}
$data['seller_name'] = $this->members_model->get_members_by_id($seller);
if (empty($data['product']))
{
show_404();
}
$data['main_content'] = 'pages/show';
$this->load->view('templates/template', $data);
}
I think the code is self explanatory what I want is to grab the seller_id field from the products database to use it to grab seller_name field from members database. The code that I thinks it's not working is this part:
foreach ($data['product'] as $row) {
$seller = $row['seller_id'];
}
It gives me this error:
Severity: Notice
Message: Array to string conversion
Severity: Notice
Message: Array to string conversion
This message means that you are getting an array and you are considering it as string as you are passing it like string.
So here is condition ,either you are getting multiple values in $data['product'] and yes that is correct because you are applying foreach there.So make your query in such a way that it will return only one row from products table.that will be solution
You don't need foreach loop, just check if !empty($data['product']) and use $data['product']['seler_id'] or $data['product'][0]['seller_id'] depends how is your model code. So, as you want exact product by id your code should looks like:
public function show($id)
{
$data['product'] = $this->products_model->get_product_by_id($id);
if (empty($data['product']))
{
show_404();
}
else
{
$seller_id = $data['product']['seller_id'];
$data['seller_name'] = $this->members_model->get_members_by_id($seller);
$data['main_content'] = 'pages/show';
$this->load->view('templates/template', $data);
}
}
One notice here, since you have relations between tables, query in model should contain JOIN section and you would be able get seller details in one query. Basically, your $data['product'] would contain name (and other details from sellers table) in same time or in one query. Check this link.
use array_column to get all seller_id's in one array. just like var_dump array_column ( $data, 'seller_id' ) .
Sorry guys the problem was with the view page, That's how I used the array in the view page and it did work:
foreach($seller_name as $row){
echo'<p class="product-price">'.$row['user_name'].'</p>';
echo'<p class="product-price">'.$row["phone_number"].'</p>';
}

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;
}

Codeigniter passing data from model to controller

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'];

Categories