Codeigniter Extracting Data From Database With Function - php

I have to extract two separate pieces of information from my mysql database. I'm having a hard time trying to figure out how to extract two different sets of information via function(s) I'm writing. I'm trying to figure out a solution but I'm not getting it. Below is my syntax so far. My goal here is to get both of the functions (getPrice and getOtherPrice) all within the same function working. If I // one, the other one works. If both are active, just one is working. How would you guys go about correcting this issue, what do you think I'm doing wrong? Thanks Everyone.
function getJoinInformation($year,$make,$model)
{
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$model);
return $data;
}
function getPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}
function getOtherPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}

you are returning only last function result you should put the the result of both function by making an array of $data
function getJoinInformation($year,$make,$model)
{
$data['getPrice'] = $this->getPrice($year,$make,$model);
$data['getOtherPrice'] = $this->getOtherPrice($year,$make,$model);
return $data;
}

You're replacing the result in the variable $data.
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$odel);
$data will always just contain the result of the last function.

Related

Getting compiled WHERE statement from Codeigniter db class

Currently working on a small project with a few different database functions. I am trying to figure out a way to get the compiled Codeigniter WHERE statement.
// Database GET function
function dbGetRow($id, $field)
{
$this->db->select($field)->from('friends');
$query = $this->db->get();
return $query->row_array();
}
// Calling the function
$this->db->where('id', 2);
$value = parent::dbGetRow(null 'id');
What i am trying to figure out is how to get the compiled WHERE statement inside of the dbGetRow() function before performing the query.
Got this working.
the only solution is to use $this->db->get_compiled_select();
private function hasInlineQuery()
{
$string = $this->db->get_compiled_select();
$string = str_ireplace('SELECT *', '', $string);
return $string ? true : false;
}

i want to display value from database in codeigniter but i am getting errror

I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.

Laravel SQL with where not in example and gettype with dynamic value

There is one function for getting all the data from table with one where clause and one with not wherein clause. I am stuck when I am passing data dynamically but when I am hardcoding the data, it is showing me correct data.
Hard-coded Example :
public function getAllTickets($drawId, $existing)
{
$login = [200263129,200263162,200263735,200263752];
$data = $this->select('ticket')
->where('wlf_draws_id', $wlfDrawId)
->whereNotIn('login', $login)
->get();
return $data;
}
Dynamic Example :
public function getAllTickets($drawId, $existing)
{
$login = [$existing];
$data = $this->select('ticket')
->where('wlf_draws_id', $wlfDrawId)
->whereNotIn('login', $login)
->get();
return $data;
}
In variable $existing I am same data as 200263129,200263162,200263735,200263752
But result is varying for both data and hard-coded example is showing me correct result.
Please use this it may help you:
public function getAllTickets($drawId, $existing)
{
$login = explode(',',$existing);
$data = $this->select('ticket')
->where('wlf_draws_id', $wlfDrawId)
->whereNotIn('login', $login)
->get();
return $data;
}

Codeigniter can't fetch all data from database table

I am fetching data from mysql database table in codeigniter using following code
$result = $this->db->get("shipping");
$data = $result->result_array();
But returns no data.
When I apply the limit as
$this->db->get("shipping",1,30)
The code works.
But I want to fetch all result and not by limiting it.
Could anyone please let me know how do I resolve this issue.
Thanks in advance.
have you tried using query rather than get?
$sql = "
SELECT
*
FROM
shipping
";
$query = $this->db->query($sql);
$data = $query->result();
You have not put a return
https://www.codeigniter.com/user_guide/database/results.html
// This will get every result from shipping
public function somefunction() {
$result = $this->db->get("shipping");
return $result->result_array();
}
Controller function
public function somecontrollerfunction() {
$this->load->model('some_model');
$data['shipping'] = array();
$data['shipping'] = $this->some_model->somefunction();
// var_dump($data['shipping']);
$this->load->view('your_view', $data);
}

Codeigniter with Ignited Datatables

I have finally got Codeigniter to work with ignited datatables. Now i have run into a different problem. Can anyone help or tell me if i could run the below query with the datatables plugin for codeigniter.
At present i'm doing it within the controller which is lame i know (this was only for testing)
Controller
$data['query'] = $this->test_queries->list_partners();
foreach($data['query'] as $k => $company){
$data['query'][$k]->partner_contacts = $this->test_queries->get_partner_contacts($company->id);
}
Queries in the Model
function list_partners(){
$this->db->select("company.id,name,general_email,general_phone,market");
$this->db->from("company");
$this->db->join('markets','markets.id = company.market_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function get_partner_contacts($id){
$this->db->select('partner_contacts.id,contact_type');
$this->db->from('partner_contacts');
$this->db->where('company_id',$id);
$this->db->join('department','department.id = partner_contacts.contact_type_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can change the queries in the model as below:
function list_partners(){
$this->datatables->select("company.id,name,general_email,general_phone,market");
$this->datatables->from("company");
$this->datatables->join('markets','markets.id = company.market_id');
return $this->datatables->generate();
}
function get_partner_contacts($id){
$this->datatables->select('partner_contacts.id,contact_type');
$this->datatables->from('partner_contacts');
$this->datatables->where('company_id',$id);
$this->datatables->join('department','department.id = partner_contacts.contact_type_id');
return $this->datatables->generate();
}
You can also use method chaining if you are using PHP 5 or above.

Categories