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;
}
Related
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();
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.
I have two tables set up in a database, a "Posts" table and an "Images" table. Rows in the Images table are related the Posts table with an ID so at any time there can be a single Post, but multiple rows from the Image table assigned to it. I don't want to run a join as it results in duplicate content from the Posts table when it adds the Posts to each row in Images
I'd like to be able to iterate through this content in the following manner:
foreach(posts as post)
{
foreach(post['image'] as image)
{
// something here
}
}
In Django, this can be set up in my model through the following:
class Posts
# something here
def get_images(self):
return self.images_set.select_related('post')
Is it possible to have a similar set up in a Codeigniter model using their Active Record? The only way I've been able to it was to query the Posts table, loop through results and query the Images table while inside the loop. Then set everything up as a new array and send it to the controller. But that doesn't seem efficient at all. Here's an example:
public function get_posts()
{
// Grab all posts
$query = $this->db->get('posts_table');
$results = $query->result_array();
// Arrays for storage
$photoArray = array();
$postsArray = array();
$postsResultArray = array();
$postsResultArray[] = $results;
// Query photos in DB
foreach($photoResultArray[0] as $row)
{
$query = $this->db->get_where('uploads', array('tableName' => 'posts_table', 'recordNum' => $row['num'], 'fieldname' => 'images'));
$results = $query->result_array();
foreach($results as $row)
{
$photoArray[] = $row;
}
}
// Associate each post with photo if available
foreach($postsResultArray[0] as $post)
{
foreach($photoArray as $row)
{
if($row['recordNum'] == $post['num'])
{
$post['image'] = $row['urlPath'];
}
}
$postsArray[] = $post;
}
return $postArray;
}
Try this
public function get_posts()
{
// Grab all posts
$query = $this->db->get('images');
$results = $query->result_array();
// Arrays for storage
$photoArray = array();
$postsArray = array();
$photoResultArray[] = $results;
// Query photos in DB
foreach($photoResultArray as $row)
{
$photoArray[$row->id] = $row->image;
}
$query2 = $this->db->get('posts');
$results2 = $query2->result_array();
$postsResultArray[] = $results2;
// Associate each post with photo if available
foreach($postsResultArray[0] as $post)
{
$postsArray[] = $photoArray[$post->image_id];
}
return $postArray;
}