How to select rows from a table using join query in codeigniter? - php

I have two table competition_registration and competition_schedule. I have wa select query for selecting rows from competition_registration. this is my code as shown below:
$this->db->select('competition_registration.permenent_registration_number');
$this->db->from('competition_registration');
$where = "permenent_registration_number is NOT NULL";
$this->db->where($where);
$this->db->join('competition_schedule', 'competition_schedule.competition_schedule_id = competition_registration.competition_schedule_id');
$this->db->join('competition_schedule', 'competition_schedule.period_id = 6');
$this->db->join('competition_schedule', 'competition_schedule.competition_level_id = 3');
$query = $this->db->get(); echo $this->db->last_query();exit;
But this is showing an error. Can anyone check this query and rectify it for me ?
I want to select the columns from the both tables with same competition_schedule_id and competition_level_id equal to 3 and period_id equal to 6 from competition_schedule table .

Hope this will help you :
Put competition_schedule.period_id = 6 and this competition_schedule.competition_level_id = 3 in where clause like this :
$this->db->select('competition_registration.permenent_registration_number');
$this->db->from('competition_registration');
$this->db->join('competition_schedule', 'competition_schedule.competition_schedule_id = competition_registration.competition_schedule_id');
$where = "competition_registration.permenent_registration_number is NOT NULL";
$this->db->where($where);
$this->db->where('competition_schedule.period_id' , '6');
$this->db->where('competition_schedule.competition_level_id','3');
//$this->db->join('competition_schedule', 'competition_schedule.period_id = 6');
//$this->db->join('competition_schedule', 'competition_schedule.competition_level_id = 3');
$query = $this->db->get();
echo $this->db->last_query();
exit;
For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Related

Impliment Select Count in Codeigniter and echo the Results

I have the following query I will like to implement in CodeIgniter:
SELECT COUNT(*) FROM tickets WHERE status = "open";
The result returned will be '1' and I will like to echo the result. I have the current code query:
$this->db->count_all_results();
$this->db->select('*');
$this->db->where('status', 'Open');
$this->data['opentickets'] = $this->support_m->get();
I am trying to display the count result within the view. Any advice on how I can do this?
Please try below code to get the count the number of row with open status.
$this->db->where("status", 'Open');
$query = $this->db->get("tickets");
$this->data['opentickets'] = $query->num_rows();
Or You can use this one
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->row_array()['COUNT(*)'];
You can run query in codeigniter :
$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->result_array();

How to get count of the sql search query?

How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;

Displaying fields with values only with foreach limit

I have db structure
id | ref_id | amenity1 | amenity2 | amenity3 | amenity4 | amenity5
Each amenity column has values 1 or 0.
When a search is made, I need to display the field that has values not equals to zero (value != 0).
I know I can do this in VIEW as
if($data->amenity1 == 0) echo '';
But I need it to be automated with limit.
My code for MODEL is
function select_all_active_amenities($for_id){
foreach($for_id as $id){
$prop_id = $id->vbc_item_id;
}
$this->db->select('*');
$this->db->from('vbc_property_amenities');
$this->db->where_in(array('v_ref_id'=> $prop_id));
$this->db->limit(5);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Please help
Add the "DISTINCT" keyword to a query before Select ,to get unique records
$this->db->distinct();
Hope it may help you out!
you just remove empty column using custom array build
function select_all_active_amenities($for_id){
foreach($for_id as $id){
$prop_id = $id->vbc_item_id;
}
$this->db->select('*');
$this->db->distinct('v_ref_id');
$this->db->from('vbc_property_amenities');
$this->db->where_in(array('v_ref_id'=> $prop_id));
// $this->db->limit(5);
$query = $this->db->get();
$result1 = $query->result_array();
//fetch the value as array
//and this query for fetch the column name what you have right now an where not equal to id and ref_id
$this->db->query("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='vbc_property_amenities' AND COLUMN_NAME !='id' AND COLUMN_NAME != 'ref_id AND COLUMN_NAME !='id' ")->result_array();
$query = $this->db->get();
$result2 = $query->result_array();
foreach($result as $key1=>$row1)
{
foreach($result2 as $key2=>$row2)
{
if($row2 ==$row1[$row2] && $row1 ==0)
{
unset($result[$key1][$row2]);
}
}
}
//$result only have the where value=1
return $result;
//you just limit the five in your view page
}

CodeIgniter Active Record multiple WHERE clauses

I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();

Zend query update

I want t make a zend update query.
I was looking at zend documentation but I didn't see any example like the one I want.
The question its if may someone could help me with the query.
I want to make the next query:
Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)
Simplest way to run any complex query. There can be some better ways to do same.
$sql = "Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)";
$query = $this->getDbTable()->getAdapter()->query($sql, $data);
$query->execute();
Try this too
$data = array { 'active' = '0' };
$where = "id in (SELECT idCar FROM UserCars Where idUser=3)";
$db->update($data, $where);
Perhaps more Zendy way:
$idUser = 3;
$sub_select
= $db->select()
->from('UserCars', array('id'))
->where('idUser = ?', $idUser);
$updated_rows
= $db->update('cars',
array('active' => 0),
"id IN ($sub_select)"
);

Categories