Access query inside foreach codeigniter - php

anyone can help me here, I am new in codeigniter framework. I am trying to get the price field under the accounting table, which connected to the test_kind table?
Thankyou..
model
public function get_pending_test($row_id){
$where = "statust='0' and tyre_no=$row_id";
$this->db->select('lab_id, tyre_no, test')
->from('test_kind')
->where($where);
$query = $this->db->get();
//return $query->result_array(); If i put this code it will return the lab_id, tyre_no, test, but I need to get the price field on the accounting table.
foreach($query->result_array() as $row){
$this->db->select('price')
->from('accounting')
->where('test', $row['test'])
->where('sample_idacc', $row['tyre_no']);
$query = $this->db->get()->row();
}
}
controller
public function samples(){ //list of sample
if($this->session->userdata('is_logged_in')){
$data['pending_test'] = $this->test_sample_model->get_pending_test($row_id);
$this->load->view('../template/header');
$this->load->view('test_sample', $data);
$this->load->view('../template/footer');
} else {
redirect('main/restricted');
}
}
view
<?php if(!empty($pending_test)) :
foreach($pending_test as $get) {?>
<tr>
<td><?=$get->lab_id?></td> //test_kind table
<td><?=$get->tyre_no?></td> //test_kind table
<td><?=$get->test?></td> //test_kind table
<td><?=$get->price?></td> //I should get this from accounting table
</tr>
<?php } else : ?>
<tr>
<td>No Result Found.</td>
</tr>
<?php endif; ?>

Use JOIN statement
$query = $this->db->query(
"SELECT
test_kind.lab_id,
test_kind.tyre_no,
test_kind.test,
accounting.price
FROM
test_kind
INNER JOIN
accounting
ON
test_kind.tyre_no = accounting.sample_idacc
WHERE
statust='0' and
tyre_no= $row_id;"
);
$result = $query->result_array();
return $result;

you can use Join to get the records like this .
$this->db->select('tk.lab_id, tk.tyre_no, tk.test,a.price')
$this->db->from('test_kind' as tk)
$this->db->join("accounting as a","a.sample_idacc = tk.tyre_no","LEFT");
$this->db->where('tk.status',0);
$this->db->where('tk.tyre_no',$row_id);
$query = $this->db->get();
return $query->result_array();

Related

Retrieve data based on user id (CodeIgniter)

Im fairly new with php so please bear with me. I have two tables in my database, users and onlineform tables. The onlineform table have userid field as a foreign key for the id field in users table. Im trying to display data from onlineform table based on current user login. The problem is, it display data from all users and not the one who logged in. I have setup a query in my form_model but i dont know if the query is correct
this is the model
form_model.php:
function getEmployees()
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}
this is the controller
form.php
public function table()
{
if($this->session->userdata('username') != '')
{
$this->load->model('form_model');
$query = $this->form_model->getEmployees();
$data['EMPLOYEES'] = null;
if($query) {
$data['EMPLOYEES'] = $query;
}
}
else
{
redirect(base_url() . 'index.php/form/login');
}
$this->load->view('table.php', $data);
}
and this is my view
table.php
<?php
foreach($EMPLOYEES as $employee)
{?>
<tr>
<td align="center"
<?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
<strong><?=$employee->day;?></strong>
</td>
<td align="center"
<?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
<strong><?=$employee->date;?></strong>
</td>
<td><?=$employee->out1;?></td>
<td><?=$employee->in1;?></td>
<td><?=$employee->out2;?></td>
<td><?=$employee->in2;?></td>
</tr>
<?php }?>
You can pass the current logged in userId into the model through getEmployees($userid)
$query = $this->form_model->getEmployees($userid);
And then use it in your query to get current userId that matches logged in user, to user in database;
function getEmployees($userid)
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->where('onlineform','users.id',$userid); //add this line
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}
And then pass it normally to your view.
Hope this helps.
You can save the current logged in userid in session then you can use it when you retrieve the data from database.
change your form_model.php code to this
function getEmployees()
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->where("users.id", $this->session->userdata('current_user_id'));
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}

