Is it possible to count all with where condition in codeiginter? - php

I am trying to do something like this
$this->db->count_all("grant_money")->where('id',5);
is it possible ?
if there is any another way to do this please let me know.Thanks
I want to do this query in single line code Like I tried above

You can use it like this.
$this->db->where('id',5);
$this->db->from("grant_money");
echo $this->db->count_all_results();
this will show all the count with where condition.
with single line try like this
echo $this->db->where('id',5)->from("grant_money")->count_all_results();

$this->db->where('id',5)->count_all_results("grant_money");

Is it possible to count all with where condition in codeiginter?
YES
count in CI based on condition
$this->db->from('mytable');
$this->db->where('field_name',"comapre_vale");
$this->db->like('filed_name',"any_value");
$count = $this->db->count_all_results();
echo $count;
An Example :
$this->db->from('tbl_ontime_attendance');
$this->db->where('Card_Number',$value['emp_code']);
$this->db->like('In_Time',"03/2018");
$days = $this->db->count_all_results();
echo $days;
this is a simple and very easy in this function i have shown the use of both the like and where clause , change according to your to your need.

Try get_data function like this :
$count_all = $this->db->get_data("select count(1) from grant_money where id = 5");
echo $count_all;

You may want to look at CodeIgniter Documentation:
Docs>Database Reference>Database Builder Class
E.g.
echo $this->db->count_all_results('my_table'); // Produces an integer, like 25
$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results(); // Produces an integer, like 17
It accepts "Query Builder restrictors" such as where(), or_where(), like(), or_like(), etc.
HTH

yes you can try this may be this will hepfull:-
public function record_count_with_where($table_name,$column_name,$type)
{
$this->db->select($column_name);
$this->db->where($column_name,$type);
$q=$this->db->get($table_name);
$count=$q->result();
return count($count);
}

Related

Laravel WhereIn method in Eloquent not working

I want to use the WhereIn method in Eloquent but it now works like the below function.
<?php
$product = DB::table('product')
->join('supplier','supp_code','=','prod_supplier_code')
->select('product.*','supplier.supp_margin')
->where('prod_seo_title','=',$id)
->first();
$decArr = explode(',',$product->prod_decoration_type);
for($i=0; $i<count($decArr); $i++)
{
if($i==0)
$inString = "'".$decArr[$i]."','";
elseif ($i<(count($decArr))-1)
$inString .= $decArr[$i]."','";
else
$inString .= $decArr[$i]."'";
}
$proDeco = DB::table('decoration')->whereIn('deco_print_type', [$inString])->get();
?>
But In the query, it's displaying like this.
select * from `decoration` where `deco_print_type` in ('\'100109C9\',\'100110B9\',\'100144C9\',\'100186C9\'');
I can't understand why these slashes are coming. Please help me to fix this issue.
The whereIn() method will accept an array of all the items. In your example you are passing an array with one element with all the values already concatenated. Based on your example you only need to pass $decArr
DB::table('decoration')->whereIn('deco_print_type', $decArr)->get();

Filter an array using explode

