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
}
Related
I have some problem with MySQL syntax in CI this is my table
id_data | nama_depan | nama_belakang | gender | luas_lantai | jenis_lantai
| status
I already tried code bellow and show error
Message: Object of class CI_DB_mysqli_result could not be converted to
int
public function getStatus1($status){
$str = "SELECT count(status)
FROM data_latih WHERE status = 1 ";
$str1 = "SELECT count(status)
FROM data_latih ";
$query = $this->db->query($str);
$query1 = $this->db->query($str1);
return $query / $query1;
}
expected output is decimal number of count status = 1 divided by count all rows
EDIT : Found the answer, thank you so much guys i'm new in programming so bear with me :)))
The problem is that you are trying to divide an Object by the Object which does not make any sense, Try this CI way,
public function getStatus1($status){
$this->db->count_all_results('data_latih ');
$this->db->where('status ', '1');
$this->db->from('data_latih ');
$result1 = (float)$this->db->count_all_results();
$result2 = (float)$this->db->count_all('data_latih');
return $result1 / $result2 ;
}
You can actually handle this with a single query:
SELECT
SUM(status = 1) / COUNT(status) AS result
FROM data_latih;
Regarding your PHP code, I would recommend assigning aliases to the various counts, and then accessing those aliases.
$sql = "SELECT SUM(status = 1) / COUNT(status) AS result FROM data_latih";
$result = $this->db->query($sql);
if ($row = $result->fetch_array()) {
echo $row['result'];
}
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
I have a country restriction on products in my shop and a slow query to select all country-columns with a value of 1 from the table product.
I select all active countries in my shop:
$ids = select id from country where active = 1;
Then I select all countries, which are active in a product:
foreach $ids as $id { select ctr_{$id} from product where ctr_{$id} = 1;
Is there a faster way to get the columns starting with ctr_* and which value is equal to 1?
Try the following
// first get the columns
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='product' AND COLUMN_NAME LIKE 'ctr_%'";
$result = mysqli_query($conn, $query) or trigger_error(mysqli_error($conn),E_USER_ERROR);
$cols = Array();
while($row = mysqli_fetch_row($result)) $cols[] = $row[0];
// then build the query
if(count($cols))
{
$query = 'SELECT '.implode(',', $cols).' FROM product WHERE 1 IN ('.implode(',',$cols).')';
$result = mysqli_query($conn, $query) or trigger_error(mysqli_error($conn), E_USER_ERROR);
while($row = mysqli_fetch_assoc($conn,$result))
{
// do something with the row
// ....
}
}
so far i made this OOP class but it not returning any values, any help guys i realy need it
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT * FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_num_rows($query);
return true;
}
else{
return false;
}
}
You are not returning the value of $count.
Try return $count;
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT * totalrows FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_num_rows(sql );
return $count;
}
else{
return false;`enter code here`
}
}
or you can use:
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT count(*) as counted totalrows FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_result($query,0,"counted");
return $count;
}
else{
return false;
}
}
You don't need to fetch all data if the only goal is to count rows. You can use:
SELECT COUNT(column_name) FROM table_name;
This returns the number of values of the specified column.
Or you can use:
SELECT COUNT(*) FROM table_name;
which returns number of rows from selected table.
Or you can use:
SELECT COUNT(DISTINCT column_name) FROM table_name;
which returns the number of distinct values of the specified column.
The way you are preforming to count rows in table is kind of unnecessary overkill.
Just want to get user_id and review_text values from table reviews, select name and city from table users where id=user_id and then return review_text, username and city to controller. Please help me.
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1:
I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city. Please check the code below:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
i think you need to use join query in SQL. if you use this code you can access to what you want
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link
i hope that this code solve your problem
UPDATE
in your new question. your code have a little buggy. you need to write your select in this way
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; any column name separate with each other by , only (not by ' and ,)
in codeigniter documentation wrote that
Permits you to write the SELECT portion of your query:
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
You can get the value for user_id using:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get(); to get the values of the second query as well. Once you get the values inside a variable you can iterate it. Something like:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}