I have a problem with the database.
I have 3 tables.
a) notebook
b) category_notes
c) domains
I have domain.id in the 'domains' table.
In the 'notes' table, I have domain_id and notes_category_id.
In the 'notes_category' table, I have id, notes_category_name
Script action: Saves notes for a given domain.
Everything works correctly - the data is saved and read.
Now I would like to add a note type, I have already done this functionality - but I'm showing the ID of the note type and not its name.
Of course, I have a relationship that's dear.
Controller domains.php
public function notes($id)
{
$this->load->model('admin/notes_model');
$result = $this->notes_model->get_by_domain_id($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - domains_category_model.php
public function get($id = false)
{
if ( $id == false) {
$q = $this->db->get('notes_category');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes_category');
$q = $q->row();
}
return $q;
}
Controller - notes_category.php
public function get($id = false)
{
$result = $this->notes_category_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Controller - notes.php
public function get($id = false)
{
$result = $this->notes_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
Model - Notes_model.php
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('notes');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes');
$q = $q->row();
}
return $q;
}
public function get_by_domain_id($id)
{
$this->db->where('id_domain_rel', $id);
$q = $this->db->get('notes');
$q = $q->result();
return $q;
}
If you want to receive the name without conflict , use alias in select.
$this->db->select('n.* , n.id id_noted')->from('notes n')->get()->result();
$this->db->select('*, id id_noted')->from('notes')->get()->result();
I hope I understand what you want to do.
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;
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;
}
Trying to get result from database by calling stored procedure in code-igniter. Below is my controller & model. Model is working fine getting result from stored procedure and can show the result when I do print_r() in model. But I am not able to get the result of query in controller method. Not able to understand the error here.
Controller:
function weeklysalesstatus()
{
$this->data['rpt'] =$this->reports_model->weeklysalesstatus(true);
$this->data['page_title'] ='Weekly Sales Status [ Residential ]';
print_r("here...");
$this->page_construct('reports/weeklysalesstatus', $this->data);
}
Model:
public function weeklysalesstatus($sender = false)
{
$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
if(count($query) > 0) {
foreach (($query->result()) as $row) {
$data[] = $row;
//var_dump($data);
}
return $data;
} else {
print_r("no rows");
}
return $data;
}
The issue is somewhere in this line : $this->data[$rpt] = $this->reports_model->weeklysalesstatus();
But can't figure it out.
Thanks
EDIT:
These errors arise only when I call Stored Procedure CALL. For other queries it work perfectly. So is there any settings I need to do on Codeigniter DB class for calling a Stored procedure thru active record?
$q = $this->db->query('CALL GetWeeklySalesStatus');
Change this line to
$q = $this->db->query('CALL GetWeeklySalesStatus')->result();
In Model
public function weeklysalesstatus()
{
//$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
if(!empty($query))
{
$data = $query->result_array();
return $data;
}
else
{
return FALSE;
}
}
In Controller
function weeklysalesstatus()
{
$rpt =$this->reports_model->weeklysalesstatus();
if ($rpt == FALSE) {
echo "Empty";
} else {
$data['rpt'] = $rpt;
}
$data['page_title'] ='Weekly Sales Status [ Residential ]';
$this->page_construct('reports/weeklysalesstatus', $data);
}
Modify the controller like the following hope it works
function weeklysalesstatus()
{
$this->data['rpt'] =$this->reports_model->weeklysalesstatus();
$this->data['page_title'] ='Weekly Sales Status';
$this->page_construct('reports/weeklysalesstatus', $this->data);
}
your model
public function weeklysalesstatus($sender = false)
{
$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
$data["result"] = array();
if(count($query) > 0) {
foreach (($query->result()) as $row) {
$data["result"][] = $row;
//var_dump($data);
}
}
return $data;
}
your controller
function weeklysalesstatus()
{
$data['rpt'] =$this->reports_model->weeklysalesstatus(true);
$data['page_title'] ='Weekly Sales Status [ Residential ]';
print_r("here...");
$this->page_construct('reports/weeklysalesstatus', $data);
}
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']
}