My question is fairly simple. I have a column in my table labeled 'area' that appears like:
ma_south-coast
ma_south-caost
ma_north
ca_los-angelos
ca_los-angelos
I want to be able to select just the 'ma' ones. I am trying to do something such as:
$res_area = mysqli_query($connection,"select area from users");
while ($row_state = mysqli_fetch_array($res_area)) {
$state_exp = reset(explode("_", $row_state['area']));
}
Printing $state_exp at this point would give me: mamamacaca which is good. But I want to filter so I only get the ma ones.
You can extend your query with WHERE column LIKE "ma%" or check with substr($row_state['area],0,2) if the first two characters are "ma".
You could try this:
$res_area = mysqli_query($connection,"select area from users");
while ($row_state = mysqli_fetch_array($res_area)) {
$state_exp = stristr(reset(explode("_", $row_state['area'])),'ma');
}
You're looking for the LIKE operator. The LIKE operator is used to search for a specified pattern in a column.
Try this:
SELECT * FROM tablename WHERE areaLIKE 'ma_%';
You can read more about the LIKE operator here.
The optimal solution could be :-
$res_area = mysqli_query($connection,"select area from users WHERE area LIKE 'ma_*')";
while ($row_state = mysqli_fetch_array($res_area)) {
if(stripos(reset(explode("_", $row_state['area'])),'ma') !== FALSE)
{
$state_exp = reset(explode("_", $row_state['area']));
}
}

Multiple Like clause CodeIgniter

I'm trying to use multiple 'Like' to look for matches in more than one column.
PHP & mysql:
$area = $this->session->userdata('area');
if($area == 'inbox'){
$this->db->where('receiver',$user);
$this->db->where('receiver_deleted !=','yes');
}elseif($area == 'sent'){
$this->db->where('sender',$user);
$this->db->where('sender_deleted !=','yes');
}
$this->db->like('sender',$k);
$this->db->or_like('msg',$k);
$this->db->from('messages');
$this->db->join('users','users.username = messages.sender','inner');
$this->db->order_by('read','no');
$sql = $this->db->get();
if($sql->num_rows > 0){
return $sql->result();
}else{
echo "failed!";
}
I've also tried:
$this->db->like('sender',$k);
$this->db->like('msg',$k);
the first one nothing happens. The second one only one is executed, in this case the first like. I cannot get both to work at the same time.
Any help will be appreciated,
Mike
for this, you should use Grouping with where clause.
$area = $this->session->userdata('area');
if($area == 'inbox'){
$this->db->where('receiver',$user);
$this->db->where('receiver_deleted !=','yes');
}elseif($area == 'sent'){
$this->db->where('sender',$user);
$this->db->where('sender_deleted !=','yes');
}
$this->db->group_start(); //group start
$this->db->like('sender',$k);
$this->db->or_like('msg',$k);
$this->db->group_end(); //group ed
or_like will produce an OR between the two likes. You can pass an array to like to put multiple with an AND between them.
Have you tried doing:
$this->db->like(array('sender' => $k, 'msg' => $k));
This will search multiple column with OR in between
$this->db->or_like(array('sender' => $k, 'msg' => $k));/* LIKE OR LIKE */
Use this:
$this->db->or_like('location',$this->input->post('searchtext'));
$this->db->or_like('title',$this->input->post('searchtext'));
and you will get exact you want.
You could do something like this:
$this->db->get_where('tablename',$whereclause);
I have the same problem and it worked.

mysql count occurrences of special character in a field

I am wanting to count all occurrences of the # symbol in a field and originally i thought LIKE '%#%' would be the way to go, but if the character appears in the field more than once it only counts it as one.
What other method are there that i could use that would count every occurrence?
Thanks.
EDIT
For anyone needing it, this is what i ended up using that works.
$count = 0;
$sql = mysql_query("SELECT LENGTH(field_name) - LENGTH(REPLACE(field_name,'#','')) AS 'occurs' FROM table_name WHERE field_name LIKE '%#%'");
while ($data = mysql_fetch_assoc($sql)) {
$count += $data['occurs'];
}
echo $count;
select length('aa:bb:cc:dd')-length(replace('aa:bb:cc:dd',':',''));
source: http://lists.mysql.com/mysql/215049
You could make this even simpler by using the ``substr_count function in php. see below.
$message = $row['themessage'];
echo substr_count($message, '#');
what this will return is the number of times # has occurred in your "themessage" field in your database.

problem with MySQL queries in PHP

I have the following queries that work perfectly in MySql:
SELECT * FROM rapoarte WHERE nrtel LIKE '0256%' OR nrtel LIKE '0356%
SELECT * FROM rapoarte WHERE nrtel NOT LIKE '07%' AND nrtel NOT LIKE '0256%' AND nrtel NOT LIKE '0356%'
SELECT * FROM rapoarte WHERE nrtel LIKE '07%'
in PHP they will result the following:
results just for LIKE '0256%'
no results
inclomplete results. i have phone numbers that start with 076, 075 and it only shows the numbers that start with 076.
Anyone know why?
thanks,
Sebastian
EDIT
here is the code:
$select_int= mysql_query("SELECT * FROM rapoarte WHERE nrtel LIKE '0256%' OR nrtel LIKE '0356%'");
$local = mysql_fetch_array($select_int);
echo "<table align='center' border='0' width='600'><tr><td><b>Ziua</b></td><td><b>Ora</b></td><td><b>Trunchi</b></td><td><b>interior</b></td><td><b>Durata</b></td><td><b>Numar Format</b></td></tr>";
while($int = mysql_fetch_array($select_int)) {
echo "<tr>
<td>".$local['ziua']."</td>
<td>".$local['ora']."</td>
<td>".$local['linie']."</td>
<td>".$local['interior']."</td>
<td>".$local['durata2']."</td>
<td>".$local['nrtel']."</td></tr>";
}
echo "</table>";
Again...
Here $local = mysql_fetch_array($select_int); you discard your first line. You fetch it and you don't use it.
The second problem is here $int = mysql_fetch_array($select_int). You actually want $local = mysql_fetch_array($select_int) because that's what you use in the while block.
You seem to be discarding the first result on the 2nd line with
$int = mysql_fetch_array($select_int);
...not to mention the query in the code snippet you edited in doesn't actually match any of the three you claim work correctly.
You're not iterating over the results, rather, you're just getting the first one.
while(($local = mysql_fetch_array($select_int)) != null){
// $local contains 1 result row
}
The query you're using in your PHP script doesn't use "LIKE"

Categories