i want to count the different rows in CodeIgniter...
I have a table like this
NAME | ZIPCODE
Mike | 12345
Marc | 51233
TEST | 12345
Now i want a Result of "2" cause there 2 different Zipcodes.
I tried so much, but dont get this :(
$this->db->select('zipcode, count(*)');
$getAll = $this->db->get('ads');
echo $getAll->num_rows();
but dont get result of or anything... idk how i can make this.
Please help
//EDIT:
Okay i found it. Sorry for Question. Here is the Answer
$this->db->distinct();
$this->db->select('zipcode');
$getAll = $this->db->get('ads');
echo $getAll->num_rows();
you can use this:
$query = $this->db->query('select count(1) as x from your_table_name group by zipcode');
$row= $query->row();
$x = $row->x;
You could use group_by() in your query as follows.
$this->db->select('zipcode', 'count(*) as totalcount');
$this->db->group_by('zipcode');
$this->db->get('ads');
Related
Hi I am running into a unique problem. I have database structure of sales order and comparision tables like below.
enter image description here
There will be more records in comparision table.
Basically I want to get the result like the pictures belows. Note: the AFFID can be any random number.
I haven't be able to think of a good way to call SQL. I feel like I have to call SQL then create a new Array that has structure
[
'campaign_left',
'campaign_right'
'Comparision_id'
]
to be able to achieve this.
I think I can get the answer by doing this :
step 1 : get the uniqueAFFIDS array = [1020,1040,1028]
step 2 : sort the uniqueAFFIDS array = [ 1020,1028,1040]
ForEach Comparisions as Comparision
------ ForEach uniqueAFFIDS as uniqueAFFID
------------- $total left = Select Where campaign_id = comparision->campaign_id-left && affid = uniqueAFFID ;
------------- $total right = Select Where campaign_id = comparision->campaign_id-right && affid = uniqueAFFID ;
Then i just display it.
I wonder if anyone has good idea other that this.
Thanks.
I have two tables- One with attendance details and another one with student detail. The following are the table structures:
tbl_attendance
aid date attendance
1 2017-03-09 5,6,9
2 2017-04-06 12,6,10
tbl_students
student_id name
5 John
6 Bryan
9 Anna
10 Mathew
12 Susan
Now, I want to display the names of the absentees in the view as something like say. for example:
Date Absentees
2017-03-09 John, Bryan, Anna
2017-03-06 Susan, Bryan, Mathew
I was trying to do it with FIND_IN_SET()..but it seems bad luck..Is there a better way to sort this out?
UPDATE
I used this query instead and it echoed only the first id's name in each row...
$query = $this->db
->select("tbl_attendance.*,tbl_students.name")
->from("tbl_attendance")
->join("tbl_students","tbl_students.student_id=tbl_attendance.attendance")
->where('FIND_IN_SET(tbl_students.student_id, tbl_attendance.attendance)')
->GROUP_BY('tbl_students.student_id')
->get()->result_array();
But as there are three numbers separated by commas in each row I want the rest to be echoed as well.
This Works
$query = $this->db
->select("td.Date, GROUP_CONCAT(ts.student_name SEPARATOR ',')")
->from("tbl_students AS ts")
->join("tbl_attendance AS ta","find_in_set(ts.st_id,ta.attendance)<> 0","left",false)
->get();
How about that ?
$query = $this->db
->select("td.Date, GROUP_CONCAT(ts.student_name)")
->from("tbl_students AS ts")
->join("tbl_attendance AS ta","find_in_set(ts.st_id,ta.attendance)","left",false)
->get();
You can try query like this,
SELECT a.`date`,group_concat(s.student_name)
FROM tbl_attendance a,tbl_students s
WHERE FIND_IN_SET(s.st_id, a.attendance) group by `date`;
Description :
FIND_IN_SET that allows you to find the position of a string within a comma-separated list of strings.
Syntax:
FIND_IN_SET(needle,haystack);
Hope this will solve your problem.
Here comma separated category IDs are saved in row 'category' eg., '12,15,7,19'
$category_ID = 15;
$this->db->select('*');
$this->db->from('products');
$this->db->where('FIND_IN_SET("'.$category_ID.'","category") <>','0');
$this->db->where('deleted','0');
$this->db->order_by('product_ID', 'DESC');
I hope this helps CI developers to use FIND_IN_SET.
im using codeigniter and mysql as db. i know might can be solved with mysql query, but if anyone can solve with codeigniter query it will be awesome, otherwise normal PHP mysql query can work also.
I have Table with 3 columns, Well it have many feilds but i have shown here 4 enteries, i didnt wanted to make a table with many columns but only 1 row of data. so instead i went for this style of table as someone suggested me on this website.
Problem is i never worked with this kind of table.
SettingsID SettingsKey SettingsValue
----------------------------------------------------------------------
1 | facebookLink | facebook.com
2 | twitterLink | twitter.com
3 | youtubeLink | youtube.com
4 | googlePlusLink | googleplus.com
Now i want to run a query that should return me rows in to columns. i searched over net and found some solutions but i am not good with this new queries.
I suppose this guy had same kind of problem like i have but in his case he has same value repeated in column, where my SettingsKey has all the values unique.
Here is the link to similar kind of question :
mysql select dynamic row values as column names, another column as value
i cant understand his query and that query i am not sure if is any use of me.
Please can anyone help me build a query to return row values of SettingsKey as columns.
This should work:
<?php
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', USERNAME, PASSWORD);
$stmt = $db->query("SELECT SettingsKey, SettingsValue FROM Settings");
$links = array();
while($row = $stmt->fetch()) {
$links[$row['SettingsKey']] = $row['SettingsValue'];
}
$db = null;
This code queries this table and for each row the SettingsKey and the SettingsValue will be shown.
Now you can get the facebookLink like this:
echo $links['facebookLink'];
Hope this helps.
$sql = "
SELECT SettingsKey, SettingsValue
FROM Settings
";
$q = $this->db->query($sql);
return $q->result_array();
Go to your controller
$data['settings'] = $this->model_selection->your_function();
$this->load->view('example', $data);
And lasty on your view
<?php if(!empty($settings)): ?>
<?php foreach($settings as $k => $v): ?>
//print your staff in here mix with html and go crazy
<?php endforeach; ?>
<?php endif ?>
$this->db->select('*')->from('myTable')->where('name',$user_name)->get()->results_array();
If i do this after the query above,
echo $this->db->count_all_results();
Even though myTable has rows :
Table: myTable Column1 name: id Column2 name: name
row1 - 1 row1 - peter row2 - 2 row2 - peter
It would echo 1, although there are 2 peters. My thoughts is that i returned the results as an array.
How should i return the results? Active_records class does not show how to. It only shows another format where i type in the query myself manually. I don't like doing queries like that.
Although might i ask for professionals' opinion. Which is better. The way im doing it or the examples in active records like,
$query = $this->db->get('mytable');
Although they dont show an example where you specifically select column names.
You should do it in 2 steps.
$query = $this->db->select('*')->from('myTable')->where('name',$user_name)->get();
$result = $query->result_array();
$countResult = $query->num_rows();
I want to count the number of positive, neutral, negative feedbacks.
I have those 3 queries that look very much alike. Is there a way I can put all this into one query? Or is it the most concise way of doing it?
Thanks in advance, regards.
$total_positive_seller = mysql_num_rows(mysql_query("SELECT id FROM feedback
WHERE seller='$user' AND feedback='positive'"));
$total_neutral_seller = mysql_num_rows(mysql_query("SELECT id FROM feedback
WHERE seller='$user' AND feedback='neutral'"));
$total_negative_seller = mysql_num_rows(mysql_query("SELECT id FROM feedback
WHERE seller='$user' AND feedback='negative'"));
If you just want to count appearances, your use of mysql_num_rows is rather inefficient. It would be better to use the count(*) functionality of MySQL like in the following query:
SELECT feedback, count(*) AS `count`
FROM feedback
WHERE seller='$user'
GROUP BY feedback
This gives you something that should look like the following
feedback | count
----------------
positive | 12
neutral | 8
negative | 3
You can afterwards parse this like any other query-result in a row-wise fashion.
EDIT
If you want to address each entry seperatly in your following code you can use something like the following. After this code you can reference all entries, e.g., by $result['positive'].
$qres = mysql_query( 'SELECT ...' );
$result = array();
while( $row = mysql_fetch_array( $qres ) ) {
$result[ $row['feedback' ] ] = $row['count'];
}