When i using this below without where condition query i get the values
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$query = $this->db->get();
return $query->result_array();
}
Output:
2323232,262626,32323,,26262623265265,
When i using this below with where condition query i can't get all the values it show me just one value but when i tried this sql in my mysql server its show me 2 values and here i get only one value.
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$this->db->where('staff_roles.role_id',2);
$query = $this->db->get();
return $query->result_array();
}
Output:
262626,,
one value is missing..
Here i give you controller code:
$all_teacher = $this->teacher_model->get_teacher_number();
$x = '';
foreach ($all_teacher as $val) {
$smsid= $val["id"];
$number = $val["contact_no"];
$x = $x.$number.","; //number separated by comma
}
echo "<pre>";
print_r($x);
exit();
Related
Need to bind Page drop-down conditionally on base of 'Content' table. Page titles are stored in an associative array and 'Content' table have page code stored in it. Here is the code
Function which return page titles
public function getPageTitles(){
$pageTitles = array("Home"=> "Home",
"AboutUs"=> "About Us", //AboutUs will save in database as pageCode
"Features"=> "Features",
"ContactUs"=> "Contact Us");
return $pageTitles;
}
Function which checks if page have content or not:
public function getPageTitlesWithNoContent()
{
$pageTitles = $this->getPageTitles();
$this->db->distinct('pageCode');
$this->db->select('pageCode');
$this->db->from('content');
$this->db->where('status', 1);
$data = $this->db->get();
$queryResult = $data ? $data->result_array() : 0 ;
$emptyPageTitle = array();
foreach($pageTitles as $x => $x_value)
{
$hasContent = in_array($x, $queryResult);
if (!$hasContent){
$emptyPageTitle[$x] = $x_value;
}
}
return $emptyPageTitle;
}
This function is returning all page titles.. new to php no idea what is wrong
Check name fields in table is same? With Uppercase first char?
Also change your code in this loop:
foreach($pageTitles as $x => $x_value)
{
if (in_array($x, $queryResult)){
$emptyPageTitle[$x] = $x_value;
}
}
I remove ! negative in check condition
#NMathur I think you almost got it. Made some changes for you in that code, Check it.
public function getPageTitlesWithNoContent() {
$pageTitles = $this->getPageTitles();
$this->db->select('pageCode');
$this->db->from('content');
$this->db->where('status', 1);
$this->db->group_by('pageCode');
$query = $this->db->get();
$queryResult = array();
foreach ($query->result_array() as $row) { // This loop should need to form an array based on query result
$queryResult[$row['pageCode']] = $row['pageCode'];
}
$emptyPageTitle = array_diff_key($pageTitles,$queryResult); // Compares the keys from array1 against the keys from array2 and returns the difference
return $emptyPageTitle;
}
As #TamilvananN guided, I printed the queryResult and tried this workaround:
foreach($pageTitles as $x => $x_value)
{
foreach ($queryResult as $item)
{
if (!($x == $item['pageCode'])){
$emptyPageTitle[$x] = $x_value;
}
}
}
It is working, but as you can see this has loop in a loop .. that can be very costly .. can you please share any fast way to compare the results.
I want to know how to compare these 2 outputs http://prntscr.com/bx7ay9 and return the highest value in the array ?
MY MODEL CODES
$this->db->select('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$return[] = $row->additional;
}
return $return;
Just use select_max in query to get highest value and use row() to fetch single row without using foreach loop as
$this->db->select_max('additional AS max_value');
$this->db->from('order_detail');
$this->db->where('order_id', $id);
$query = $this->db->get();
$ret = $query->row();
return $ret->max_value;
$this->db->select('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$return[] = max($row->additional);
}
return $return;
Just used select_max of Codeigniter Query Bulder Class.
Model,
public function get_max ($id){
$this->db->select_max('additional');
$this->db->where('order_id',$id);
$query = $this->db->get('order_detail');
return $query->row();
}
In Controller,
$max = $this->Model->get_max($id);
echo $max['additional']; // Display and see the value.
If you want to get the max number then use select_max(),
$this->db->select_max('additional');
$this->db->from('order_detail');
$this->db->where('order_id',$id);
$query = $this->db->get();
if($query->num_rows()){// check if data is not empty
return $query->row()->additional;
}
return false;// you can return false here
And if you want to get the result array for another use then, you can use max() with $this->db->select('additional'); with your code to get the max number in array.
I want to pass multiple id in where condition how to do it?
A query fetches org_id from database.
now I want to pass that in my where condition, so how to do it?
I tried foreach loop:
Below is my code:
$devices = $this->activation_m->get_activated_devices();
$org_id = array(); // create org_id array
foreach ($devices as $row)
{
$org_id[]= $row['org_id']; // assign ids into array
//$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
}
//Now $org_id is array
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
echo $this->db->last_query();
Model Code
public function get_activated_devices()
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.site_key = activation.site_key');
$this->db->from('activation');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
public function get_company($org_id)
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
now currently my query passes only one org_id , I want to pass all the org_id I get from my first query.
You can use where_in from active-records codeigniter
as
$devices = $this->activation_m->get_activated_devices();
$org_id = array();
foreach ($devices as $row)
{
$org_id[] = $row['org_id'];
}
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
echo "<pre>";
print_r( $companys->result());
echo "</pre>";
}
And for Model
public function get_company($org_ids = array())
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where_in('company.id', $org_ids ); //this is condition
$this->db->from('company');
return $this->db->get();
}
You can use codeigniter's $this->db->or_where() for the purpose. Just traverse the array of organization id's and apply or_where conditions.
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
foreach($org_id as $org)
{ // where $org is the instance of one object of active record
$this->db->or_where('company.id',$org);
}
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
Another way to do this is to make a custom query by traversing the array like this and appending where clauses in string.
Am creating a web app in codeigniter, it retrieves posts from the database for a user based on the keywords they have added to their keyword list.
Am trying to retrieve all the posts with the users keywords but,it returns only one post whereas there are mutliple matches for the keywords in the posts
This is are the functions from my model
This function retrieves the users keywords
function user_keywords($user_id) {
//Get all the user keywords
$q = $this->db->select('keywords')
->from('keywords')
->where('U_id',$user_id)
->get();
$words = $q->result_array();
return $words;
}
This function retrieves the posts
function get_latest_pheeds() {
$this->load->helper('date');
$time = time();
$keyword = $this->user_keywords($this->ion_auth->user_id);
foreach($keyword as $word) {
$q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments')
->from('pheeds')
->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left')
->like('pheed',$word['keywords'])
->order_by('datetime','desc')
->get();
}
$rows = $q->result_array();
return $rows;
}
And my controller encodes it in JSON
function latest_pheeds() {
if($this->isLogged() == true) {
$this->load->model('pheed_model');
$data = $this->pheed_model->get_latest_pheeds();
echo json_encode($data);
}
return false;
}
I really appreciate the help
I think (but it is hard without testing) that the problem comes form the function get_last_feeds :
you query is built over each loop's iteration, but you fetch the results after. I think you get the results only for the last query (if you don't fetch the results on each loop, the next iteration overwrites the original request without fetching it's results).
I would make something like this :
<?php
function get_latest_pheeds() {
$rows = array(); // The final result, an array of rows
$this->load->helper('date');
$time = time();
$keyword = $this->user_keywords($this->ion_auth->user_id);
foreach($keyword as $word) {
$q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments')
->from('pheeds')
->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left')
->like('pheed',$word['keywords'])
->order_by('datetime','desc')
->get();
$rows[] = $q->result_array();
}
return $rows;
}
original I thought the $query is array object, since I can print these data to a list table like this.
my controller code
$data['dates'] = $this->calendar_model->get_cal_eventDate();
$this->load->library('table');
$this->load->view('my_test', $data);
my view code
echo $this->table->generate($dates);
but when I changed my code to try to print $tbDataArr via foreach. It didn't work. How can I convert the $query result (eventDate field values) to array object.
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
return $query;
}
$tbDataArr = $this->calendar_model->get_cal_eventDate();
foreach ($tbDataArr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Are you using CodeIgniter? If you are you can do this
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
$result = $query->result_array();
return $result;
}
If not need more info about what your doing with your PHP.
Codeigniter, right?
Use the $query->result_array() method.
see more here:
http://codeigniter.com/user_guide/database/results.html
//Convert $query->result_array() to $query->result() after add new field in array
$result = $query->result_array();
for($i=0;$i<$query->num_rows();$i++){
//insert new field in array (test_field with value 1)
$result[$i]+=array('test_field'=>1);
$result[$i] = (object)$result[$i];
}
return $result; //equivalent to return $query->result() without new field;