Codeigniter - Get_where base from session_userdata

I want to select all the rows that emp id's are match to my session userdata (emp_id). Here's my code. I get so many errors and no row has been selected. Someone help me please. Thanks
Model:
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->row_array();
}
Controller:
public function show() {
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
$this->load->view('show',$data);
}
Views:
<?php foreach ($save as $row) { ?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php }>
Try This
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result();
}
you can try solution for your problem :
Changes your modal function :
Modal.php
public function get_save_samp($emp_id) {
$this->db->select("*");
$this->db->where('id', $emp_id);
$this->db->get('tblsavesample');
return $query->row();
}
Views:
<?php foreach ($save as $row) {?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code ?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php } ?>
row_array() returns the first row only. If you want all the records to be returned, use result_array() instead.
$result = $query->result_array();
return $result;
Link : click
Change your Modal Code like this :
public function get_save_samp($emp_id) {
//$query = $this->db->get_where('tblsavesample', array('emp_id' =>$emp_id));
$this->db->select("*");
$this->db->where_in('id', $emp_id);
$query = $this->db->get('tblsavesample');
return $query->result();
}
And Change your Controller code like this to check what data are we getting inside controller.
public function show()
{
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo "<pre>";
print_R($data['save']);
exit();
$this->load->view('show', $data);
}
row_array() used to return only single record. change your code in model as follows :
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result_array();
}
and check number of records returned in your controller as follows:
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo $data['save']->num_rows();
if this displays 0, then you have no matching records in your database table.
to test the query, try to execute the same query in mysql

how to query two tables from database and show the result in a single row using codeigniter

I have two tables One for users and the other for project listing
My problem is that i want to display each project(from project_table)and email belonging to user who listed the project(from user_table) on a single row
The project_table has a row for user_id(i.e id from user_table that identifies the user who posted the project)
here's my view(project_view):
I this case im displaying data from project_table but i want to display email for a particular user from user_table
<?php
foreach($query as $row)
{
?>
<p> <?echo $row->pro_name ?></p>
<p> <?echo $row->duration ?></p>
<p> <?echo $row->budget ?></p>
<p>User email will be displayed here</p>
<?
}
my model:
function get_projects()
{
$query = $this->db->get('project_table');
return $query->result();
}
my controller:
function show_projects()
{
$data['query']=$this->project_model->get_projects();
$this->load->view('project_view', $data);
}
Any ideas on how to implement this will be much appreciated
You can use joined query in your model
function get_projects()
{
$query =$this->db->select('p.*,u.email')
->from('project_table p')
->join('user_table u','p.user_id = u.id')
->get();
return $query->result();
}
And in your view you can do so
<?php
foreach($query as $row)
{
?>
<p> <?php echo $row->pro_name; ?></p>
<p> <?php echo $row->duration; ?></p>
<p> <?php echo $row->budget; ?></p>
<p><?php echo $row->email;?></p>
<?php
}
?>
I would rewrite controller and model function to allow for a user parameter:
function get_projects($user=null)
{
if ($user == null){
$query = $this->db->get('project_table');
}else{
$query = $this>db->get('project_table')->where('user_id',$user);
}
return $query->result();
}
function show_projects($user)
{
$data['query']=$this->project_model->get_projects($user);
$this->load->view('project_view', $data);
}
Now you can call your controller with a user parameter
http://..../show_user/1
1st approach is JOINs
$this->db->select('project_table.* as project, user.id as user_id, user.email as email')
->from('project_table')
->join('user', 'project.id = user_id');
$result = $this->db->get();
2nd not recommended
function get_projects()
{
$query = $this->db->get('project_table');
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
function get_email($id = FALSE) {
if ($id === FALSE) return FALSE;
$query = $this->db->get_where('user', array('user_id' => $id));
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
somewhere in controller...
if (!$projects = $this->model->get_projects()) {
foreach ($projects as $key => $project) {
# code...
$projects[$key]->user_email = $this->model->get_email($project->user_id);
}
//projects is now with user_email field
$this->load->view()...
} else {
//no data recieved
redirect(); //redirect to default_controller
}
Try join this -
1) Model
function get_projects()
{
$this->db->select('*');
$this->db->from('project_table');
$this->db->join('user_table', 'project_table.user_id = user_table.id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
}
2) Controller
$data['user_project'] = $this->project_model->get_projects();
$this->load->view($view, $data);
3) View
foreach($user_project as $row)
{
echo "<p>" .$row->pro_name. "</p>";
echo "<p>" .$row->duration. "</p>";
echo "<p>" .$row->budget. "</p>";
echo "<p>" .$row->email. "</p>";
}

