how can i join the two arrays from controller - php

How can I join the two arrays from controller? I tried to use AND operator as shown below but I failed. How can I make it possible?
function view_product($category_id)
{
$category_id=$this->uri->segment(3);
$this->load->model('select');
$this->data["products"]=$this->select->get_products($category_id);
$this->data["prices"]=$this->select->get_price($category_id);
$this->load->view("header");
$this->load->view("products",
$this->data);
}
here is my view:
<?php
$n=0;
foreach(($products as $product) && ($prices as $price) ) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
echo '</table>';
?>
My model
function get_products($category_id)
{
$this->db->select('*');
$this->db->from('categories c');
$this->db->join('products p', 'c.category_id=p.category_id', 'left');
$this->db->where('c.category_status = 0');
$this->db->where('c.category_id', $category_id);
$query = $this->db->get();
return $query->result_array();
}
function get_price($category_id)
{
$this->db->select('*');
$this->db->from('categories c');
$this->db->join('price r', 'c.category_id=r.category_id', 'c.product_id=r.product_id', 'left');
$this->db->where('c.category_status = 0');
$this->db->where('c.category_id', $category_id);
$query = $this->db->get();
return $query->result_array();
}
How can i join the two arrays from controller?

to iterate over 2 arrays, use array_map
function output_prices($product,$price) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
array_map('output_prices',$products,$prices);
source: http://php.net/manual/en/function.array-map.php

Please change your View as per below mentioned code.
<?php
$n=0;
$merg_detls=array_merge($products,$prices);
foreach($merg_detls as $detl) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$detl['product_id'].'</a></td>
<td>'.$detl['product_name'].'</td><td>'.$detl['product_description'].'</td><td>'.$detl['price_per_unit'].'</td><td>'.$detl['price_per_item'].'</td></tr>';
}
echo '</table>';
?>

Related

Codeigniter, Duplicating Results After Nested Foreach

I have got 2 models within my Codeigniter, 1 getting all rooms and the second model is getting the booked rooms in a range of selected date.
On my view I can list all rooms with a foreach and when I insert another foreach for the booked ones to appear in red color, I get a result but, I see duplicated results
My Models:
public function rooms(){
$result = $this->db->select('*')
->from('rooms')
->group_by('room_id')
->get()
->result();
return $result;
}
public function check_room_availability($start, $end){
$this->db->select('*');
$this->db->from('room_actions as ra', 'rooms as r');
$this->db->join('rooms as r', 'r.room_id = re.room_id', 'LEFT');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$this->db->group_by('ra.room_id');
$query = $this->db->get()->result();
return $query;
}
And my View :
<?php foreach($all_rooms as $room) {
foreach($booked_ones as $booked) {?>
<div class="<?php
if($booked->room_id == $room->room_id){
echo 'room_box red';}
else{
echo 'room_box green';}?>">
<?php echo $room->room_id; ?>
</div>
<?php } ?>
<?php } ?>
I'd appreaciate if you could help me. Thanks.
As you can see in the image below if I have 2 rooms booked for the chosen dates my rooms duplicate. If I have 3 rooms booked, then it becomes X 3.
https://pasteboard.co/I8AkG8i.jpg
Try this, hope it will help you.
If rooms table has a unique room_id then do not need to use group_by()
public function rooms(){
$result = $this->db->select('*')
->from('rooms')
->get()
->result();
return $result;
}
Here do not need to join()
public function check_room_availability($start, $end){
$booked_rooms = array();
$this->db->select('room_id');
$this->db->from('room_actions as ra');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$query = $this->db->get()->result();
if(isset($query) && count($query) > 0){
foreach($query as $row){
if(!in_array($row->room_id, $booked_rooms)){
array_push($booked_rooms, $row->room_id);
}
}
}
return $booked_rooms;
}
Now you have a simple array of booked room ids. In the view do this with only one forach() loop
foreach($all_rooms as $room) {
if(in_array($room->id, $booked_rooms)){
//Room is booked
}
}
Try this,
public function check_room_availability($start, $end) {
$this->db->select('r.room_id as room_id,ra.room_id as booked_room_id');
$this->db->from('rooms as r');
$this->db->join('room_actions as ra', 'r.room_id = ra.room_id', 'LEFT');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
}
OR
public function check_room_availability($start, $end) {
$this->db->select('*,(select room_id from room_actions as ra where ra.room_id = r.room_id and ra.days >=' . $start . ' and ra.days <=' . $end . ') as booked_room_id');
$this->db->from('rooms as r');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
}
To better result please share your tables or your expected output, according to that I can update my query

"Message : Undefined Variable:query " error on joining two tables codeigniter mysql php

