How to grab a value from multi dimensional array - php

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

Related

In blade, I want to print out all the items except $id item that is from model

In Controller,
public function detail($id)
{
$item = Item::find($id);
return view('frontend.detail',compact('item'));
}
In blade,
{{$item->subcategory->items}} //question //this code print all the items//
You need to select the items manually if you don't want to fetch the id
Currently, you are fetching all the items by using $item = Item::find($id); it means that fetch all the fields of the particular table againt the given id.
you can use below query by modifying you required fields
Item::select('name','surname')->where('id', 1)->get();
You Need to make hidden id of the column in query Example
Item::find($id)->makeHidden(['id']);
return view('frontend.detail',compact('item'));

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

Laravel Get All Rows From Different Ids

today i was just practicing my skills and got stuck. I just want to fetch data from the database using specific ids which i put inside my session as an array. The problem is, i cant find a way to fetch all the data that includes these ids.
here's my cart checkout method
public function checkout(){
$uniqid = session('products');
$post=Post::find($uniqid);
return view('pages.checkout')->with('post',$post);
}
I am getting no data, even if i try to get something i only get one field when there's something like 7 of them.
Here's the session products array
0 "15be4aa3b55f10"
1 "15be4814ceb2a0"
2 "15be4814ceb2a0"
3 "15be4aa3b55f10"
4 "15be4aa3b55f10"
5 "15be4aa3b55f10"
6 "15be4aa3b55f10"
7 "15be4aa3b55f10"
The find query returns single row of data. If you want to fetch more than 1 data, use the all query:
$post = Post::all();
or if you want to get all data with the same id as your $uniqid, use the where query:
$post = Post::where('column_name', $uniqid)->get();
EDIT
IF your session has many id, you may want to loop and find all the data with the same id as your $uniqid
public function checkout(){
$products = [];
foreach(session('products') as $session){
$post=Post::find($session->id);
$products[] = $post;
}
return view('pages.checkout')->with('products ',$products);
}
you may try this:
public function checkout(){
$uniqid = session('products');
$products = Post::whereIn('unique_id', $uniqid)->get();
return view('pages.checkout')->with('products',$products);
}
where whereIn method takes a field name and array of values
From laravel docs:
The whereIn method verifies that a given column's value is contained within the given array
You should use whereIn() method to fetch records against multiple ids. do like this
public function checkout(){
$uniqidsArray= session('products');
$post=DB::table('yourTableName')
->whereIn('id', $uniqIdsArray)
->get();
return view('pages.checkout')->with('post',$post);
}

Only last row getting displayed in view in Codeigniter

I'm trying to fetch certain values from and then pass it to another model in the same control.
However I'm only able to display the last row in the view.
I have shared my code below and I'm not sure where I'm going wrong.
Controller:
public function test($id){
$mapping_details = $this->queue_model->get_mapping_details($id);
foreach ($mapping_details as $value) {
$data['agent_details'] = array($this->agent_model->get_agent_details($value['user_id']));
}
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Model:
public function get_agent_details($id) {
$query = "select * from user_table where id = ".$id." and company_id = ".$this->session->userdata('user_comp_id');
$res = $this->db->query($query);
return $res->result_array();
}
Welcome to StackOverflow. The problem is the iteration in your controller. You are iterating through the $mapping_details results and per every iteration you are re-assigning the value to $data['agent_details'] , thus losing the last stored information. What you need to do is push to an array, like this:
foreach ($mapping_details as $value) {
$data['agent_details'][] = $this->agent_model->get_agent_details($value['user_id']);
}
However, wouldn't it be best if you created a query that uses JOIN to get the related information from the database? This will be a more efficient way of creating your query, and will stop you from iterating and calling that get_agent_details() over and over again. Think of speed. To do this, you would create a model method that looks something like this (this is just an example):
public function get_mapping_details_with_users($id){
$this->db->select('*');
$this->db->from('mapping_details_table as m');
$this->db->join('user_table as u', 'u.id=m.user_id');
$this->db->where('m.id', $id);
$this->db->where('u.company_id', $this->session->userdata('user_comp_id'));
return $this->db->get()->result();
}
Then your controller will only need to get that model result and send it to the view:
public function test($id){
$data['details_w_users'] = $this->queue_model->get_mapping_details_with_users($id);
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Hope this helps. :)

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

Categories