I have been getting this error Undefined variable nik when I want to access the array from for each this array getting information from the database
View
foreach ($data as $user) {
$id_user = $user->id_user;
$nama = $user->nama;
$nik = $user->nik;
echo $nik;
$alamat = $user->alamat;
$no_telp = $user->no_telp;
$pass = md5($user->password);
$name = $user->name;
}
Controller
public function user_akun_detail() {
$id_user = $this->input->get('id');
$detail['data'] = $this->Madmin->user_akun_detail($id_user);
$this->load->view('admin/Detail_user_pemilih', $detail);
$this->session->set_flashdata('id_user', $id_user);
}
Model
public function user_akun_detail($id_user) {
$this->db->select('*');
$this->db->from('akun');
$this->db->join('detail_user', 'detail_user.id_user = akun.id_user');
$this->db->join('image_user', 'akun.id_user = image_user.id_user');
$this->db->where('akun.id_user', $id_user);
$query = $this->db->get();
return $query->result();
}
Related
i want to join the data of 2 tables in 1 table and display the data of 2 tables. I already have function that load the one table. can someone modify this function code to get the 2 tables ? im just a beginner in php.
Model_users.php
public function getUserGroup($userId = null)
{
if($userId) {
$sql = "SELECT * FROM user_group WHERE user_id = ?";
$query = $this->db->query($sql, array($userId));
$result = $query->row_array();
$group_id = $result['group_id'];
$g_sql = "SELECT * FROM groups WHERE id = ?";
$g_query = $this->db->query($g_sql, array($group_id));
$q_result = $g_query->row_array();
return $q_result;
}
}
Controller User.php
public function index()
{
if(!in_array('viewUser', $this->permission)) {
redirect('dashboard', 'refresh');
}
$user_data = $this->model_users->getUserData();
$result = array();
foreach ($user_data as $k => $v) {
$result[$k]['user_info'] = $v;
$group = $this->model_users->getUserGroup($v['id']);
$result[$k]['user_group'] = $group;
}
$this->data['user_data'] = $result;
$this->render_template('users/index', $this->data);
}
Here you go
$this->db->select("ug.*,g.*");
$this->db->from("user_group ug");
$this->db->join('groups g', 'ug.user_id = g.group_id');
$this->db->where("ug.user_id",1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
} else {
$result = null;
}
return $result;
public function get_inner_refs($referral){
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$referral = $row['username'];
$referrals[] = $row['username'];
$this->get_inner_refs($referral, $referrals);
}
}
return $referrals;
}
here i want to return an array of all users but it returns only first value from database,
Try to create a private variable to assign to and create a get method:
private $referrals;
public function get_inner_refs($referral)
{
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$username = $row['username'];
$this->referrals[] = $username;
$this->get_inner_refs($username);
}
}
return $this;
}
public function getReferrals()
{
return $this->referrals;
}
Then to use:
$referrals = $ReferralClass->get_inner_refs($username)->getReferrals();
i think you can edit like this
public function get_inner_refs($referral='',$referrals = []){
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$referral = $row['username'];
$referrals[] = $row['username'];
$this->get_inner_refs($referral, $referrals);
}
}
return $referrals;
}
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;
}
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']
}
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();
}