How to get a single value using codeigniter and mysql - php

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']
}

Related

Recursive Function not working In codeIgniter

I am trying to get all categories with related parent ID with a recursive function.
If i print that array its showing me all data but if i want to return this is not working. I am sure i am doing some mistake.
My code for recursive function is:
public function get_child($id,$level=0){
//$level=0;
$this->db->select('*');
$this->db->from('whole_category');
$this->db->where('parent_id',$id);
$this->db->order_by("id", "ASC");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$res= $query->result();
echo $current_node_id=$res[0]->id;
$level++;
$this->get_child($current_node_id,$level);
$cat["level".$level][] =$res;
return $cat;
}else{
return $cat;
}
}
Calling it like:
public function getcategories(){
echo "<pre>";
$a=$this->get_child(1);
var_dump($a);
}
I had created a Recursive function for a Multi level Marketing Project
Please try it.
public function getSubMembers($memberID, $tableName = 'member_master') {
$data = arra[enter image description here][1]y();
$strQuery = "select MEMBER_ID,LOGIN_NAME,FIRSTNAME,LASTNAME,SPONSOR,PARENT_ID,PAYMENT_STATUS from member_master where SPONSOR='" . $memberID . "' order by POSITION ";
$result = mysqli_query($this->conn, $strQuery);
if (mysqli_field_count($this->conn)) {
while ($row = mysqli_fetch_assoc($result)) {
$data[$row['LOGIN_NAME']] = $row;
foreach ($data as $values) {
$data[$values['LOGIN_NAME']]['SUB'] = $this->getSubMembers($values['LOGIN_NAME']);
}
}
}
return $data;
}

Codeigniter foreach Undefined variable Update Data In

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.

search function using framework codeigniter, php

my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.
my search controller function
public function search()
{
$li = $this->session->userdata('logged_in');
$id = $this->session->userdata('idnumber');
if($li == TRUE)
{
$this->load->model('users_model');
$this->load->helper('smiley');
$this->load->library('table');
$image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array = $this->table->make_columns($image_array, 8);
$image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array2 = $this->table->make_columns($image_array2, 8);
$this->data['smiley_table1'] = $this->table->generate($col_array);
$this->data['smiley_tables'] = $this->table->generate($col_array2);
if($this->input->post())
{
$search = $this->input->post('search');
$memb = $this->users_model->search($search);
$usersearch = $this->users_model->search($search);
$grpsearch = $this->users_model->searchgrp($search);
redirect ('home/profile/'.$memb);
}
}
}
My users_model model
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
if($hehe==NULL)
{
echo $error; exit;
}
else
{
return $memb;
}
}
Always check the varibles using in condition are declared before the condition.
If it doesnt pass the condition what would be the output
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = null;
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
$hehe = $query1->result_array();
if($hehe==NULL)
{
return $error;
}
else
{
return $memb;
}
}
Make sure the variables that youll be using in queries are set. Trap before executing queries.
Also, use Query Builder (Active Record) when you can.
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = '';
if ($query->result_array() > 0) {
foreach ($query->result_array() as $row) {
$memb = $row['idnumber'];
}
if ($memb) {
$this->db->select("*");
$this->db->where("idnumber", $memb);
$query1 = $this->db->get("users");
//$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
return ($hehe) ? $memb : false;
}
return false;
}
$error = 'There is no record for the one you searched. Please go Back.';
return $error;
}

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();
}
}
?>

Update specific row using codeigniter?

I am trying to update specific row of table in codeigniter. But my code update all rows.
Model:
function edit_profile($array){
$email=$array['email'];
$this->db->select("email");
$this->db->from("user");
$this->db->where('email', $email);
$query=$this->db->get();
if ($query->num_rows() == 1){
$this->db->update('user',$array);
return 1;
}else{
return $query->num_rows();
}
}
Controller:
public function edit()
{
$name = $this->input->post('user_name', TRUE);
$email=trim($this->session->userdata('email'));
$array = array('user_name'=>$name,'email'=>$email);
$result = $this->edit_profile_model->edit_profile($array);
echo $result;
}
And in new page it show the echo value 1.
How to update specific row? why $query->num_rows() return 1 and update all rows?
Try using a where condition to update a specific record
$updateData = [
'user_name' => $array['user_name'],
];
$this->db->where('email', $array['email']);
$this->db->update('user', $updateData);
function edit_profile($username, $email){
$this->db->set('username',$username);
$this->db->where('email', $email);
$this->db->update('user');
$result = $this->db->affected_rows();
return $result
}
Controller
public function edit()
{
$name = $this->input->post('user_name', TRUE);
$email=trim($this->session->userdata('email'));
$result = $this->edit_profile_model->edit_profile($name,$email);
echo $result;
}
In your model just use
$update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value');
$this->db->where('email', $email);
$this->db->update('my_table', $update_data);

Categories