fetch id from view and pass to another controller - php

i have 2 table as follows
project
id | name | address
boq
id | project_id |item_no | description
project_id is the foreign key. then i have loaded the all data from project table.then i want to load all the data in to boq table which related to project id when click on the project name. here is my code.but it doesn't work.any one can plz help me.
controller
function show_boq() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['boq'] = $this->project_list_model->show_boq($project_id);
$this->load->view('boq_doc', $data);
}
model
function show_boq($id){
$this->db->select('*');
$this->db->from('boq');
$this->db->where('project_id',$data);
$this->db->join('project', 'project.id = boq.project_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
view
<?php foreach ($boq as $boq): ?>
<tr>
<td><?php echo $boq->id; ?></td>
<td><?php echo $boq->item_no; ?></td>
<td><?php echo $boq->description; ?> </td>
</tr>
<?php endforeach; ?>

Use Model like this. Use project table as first and boq table as second.
function show_boq($data){
$this->db->select('*');
$this->db->from('project');
$this->db->join('boq', 'project.id = boq.project_id');
$this->db->where('project.id',$id);
$query = $this->db->get();
$result = $query->result();
}
And view file is
<?php
if (is_array($boq) || is_object($boq))
{
foreach ($boq as $row): ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->item_no; ?></td>
<td><?php echo $row->description; ?> </td>
</tr>
<?php endforeach;
}
?>

Related

Codeigniter - join value from two tables with same column name

i have two table which is riders and teams, and i like to show my data in my table, but the problem is both of those table have the same column name which is name, here for example :
Table riders
Table Teams
and here is the view in my table :
so what im trying to do is to join both table and make alias for the id_team, so that i can get the team name instead of their id_team, but since they had the same column name, i dont know how to do it, here is my model :
public function show()
{
$this->db->select('team.id_team, team.name, rider.*', false);
$this->db->from('teams as team');
$this->db->join('riders as rider', 'team.id_team = rider.id_team');
$query = $this->db->get();
return $query->result();
}
here is my controller :
public function index()
{
$data['riders_data'] = $this->riders->show();
$this->load->view('back-end/template/header');
$this->load->view('back-end/master-rider', $data);
$this->load->view('back-end/template/footer');
}
and here is my table in my view :
<?php
$i = 1;
foreach($riders_data as $rider) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><img src="<?=base_url('/assets/riders/'.$rider->image); ?>" alt="" width="50px"></td>
<td><?php echo $rider->name; ?></td>
<td><?php echo $rider->number; ?></td>
<td><?php echo $rider->country; ?></td>
<td><?php echo $rider->id_team; ?></td> //team name
</tr>
<?php
$i++;
}
?>
any help is really appreciated, thank you!.

Codeigniter using SELECT_SUM, only returning the first column

I am trying to get the value of how many dishes are in each order of the logged-in user, by adding up the quantity of dishes group by order.
The normal query I run which is a success by assuming logged in user's userid is 35:
SELECT o.orderid, location, status, pay, total,
SUM(quantity) AS dishnum
FROM orders o
JOIN ordersdish od
ON od.orderid = o.orderid
WHERE userid = 35
GROUP BY o.orderid;
Result:
Codeigniter code I tried to run:
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','location','status','pay','total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}
Thank you.
Update: I changed a bit my code, it can return data but only the orderid.
Code i used in view:
<?php
if($orderdata->num_rows() > 0)
{
foreach($orderdata->result() as $row)
{
?>
<tr>
<td><?php echo $row->orderid; ?></td>
<td><?php echo $row->location; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->pay; ?></td>
<td><?php echo $row->total; ?></td>
<td><?php echo $row->quantity; ?><td>
<td><button type="button" href="#" class="delete_data delButton" orderid="<?php echo $row->orderid; ?>">Delete</button></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="7" align="center">No Data Founds</td>
</tr>
<?php
}
?>
Code I used in maim.php
public function vieworderself()
{
$this->load->view('header');
$this->model->caltotal();
$data["orderdata"]=$this->model->getorderself();
$this->load->view('/orders/vieworderself', $data);
$this->load->view('footer');
}
Try using table name before every column. Eg. orders.location
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','orders.location','orders.status','orders.pay','orders.total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('orders.userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}

Codeigniter blog: display post title in comments table

I am working on a blog application in Codeigniter 3.1.8 and Bootstrap 4.
There is a posts table and a comments table in the database. I have displayed all the comments in a Bootstrap 4 table. I want to display the title of the post each comment belongs to, instead of the post's id:
My Comments controller:
class Comments extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Static_model');
$this->load->model('Posts_model');
$this->load->model('Categories_model');
$this->load->model('Comments_model');
}
public function index() {
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
$data['number_of_categories'] = $this->Categories_model->get_num_rows();
$data['posts'] = $this->Posts_model->get_all_posts();
$data['number_of_posts'] = $this->Posts_model->get_num_rows();
$data['comments'] = $this->Comments_model->get_all_comments();
$this->load->view('partials/header', $data);
$this->load->view('dashboard/comments');
$this->load->view('partials/footer');
}
}
In the Comments_model model I have:
public function get_all_comments(){
$this->db->select('comments.*');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
In the view:
<tbody>
<?php foreach ($comments as $index => $comment): ?>
<tr id="<?php echo $comment->id; ?>">
<td><?php echo $index + 1; ?></td>
<td class="w-25"><?php echo $comment->comment; ?></td>
<td><?php echo $comment->name; ?></td>
<td><?php echo $posts['title']; ?></td>
<td><?php echo nice_date($comment->created_at, 'D, M d, Y'); ?></td>
<td>Aproved</td>
<td></td>
</tr>
<?php endforeach ?>
</tbody>
While <?php echo $posts->id; ?> displays the posts id, which i do not need in the view, the line results in an
Message: Undefined index: title error.
What is missing?
Hope this will help you :
get_all_comments method should be like this : add posts.title in select also
public function get_all_comments()
{
$this->db->select('comments.*,posts.title as post_title');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
Replace it
<td><?php echo $posts['title']; ?></td>
with
<td><?php echo $comment->post_title; ?></td>

Codeigniter-how to foreach and echo project name by id in my view page

$data['posts'] = $this->whiteboard_model->u_getTSrowsindex(array('limit'=>$this->perPage));
$data['postsexp'] = $this->whiteboard_model->u_getTSrowsexp(array('limit'=>$this->perPage));
//print_r($data['postsexp']); exit;
foreach($data['posts'] as $post)
{
$project_id =$post['project_name'];
$data['projectName'] = $this->whiteboard_model->get_project_name($project_id);
print_r($data['projectName']);
}
//This is my controller
public function get_project_name($project_id) {
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
//echo $this->db->last_query();
//return fetched data
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//my model
<?php if(!empty($posts)): foreach($posts as $post):
//print_r($post);?>
<tr>
<td> <?php echo $post['advisor']; ?></td>
<td> <?php echo $post['spentdate']; ?></td>
<td><?php echo $post['project_name']; ?>
<?php if(!empty($projectName)){
foreach($projectName as $prj_name){ //print_r($projectName);
echo $prj_name['project_name'];
}
} ?>
</td>
<td> <?php echo $post['activity']; ?></td>
<td> <?php echo $post['billtype']; ?></td>
<td> <?php echo $post['timespent']; ?></td>
<td> <?php if($post['status'] == 1){?>
<b style = "color:blue;">Pending</b>
<?php }else if($post['status'] == 2){?>
<b style = "color:green;">Approved</b>
<?php }else{?> <b style = "color:red;">Rejected</b>
<?php } ?> </td>
</tr>
<?php endforeach; else: ?>`enter code here`
//my view file
Finally iam not getting proper values from projectName array ..its giving only one name
It seems like you are fetching data using the project_id
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
Here you may be getting only the details of one project where the project_id = $project_id
Comment out that line and see if you get all the project names. Additionally use a print_r($query->result()); to verify your data.
So that it would look like:
$this->db->select('project_name');
$this->db->from('project_list');
//$this->db->where('project_id',$project_id);
$query = $this->db->get();
print_r($query->result());
You can refer more here.

Nothing happened when I tried to echo data from join table in CodeIgniter

My model :
public function category($cat)
{
$this->db->select('c.id, c.cat2, c.category, m.id, m.date, m.when_date, m.when_time, m.where_m');
$this->db->from('category c');
$this->db->where('c.category', '$cat');
$this->db->join('meeting m', 'm.id_cat = c.id');
$result = $this->db->get();
return $result->result();
}
My controller:
$data['meeting'] = $this->users_m->category($cat);
$this->load->view('category', $data);
My view:
<table>
<?php foreach($meeting as $row): ?>
<tr>
<td><?php echo ($row->id); ?> </td>
<td><?php echo ($row->id_cat); ?> </td>
<td><?php echo ($row->date); ?> </td>
<td><?php echo ($row->when_date); ?> </td>
<td><?php echo ($row->where_m); ?></td>
</tr>
<?php endforeach; ?>
</table>
And nothing happend while I try to echo this data; no error message nothing. Any idea? I'm new in this.
The where clause could be the problem. You wrote:`
$this->db->where('c.category', '$cat');
I think it should look something like:
$this->db->where('c.category', $cat);
(without the single quotes)
So the query is comparing the category to the string $cat. I guess this won't be in your database. So the result will be empty.

Categories