Codeigniter foreach Undefined variable Update Data In - php

I want Update Data In Database Using CodeIgniter! but I have a problem
View
<?php foreach ($single_user as $user): ?>
Controller
$id = $this->uri->segment(3);
$data['user'] = $this->authentication->show_students();
$data['single_user'] = $this->authentication->show_student_id($id);
Library
function show_students(){
$query = $this->ci->db->get('users');
$query_result = $query->result();
return $query_result;
}
function show_student_id($data){
$this->ci->db->select('*');
$this->ci->db->from('users');
$this->ci->db->where('id', $data);
$query = $this->ci->db->get();
$result = $query->result();
return $result;
}

Try to add this expression in your controller
$this->load->view('your_view', $data);
your code must like this :
$id = $this->uri->segment(3);
$data['user'] = $this->authentication->show_students();
$data['single_user'] = $this->authentication->show_student_id($id);
$this->load->view('your_view', $data);
Hopefully it helps you.

Related

if condition with where clause

I just want to do this if(status==0) then only where condition is run and print only related to status==0 record.And when status==1 come then it bypass the where clause and print both data for status 0 and 1. I don't know how to build a query for this. Please help me in this.
my model code
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->where('student_info.admin_id',$admin_id)
->join('admin','admin.admin_id = student_info.admin_id')
->get();
return $query->result();
}
This code work properly and print separate data whose status 0 or 1.
Only admin_id = 1 contain Status = 1, other id's contain 0 status.
Replace your function with the following code...
<?php
function listing($status)
{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
if($status==0)
{
$this->db->where('student_info.admin_id',$admin_id);
}
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
?>
Try this:
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->join('admin','admin.admin_id = student_info.admin_id')
->where("student_info.admin_id = IF (admin.status = 0, $admin_id, '>-1'")
->get();
return $query->result();
}
It solves my problem..
function listing()
{
$admin_id = $this->session->userdata('admin_id');
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->or_where('admin.status < 1');
$query = $this->db->get();
return $query->result();
}
<?php
function listing($status)
{
if($status == 0){
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
}
?>

How to get a single value using codeigniter and mysql

i need to get service_price field from db by using this code.
public function get_service_price($id) {
$this->db->select('*');
$this->db->from('service');
$this->db->where('id', $id);
$query = $this->db->get('service');
return $query->row('service_price');
}
here i called this function function.
public function vat($vat=14.5)
{
$id = $this->uri->segment(4);
$data['service_price'] = $this->service_model->get_service_price($id);
$price_with_vat=0;
$price_with_vat = $data + ($vat*($data/100));
//$price_with_vat=78.005;
$price_with_vat = round($price_with_vat, 2);
$this->load->view('admin/service/vat', $price_with_vat);
}
There some mistake. please kindly help me
To get first row use following
$query = $this->db
->where('id', $id);
->get('service');
$firstRow = $query->first_row();
return $firstRow->service_price;
Use this code for your model :
public function get_service_price($id) {
$this->db->select('service_price');
$this->db->from('service');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row();
}
in model
public function get_service_price($id) {
$query = $this->db->query("SELECT * FROM service WHERE id='$id'");
$result = $query->result_array();
return $result;
}
in controller
$id = $this->uri->segment(4);
$data['service_price'] = $this->service_model->get_service_price($id);
$price_with_vat=0;
$price_with_vat = $data[0]['table_field_name'] + ($vat*($data[0]['table_field_name']/100)); //$price_with_vat=78.005;
$price_with_vat = round($price_with_vat, 2);
$this->load->view('admin/service/vat', $price_with_vat);
in view
foreach ($service_price as $new_service_price)
{
echo $new_service_price['table_field_name'];//exapmle echo $new_service_price['service_price']
}

assigning a value from a array to a variable in codeigniter

i want to save a value from a array to a variable .for doing a if condition check . this is my code inside models folder . i got a errormysqli_fetch_array() expects parameter 1 to be mysqli_result, object given .but the $query returns a array value.
public function this_is_try(){
$this->db->select('*');
$this->db->from('partnerprofile');
$this->db->where('User_Id','20');
$query = $this->db->get();
$query->result_array();
$row = mysqli_fetch_array($query);
$user_id = $row['User_Id'];
$agefrom = $row['AgeFrom'];
print $user_id;
exit;
}
Instead of passing the entire $results(result object). store the result_object to a variable and pass it ie $result=$results[0];
public function this_is_try(){
$this->db->select('*');
$this->db->from('partnerprofile');
$this->db->where('User_Id','20');
$query = $this->db->get();
$results = $query->result();
$result = $results[0];
$user_id=$result->user_id;
$agefrom = $result->AgeFrom;
print $user_id;exit;
}
Try this :
public function this_is_try(){
$row = $this->db->select('*')
->from('partnerprofile');
->where('User_Id','20');
->get()->row();
$user_id = $row->User_Id;
$agefrom = $row->AgeFrom;
print $user_id;exit;
}

I want to get data with and condition but unknown error occured in codeigniter

my code is ... i m fetch data with and condition in codeigniter but
some error has occured... my model is this bellow...
public function hospital_edit($param = null){
$Hospitals_id = $param;
$status = 1;
$this->db->select('*');
$this->db->from('wl_hospitals');
$this->db->where('Hospitals_id', $Hospitals_id);
$this->db->where('Hospitals_status',$status);
$result = $this->result();
echo "<pre>"; print_r($result);exit;
}
You cannot directly fetch the result without calling get() on your query try this
$Hospitals_id = $param;
$status = 1;
$query=$this->db
->select('*')
->from('wl_hospitals')
->where('Hospitals_id', $Hospitals_id)
->where('Hospitals_status',$status)
->get();
$result = $query->result();
try this
public function hospital_edit($param = null){
$Hospitals_id = $param;
$status = 1;
$this->db->where('Hospitals_id', $Hospitals_id);
$this->db->where('Hospitals_status',$status);
$query = $this->db->get('wl_hospitals');
if($query->num_rows()>0){
$result = $query->result();
}else{
echo "No Data";
}
}
Try this :
public function hospital_edit($param = null){
$Hospitals_id = $param;
$status = 1;
$hospitals = $this->db->get_where('wl_hospitals', array('Hospitals_id' => $Hospitals_id,'Hospitals_status' => $status));
$result = $hospitals->result();
}

CodeIgniter passing array with queries into view

Hi I am having an issue getting the view to show any results for the following code.
Controller:
$datedue = 2011-01-27;
$username = $this->tank_auth->get_username();
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$count = 0;
$taskdetails = array();
foreach($tasks->result() as $row)
{
$taskid = $row->taskid;
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
$count++;
}
$data['taskdetails'] = $taskdetails;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
View:
<?php
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row->name;
}
}
?>
Any help would be nice thanks.
The reason why your view is not displaying something is because you're query is not completely correct.
$datedue = 2011-01-27;
should be enclosed in quotes, since the date is a string.
$datedue = "2011-01-27";
Also, you're not correctly following the concept of MVC. All the database querying and results should occur inside of the Model. And all of the data handling inside the Controller.
The Controller nor the View should handle any of the database connections, that is the duty of the Model. Therefore I would advise to create a Model and put all of the Database querying in one or two functions. The Controller would then call these functions and receive the data back. Then it should manipulate it and pass it to the View in a clean matter (by this I mean, that the View should NEVER call result()
Here is how you're code should be structured:
CONTROLLER
class Tasks extends Controller {
function Tasks ()
{
parent::Controller();
$this->load->model('tasks_model');
$this->load->database();
}
function index()
{
$datedue = "2011-01-27";
$username = $this->tank_auth->get_username();
$tasks = $this->tasks_model->getTasks($username, $datedue);
$count = count($tasks);
$data['taskdetails'] = $tasks;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
}
}
MODEL
class Tasks_model extends Model {
function Tasks_model()
{
// Call the Model constructor
parent::Model();
}
function getTasks($username, $datedue) {
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$taskdetails = array();
foreach($tasks->result() as $row){
$taskid = $row->taskid;
array_push( $taskdetails, $this->getTasksDetails( $taskid ) );
}
return $taskdetails;
}
function getTasksDetails($taskid) {
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails = $this->db->query($subsql, array($taskid));
return $taskdetails->row();
}
}
VIEW:
foreach($taskdetails as $task)
{
echo $task->name;
}
verify your task table contain the name field
view
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row[0];
}
}
why you are passing array in this line
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
instead write
$taskdetails[$count] = $this->db->query($subsql, $taskid);

Categories