I have two tables named 'addexpense' and 'addcategory' .
I have successfully joined each of the tables but on my view page the data are not viewing and an error message is passed on the screen. Please help.
This is my model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result();
}
This is my Controller
public function join(){
$query = $this->base_model->getExpenses();
//$data['result'] = null;
if($query)
{
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
This is my view code
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php }?>
set controller function this way
public function join(){
$data['query'] = $this->base_model->getExpenses();
$this->load->view('listexpense', $data);
}
Please can you check your data is going on view file or not by printing it at start of your view
<?php echo '<pre>';
print_r($query);
?>
It seems like you have not initialized model in controller.
In your controller you need to load model first before using its properties. Please check below for your controller method.
public function join() {
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
$data = array();
if ($query) {
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
Let me know still it not works.
Model change result() to result_array()
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result_array();
}
Controller try code below
public function join()
{
$results = $this->base_model->getExpenses();
$data['expenses'] = array();
foreach ($results as $result) {
$data['expenses'][] = array(
'exp_date' => $result['exp_date'],
'exp_amount' => $result['exp_amount'],
'exp_note' => $result['exp_note'],
'exp_created' => $result['exp_created'],
'category_name' => $result['category_name']
);
}
$this->load->view('listexpense', $data);
}
View make sure is the correct view
<table>
<thead>
</thead>
<tbody>
<?php foreach($expenses as $expense){?>
<tr>
<td><?php echo $expense['exp_date'];?></td>
<td><?php echo $expense['exp_amount'];?></td>
<td><?php echo $expense['exp_note'];?></td>
<td><?php echo $expense['exp_created'];?></td>
<td><?php echo $expense['category_name'];?></td>
</tr>
<?php }?>
</tbody>
</table>
Here is the code please check and update
Model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
$query_rows = $query->num_rows();
if($query_rows > 0){
return $query->result();
} else{
return 0;
}
}
For Controller
public function join(){
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
//print_r($query);die();
$data['query'] = '';
if($query != 0){
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
For View
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php
if($query != ''){
foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php } }?>
remove the comment "//print_r($query);die();" and check that whether the model is sending the data or not and please update..

Joins Query not working in Codeigniter

public function getQuestions($params = "",$page= "all", $count=false){
$this->db->query('SELECT questions.questions_id, questions.question_description,
questions.question_explanation, questions.created_date,
questions.updated_date, questions.is_active,
diffLevels.difficulty_levels_title
FROM '.TBL_QUESTION.' as questions
INNER JOIN '.TBL_DIFFICULTY_LEVELS.' as diffLevels
ON questions.fk_difficulty_levels_id = diffLevels.preference
WHERE questions.is_active=1');
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
foreach ($Q->result() as $row) {
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
This is my Query..and I did few many tweaks but it wont work..waiting for possible solutions? Thanks
Try this Query:
$this->db->select('
questions.questions_id,
questions.question_description,
questions.question_explanation,
questions.created_date,
questions.updated_date,
questions.is_active,
diffLevels.difficulty_levels_title
');
$this->db->from("questions");
$this->db->join("diffLevels",'questions.fk_difficulty_levels_id = diffLevels.preference' , 'inner');
$this->db->where("questions.is_active",1);
$query=$this->db->get();
$data=$query->result_array();
//echo $this->db->last_query();
//echo "<hr/>";
//echo "<pre>";
//print_r($query);
//exit;
I have directly used table name as "questions" and "diffLevels",Please change accordingly.

Codeigniter: Join not working with columns same name and id

I wanting to be able to join two tables togeather.
How ever because I my forum table has column "name" and my forum_categories column "name"
I am not able to display both names.
On my select() if I use like $this->db->select('f.name, fc.name', false); it only displays name from forum_categories
array(1) { [0]=> array(1) { ["name"]=> string(17) "News & Discussion" } }
Question how can I get both names to show from both columns and
tables.
Note: I only want to be able to use $result['name'] in my foreach loop.
So the out put I would like it to be
General
News & Discussion
Lounge
I have looked at
CodeIgniter ActiveRecord field names in JOIN statement
codeigniter - select from 2 tables with same column name
Model
public function get_forums() {
$this->db->select('f.name, fc.name', false);
$this->db->from('forum as f');
// tried $this->db->join('forum_categories as fc', 'fc.forum_id = f.forum_id');
$this->db->join('forum_categories as fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
Controller
<?php
class Forums extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['label'] = '';
$data['forums'] = array();
$results = $this->get_forums();
var_dump($results);
if (isset($results)) {
foreach ($results as $result) {
$data['forums'][] = array(
'name' => $result['name'], // Only want to use single variable.
);
}
}
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('template/forum/list_forum_view', $data);
}
public function get_forums() {
$this->db->select('f.name, fc.name', false);
$this->db->from('forum as f');
$this->db->join('forum_categories as fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}
update
works fine with code below but would rather just use one lot of join()
public function get_forums() {
$this->db->select("*");
$this->db->from('forum');
$query = $this->db->get();
foreach ($query->result_array() as $f) {
$data[] = array(
'name' => $f['name']
);
$this->db->select("*");
$this->db->from('forum_categories');
$query = $this->db->get();
foreach ($query->result_array() as $fc) {
$data[] = array(
'name' => $fc['name']
);
}
}
return $data;
}
Try This , It Will Work .
public function get_forums() {
$this->db->select('f.name as forum_name, fc.name as forum_categories_name', false);
$this->db->from('forum f');
// tried $this->db->join('forum_categories fc', 'fc.forum_id = f.forum_id');
$this->db->join('forum_categories fc', 'fc.forum_categories_id = f.forum_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
From what i see in your updated question. What you need is UNION and not JOIN. You can use get_compiled_select() to build both query before concat with UNION.
public function get_forums() {
$forum = $this->db->select('name')->get_compiled_select('forum');
$forum_categories = $this->db->select('name')->get_compiled_select('forum_categories');
$query = $this->db->query($forum.' UNION '.$forum_categories);
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
Simply use
AS
keyword, because when you try to print it, that AS keyword give you optional name for your column.
Here i have two table which have same column name "name" so i have changed those with AS keyword.
Ex:
$this->db->select('tbl_category.id_category,tbl_category.name AS cat_name,
tbl_subject.id_subject,tbl_subject.name AS sub_name');
In result
Array ( [0] => Array ( [id_category] => 9 [cat_name] => OL [id_subject] => 13 [sub_name] => Science )
So it is simple solution from the SQL language

Codeigniter put values from db in array and print them

All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>

Categories