Codeigniter db query on two tables - php

Im new to codeigniter and php, mysql and i am making a codeigniter app, and have a question about db querys using active records.
I want to loop through the "users" table and at the same time get a table called cv where the userid is equal to owner_id in the cv records. The cv table has a lot of columns like school, start_date, end_date, grades etc. Heres how i did it:
The controller:
function index() {
$data['members'] = $this->user_model->_search_members();
$data['main_content'] = 'agency/start';
$this->load->view('site_view', $data);
}
The model:
function _search_members()
{
$this->db->select('id,username,first_name,last_name,company,presentation,title_1,title_2,title_3,last_login,user_pic,counties,municipalities,birthday,gender,webpage')->from('users');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
}
function _get_cv($id) {
$this->db->select()->where('owner_id',$id);
$query = $this->db->get('cv');
return $query->result();
}
The view:
<section id="main">
<h2>Search members</h2>
<table>
<tr>
<td></td>
<td>User</td>
<td>Gender</td>
<td>Name</td>
<td>Title</td>
<td>Location</td>
<td>Age</td>
<td>School</td>
</tr>
<?php foreach ($members as $member) : ?>
<?php $cvs = $this->user_model->_get_cv($member->id); ?>
<tr>
<td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
<td><?php echo $member->username;?></td>
<td><?php echo $member->gender;?></td>
<td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
<td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
<td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
<td><?php echo $member->birthday;?></td>
<td>
<?php foreach ($cvs as $cv) : ?>
<?php echo $cv->school; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Now my question is, is this a good way to do it? I also want to be able to search through the users and cv tables on the page with a form i am creating later. Should i do a join on the tables instead to able to make better search queries or can i still do it this way?
Any help appreciated.
Regards
George

Use it like this:
$this->db->select('*');
$this->db->from('users');
$this->db->join('cv', 'users.id = cv.owner_id','left');
$query = $this->db->get();
For more information:
http://codeigniter.com/user_guide/database/active_record.html
Updated:
$this->db->select('*');
$this->db->from('users');
$this->db->join('cv', 'users.id = cv.owner_id','left');
$data = $this->db->get();
$group_cv = $users = array();
foreach ($data as $k=>$v) {
$group_cv[$v['id']][] = $v;
}
foreach ($data as $k=>$v) {
$users[$v['id']] = $v;
}
?>
<section id="main">
<h2>Search members</h2>
<table>
<tr>
<td></td>
<td>User</td>
<td>Gender</td>
<td>Name</td>
<td>Title</td>
<td>Location</td>
<td>Age</td>
<td>School</td>
</tr>
<?php foreach ($users as $member) : ?>
<tr>
<td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
<td><?php echo $member->username;?></td>
<td><?php echo $member->gender;?></td>
<td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
<td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
<td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
<td><?php echo $member->birthday;?></td>
<td>
<?php foreach ($group_cv[$member['id']] as $cv) : ?>
<?php echo $cv->school; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>

Related

How to print out two queries in one table?

I have two queries in a form. I print out the results like this:
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to present each result in a different column. But the result from the second query ("Column4") seems to be presented in a second table (??). It's not echoed next to the other columns, but below:
Current Output
How can I fix this issue?
You have to change
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
into
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
You have begun another row ('tr' tag closed and re-opened).
Try this fix...
I have removed the things that are causing it to echo on next line
Now this will work...
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

PHP & SQL WHERE (changing variable)