Pass mysql_query results to foreach

I used the following code to to get list of users facebook friends and ccheck it against users in an app database. This code would return the users of the app, who are Facebook friends of the user.
$friends_set = '(';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = mysql_query("SELECT * from user AS u, upload AS up WHERE u.fb_id IN $new_set AND u.fb_id=up.user_id") or die(mysql_error());
while($row = mysql_fetch_array($res)) {
echo $row['fb_id']. "". $row['first_name'];
echo "<br>";
}
$data['top_friends']=$res;
$this->load->view('myview');
This code works. It is in a controller of my codeigniter application and it successfully echos the correct data onto the page.
However now I need to print the result of the query in a for each statement in my view like this:
<?php foreach ($top_friends as $me) : ?>
<div >
<p><?php echo $me['first_name']; ?></p>
<?php endforeach; ?>
However when I try getting the query results in the view using the for each it doesn't work.
How do i fix this?
You could try it the codeignitor way, Create a model function say get_top_friends and i assume that you are passing a comma separated string as argument like $fb_id = '45,65,78,89'. Say facebook_model is the name of the model then :
class Facebook_model extends CI_Model{
//other functions and constrcutor here
//function to get the top friends
function get_top_friends($fb_id){
$fbId = explode(",",$fb_id)
$this->db->select('*');
$this->db->where_in('fb_id',$fbId);
$this->db->order_by('points','desc');
$this->db->limit(10);
$query = $this->db->get('user');
if ($query->num_rows() < 1) return FALSE;
return $query->result_array();
}
}
And make change in your code as below:
$friends_set = '';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = $this->facebook_model->get_top_friends($new_set);
$data['top_friends']=$res;
$this->load->view('myview',$data);
And now in view you can try
foreach ($top_friends as $me){
echo $me['first_name'];
}
[Updated for user ]
If you want to do it as in your question : then try,
$result = array();
while($row = mysql_fetch_array($res)) {
$result[] = $row;
}
$data['top_friends']=$result;
$this->load->view('myview',$data);//pass data to view

Dropdown using codeigniter is not dropping any data

In the below codeigniter code i have placed the controller ,model and view part.My aim is to drop exam name from exam table.In my case it is drop the exam name from exam table.Pls help me o do this .
Controller
public function index()
{
//echo "inside form upload";
$data = array();
//$college_name = $this->session->userdata('college_name');
if($query = $this->import_model->get_exam_data())
{
$data['exam_data'] = $query;
}
//$this->load->view('student_view', $data);
$this->load->view('form_upload');
}
model
function get_exam_data()
{
//$this->db->distinct();
$this->db->select("CONCAT(exam_name, ' ', month,' ',year) AS fullexamname", FALSE);//this will concat the value
//$this->db->where('college_name',$college_name);
$query = $this->db->get('exam_table');
return $query->result();
}
view
<?php
$data = array();
$data["Select Exam Name"] = "Select Exam Name";
foreach ($exam_data as $row)
{
$data[$row->fullexamname] = $row->fullexamname;
}
echo form_dropdown('exam_name', $data, 'small', 'class="dropdown_class" id="exam_name_id" ');
?>
*
You have to pass $data to your view file from controller
$this->load->view('form_upload',$data);

Categories