Good Day, i'm struggling with this code..
The result "10" is set $fod but this needs to be replaced to display the called row's id so that the SQL WHERE can list the correct data inside the relevant row. See Image for the Result i'm getting.
See Current Result and Desired Result
As you can see i need ROW ID 10 to display 10's items and ROW ID 11 to display 11's items. Please assist me, how can i call these rows to display correctly?
Calling function:
Controller.php
$data['order_list'] = $this->product->data_ordershalf();
$fod = 10;
$data['order_listfull'] = $this->product->data_ordersfull($fod);
Functions.php:
function data_ordershalf(){
$this->db->select('*');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('orders');
$this->db->group_by('orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
View.php:
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) { ?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?>
</table>
</td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
You can create helper and put following function it.
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
}
====================View.php======================================
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) {
$order_listfull=data_ordersfull($row['id']);
?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?></table></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
function data_ordershalf(){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->group_by('order_detail.orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->from('order_detail');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->where('order_detail.orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

How to calculate sum of unique column (column name is total) using codeigniter

Controller
This is Controller Page.
public function index() {
$data = array(
'year' => $this->year_model->list_all(),
'speces' => $this->speces_model->list_all(),
);
foreach ($data['year'] as $value):
$temp = array();
$temp['year_key'] = $value['year-range'];
$temp['year_id'] = $value['id'];
$temp['speces_key'] = array();
foreach ($data['speces'] as $value1):
$temp['speces_key'][$value1['name']] = $this->db->get_where('expendituredata', array('spacesId' => $value1['id'], 'yearId' => $value['id']))->row_array();
endforeach;
$final_result[] = $temp;
endforeach;
$data['result'] = $final_result;
$this->load->view('site/template', $data);
}
Model
public function list_all() {
return $this->db->get('year')->result_array();
}
View
This is View file.
<table>
<thead>
<tr>
<th>Year Range</th>
<?php foreach ($speces as $value): ?>
<th> <?php echo $value['name']; ?></th>
<?php endforeach; ?>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $value): ?>
<?php $sum = 0; ?>
<tr>
<td><?php echo $value['year_key']; ?></td>
<?php foreach ($value['speces_key'] as $key => $value2): ?>
<td><?php echo $value2['data']; ?></td>
<?php $sum+= $value2['data']; ?>
<?php endforeach; ?>
<td><?php echo $sum; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>Total</td>
<?php foreach ($speces as $value6): ?>
<?php
$this->db->select_sum('data');
$query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();
$totals = $query['data'];
?>
<td><?php echo $totals; ?></td>
<?php endforeach; ?>
</tr>
</table>
[Screen Shot]
My Question: How to calculate sum of column (column name is total).column name total is display above screenshot.so how to calculate sum of column(column name is total) and sum display in red arrow in above screen shot
I think you want to calculate sum of total column which is not a database field. So to do that you can take a variable in which you will addup the column value of every row.
Just Initialize a variable to zero.
<?php $sum = $totalSum = 0; ?>
Now add value of sum variable for every row.
change your line,
<td><?php echo $sum; ?></td>
with
<td><?php $totalSum+=$sum; echo $sum; ?></td>
Now your $totalSum field will have the sum of your total column. You can show it where ever you want.
UPDATED
Use this in your view file and check.
<table>
<thead>
<tr>
<th>Year Range</th>
<?php foreach ($speces as $value): ?>
<th> <?php echo $value['name']; ?></th>
<?php endforeach; ?>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<?php $totalSum=0; ?>
<?php foreach ($result as $value): ?>
<?php $sum = 0; ?>
<tr>
<td><?php echo $value['year_key']; ?></td>
<?php foreach ($value['speces_key'] as $key => $value2): ?>
<td><?php echo $value2['data']; ?></td>
<?php $sum+= $value2['data']; ?>
<?php endforeach; ?>
<td><?php $totalSum+= $sum; echo $sum; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>Total</td>
<?php foreach ($speces as $value6): ?>
<?php
$this->db->select_sum('data');
$query = $this->db->get_where('expendituredata', array('spacesId' => $value6['id']))->row_array();
$totals = $query['data'];
?>
<td><?php echo $totals; ?></td>
<?php endforeach; ?>
<td><?php echo $totalSum; ?></td>
</tr>
</table>
I think this will solve your problem.
try to use
<?php $sum = $sum + $value2['data']; ?>
instead of
<?php $sum+= $value2['data']; ?>

Can't put data from database to a table with PHP

My problem is that I can't fill my table with the data I got in my database. I wrote a code in PHP to do it but it doesn't work and I don't know how to make it work. Any idea ? Here's my code :
view gestJury :
<h3>Liste des jurys :</h3>
<div class="tablebox">
<table>
<thead>
<tr>
<th>N°</th>
<th>Professeur1</th>
<th>Professeur2</th>
<th>Salle</th>
<th>Heure</th>
<?php if($rang == 0){ ?>
<th class="action" >Supprimer</th>
<?php } ?>
</tr>
</thead>
<?php $i = 0; ?>
<?php while($donnees = $listeJury->fetch(PDO::FETCH_ASSOC)){
$i = $i + 1;
?>
<tbody>
<tr class="row0">
<td><?php echo $donnees['idJury'] ?></td>
<td><?php echo $donnees['idProf'] ?></td>
<td><?php echo $donnees['idProf2'] ?></td>
<td><?php echo $donnees['Salle'] ?></td>
<td><?php echo $donnees['horaireJury'] ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
index.php :
function listJuryGlobal(){
$jury = new Professeur();
$listeJury = $jury->getJuryGlobal();
require('view/gestJury.php');
}

Categories