CodeIgniter View Blog Post Page - php

Hello guys i am trying to create a blog. The first home page is ok.. i am getting the shrot description and the categories from the database but...i have problems with the links:
this are my controller functions:
public function index()
{
$this->load->model('Model_cats');
$data['posts'] = $this->Model_cats->getLivePosts(10);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function category($id)
{
$data['category'] = $this->Model_cats->getCategory($id);
$data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['category']['name'];
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function post($id)
{
$data['post'] = $this->Model_cats->getPost($id);
$data['comments'] = $this->Model_cats->getComments($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['post']['title'];
$data['main'] = 'public_post';
$this->load->vars($data);
$this->load->view('template');
}
this are my model function:
function getTopCategories()
{
$this->db->where('parentid',0);
$query = $this->db->get('categories');
$data = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['name'];
}
}
$query->free_result();
return $data;
}
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getCategory($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('categories');
if($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
function getAllPostsByCategory($catid)
{
$data = array();
$this->db->where('category_id', $catid);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getPost($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
and in the view page i have something like this:
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography(word_limiter($list['body'], 200));
echo anchor('post/'.$list['id'],'read more >>');
}
echo '<br/><br/>';
}
I'm getting the post id in the url but.. i don't know why the page is not found.

You have to add the controller name to the anchor uri segments.
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
More on this topic in the CodeIgniter URLs documentation.
If you want a URL like http://example.com/post/123 then you have to add the following to your application/config/routes.php file:
$route['post/(:num)'] = "CONTROLLER/post/$1";
More on routing is also available in the documentation.

Related

I want to build an advanced query in codeigniter

I want to build a query nested in codeigniter
here is the my controller
public function advancedSearch(){
if (!$this->session->userdata('logged_in')) {
redirect('main/login', 'refresh');
}
$session_data = $this->session->userdata('logged_in');
$data['code'] = $session_data['code'];
$start = $this->input->post('start');
$end =$this->input->post('end');
$ticket = $this->input->post('ticket');
$type =$this->input->post('type');
$this->db->select('*');
$this->db->from('table');
if ($start && $end) {
$this->db->where('date >=',$start);
$this->db->where('date <=',$end);
} else if ($ticket) {
$this->db->where('ticket','yes');
} else if ($type) {
$this->db->where('type',$type);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
$data['data'] = $query->result();
}
//print_r($data);
$this->load->view('advanced-search',$data);
with this code I just get the first input a click and search, If I click just ticket I get the $this->db->where('ticket','yes'); I hope I explained well.
Greetings
Hope this will help you :
instead of using if else use it with only if with empty check
if ( ! empty($start))
{
$this->db->where('date >=',$start);
}
if (! empty($end))
{
$this->db->where('date <=',$end);
}
if (! empty($ticket))
{
$this->db->where('ticket','yes');
}
if (! empty($type))
{
$this->db->where('type',$type);
}
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$data['data'] = $query->result();
}
//print_r($data);
$this->load->view('advanced-search',$data);

Implode Multiple values from Select2 form and Insert to database in CodeIgniter

I am using multiple select from a select2 dropdown and I want to store all the multiple values selected from the form in
database "xyz"
tablename='pp_companies'
row="category1" <-- Need this row to be an array , which has the selected values in CSV format)
View file
<div class="titlehead">Courses Offered</div>
<div class="input-group <?php echo (form_error('category1'))?'has-error':'';?>">
<label class="input-group-addon">Category </label>
<select name="category1" class="js-example-basic-multiple form-control" multiple="multiple">
<option value="Engineering" <?php echo (set_value('category1')=='Engineering')?'selected':''; ?>>Engineering</option>
<option value="Management" <?php echo (set_value('category1')=='Management')?'selected':''; ?>>Management</option>
<option value="Law" <?php echo (set_value('category1')=='Law')?'selected':''; ?>>Law</option>
</select>
Once i submit the form , It sends only the last data selected from the input, which is "Law" in this case
My Controller
public function index()
{
$data['ads_row'] = $this->ads;
$data['title'] = 'Create New Employer Account at '.SITE_URL;
$data['msg']='';
$data['result_cities'] = $this->cities_model->get_all_cities();
$data['result_countries'] = $this->countries_model->get_all_countries();
$data['result_industries'] = $this->industries_model->get_all_industries();
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[pp_employers.email]|strip_all_tags');
$this->form_validation->set_rules('pass', 'Password', 'trim|required|min_length[6]|strip_all_tags');
$this->form_validation->set_rules('confirm_pass', 'Confirm password', 'trim|required|matches[pass]|strip_all_tags');
$this->form_validation->set_rules('full_name', 'Your name', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('country', 'Country', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_city', 'City', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('mobile_phone', 'Mobile', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_name', 'Company name', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('industry_id', 'Industry', 'trim|strip_all_tags');
$this->form_validation->set_rules('company_location', 'Company address', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_description', 'Company Description', 'trim|required|strip_all_tags|secure');
$this->form_validation->set_rules('company_join', 'Company Join', 'trim|strip_all_tags|secure');
$this->form_validation->set_rules('company_phone', 'Company Phone', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('no_of_employees', 'No of Employees', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_website', 'Company Website', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('category1', 'Category1', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('captcha', 'Verification code', 'trim|required|validate_ml_spam');
if (empty($_FILES['company_logo']['name']))
$this->form_validation->set_rules('company_logo', 'Company Logo', 'required');
$this->form_validation->set_error_delimiters('<div class="errowbox"><div class="erormsg">', '</div></div>');
if ($this->form_validation->run() === FALSE) {
$data['cpt_code'] = create_ml_captcha();
$this->load->view('employer_signup_view',$data);
return;
}
$current_date_time = date("Y-m-d H:i:s");
$company_slug = make_slug($this->input->post('company_name'));
$is_slug = $this->companies_model->check_slug($company_slug);
if($is_slug>0){
$company_slug.='-'.time();
}
$employer_array = array(
'first_name' => $this->input->post('full_name'),
'email' => $this->input->post('email'),
'pass_code' => $this->input->post('pass'),
'mobile_phone' => $this->input->post('mobile_phone'),
'home_phone' => $this->input->post('home_phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'ip_address' => $this->input->ip_address(),
'dated' => $current_date_time
);
$company_array = array(
'company_name' => $this->input->post('company_name'),
'industry_ID' => $this->input->post('industry_id'),
'company_phone' => $this->input->post('company_phone'),
'company_location' => $this->input->post('company_location'),
'company_city' => $this->input->post('company_city'),
'company_website' => $this->input->post('company_website'),
'no_of_employees' => $this->input->post('no_of_employees'),
'category1'=> $this->input->post('category1'),
'company_description' => $this->input->post('company_description'),
'company_join' => $this->input->post('company_join'),
'company_slug' => $company_slug,
'ownership_type' => $this->input->post('ownership_type')
);
if (!empty($_FILES['company_logo']['name'])){
$company_name_for_file = strtolower($this->input->post('company_name'));
$real_path = realpath(APPPATH . '../public/uploads/employer/');
$config['upload_path'] = $real_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = true;
$config['max_size'] = 6000;
$config['file_name'] = 'JOBPORTAL-'.time();
$this->upload->initialize($config);
if ($this->upload->do_upload('company_logo')){
/*if($obj_row->company_logo){
#unlink($real_path.'/'.$obj_row->company_logo);
#unlink($real_path.'/thumb/'.$obj_row->company_logo);
}*/
}
$image = array('upload_data' => $this->upload->data());
$image_name = $image['upload_data']['file_name'];
$company_array['company_logo']=$image_name;
$thumb_config['image_library'] = 'gd2';
$thumb_config['source_image'] = $real_path.'/'.$image_name;
$thumb_config['new_image'] = $real_path.'/thumb/'.$image_name;
$thumb_config['maintain_ratio'] = TRUE;
$thumb_config['height'] = 50;
$thumb_config['width'] = 70;
$this->image_lib->initialize($thumb_config);
$this->image_lib->resize();
}
$company_id = $this->companies_model->add_company($company_array);
$employer_array['company_ID'] = $company_id;
$employer_id = $this->employers_model->add_employer($employer_array);
$user_data = array(
'user_id' => $employer_id,
'user_email' => $this->input->post('email'),
'first_name' => $this->input->post('full_name'),
'slug' => $company_slug,
'last_name' => '',
'is_user_login' => TRUE,
'is_job_seeker' => FALSE,
'is_employer' => TRUE
);
$this->session->set_userdata($user_data);
//Sending email to the user
$row_email = $this->email_model->get_records_by_id(3);
$config = array();
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->clear(TRUE);
$this->email->from($row_email->from_email, $row_email->from_name);
$this->email->to($this->input->post('email'));
$this->email->subject($row_email->subject);
$mail_message = $this->email_drafts_model->employer_signup($row_email->content, $employer_array);
$this->email->message($mail_message);
$this->email->send();
redirect(base_url('employer/post_new_job'),'');
}
Model
public function add_company($data){
$return = $this->db->insert('pp_companies', $data);
if ((bool) $return === TRUE) {
return $this->db->insert_id();
} else {
return $return;
}
}
public function update_company($id, $data){
$this->db->where('ID', $id);
$return=$this->db->update('pp_companies', $data);
return $return;
}
public function delete_company($id){
$this->db->where('ID', $id);
$this->db->delete('pp_companies');
return true;
}
/*public function get_all_companies($per_page, $page) {
$this->db->select('pp_companies.*');
$this->db->from('pp_companies');
$this->db->order_by("pp_companies.ID", "DESC");
$this->db->limit($per_page, $page);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}*/
public function get_all_companies($per_page, $page) {
$this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_phone, pp_companies.company_website, pp_companies.industry_ID, pp_companies.company_logo');
$this->db->from('pp_companies');
$this->db->join('pp_employers', 'pp_employers.company_ID = pp_companies.ID', 'inner');
$this->db->order_by("pp_employers.ID", "DESC");
$this->db->limit($per_page, $page);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function record_count($table_name) {
return $this->db->count_all($table_name);
}
public function get_company_by_id($id) {
$this->db->select('pp_companies.*');
$this->db->from('pp_companies');
$this->db->where('pp_companies.ID', $id);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function check_slug($slug) {
$this->db->where('company_slug', $slug);
$this->db->from('pp_companies');
return $this->db->count_all_results();
}
public function check_slug_edit($CID, $slug) {
$this->db->where('company_slug', $slug);
$this->db->where('ID !=', $CID);
$this->db->from('pp_companies');
return $this->db->count_all_results();
}
public function get_company_by_old_id($id) {
$this->db->select('pp_companies.*');
$this->db->from('pp_companies');
$this->db->where('pp_companies.old_company_id', $id);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
2nd Model
public function add_employer($data){
$return = $this->db->insert('pp_employers', $data);
if ((bool) $return === TRUE) {
return $this->db->insert_id();
} else {
return $return;
}
}
public function update_employer($id, $data){
$this->db->where('ID', $id);
$return=$this->db->update('pp_employers', $data);
return $return;
}
public function update($id, $data){
$this->db->where('ID', $id);
$return=$this->db->update('pp_employers', $data);
return $return;
}
public function delete_employer($id){
$this->db->where('ID', $id);
$this->db->delete('pp_employers');
}
public function authenticate_employer($user_name, $password) {
$this->db->select('pp_employers.*, pp_companies.company_slug');
$this->db->from('pp_employers');
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
$this->db->where('email', $user_name);
$this->db->where('pass_code', $password);
$this->db->limit(1);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function authenticate_employer_by_email($user_name) {
$this->db->select('pp_employers.*');
$this->db->from('pp_employers');
$this->db->where('email', $user_name);
$this->db->limit(1);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function authenticate_employer_by_password($ID, $password) {
$this->db->select('*');
$this->db->from('pp_employers');
$this->db->where('ID', $ID);
$this->db->where('pass_code', $password);
$this->db->limit(1);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function is_email_already_exists($ID, $email) {
$this->db->select('ID');
$this->db->from('pp_employers');
$this->db->where('ID !=', $ID);
$this->db->where('email', $email);
$this->db->limit(1);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row('ID');
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function get_all_employers($per_page, $page) {
$this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_employers.city, pp_employers.country, pp_employers.top_employer, pp_employers.ip_address, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_logo, pp_companies.company_phone, pp_companies.company_location, pp_companies.company_slug');
$this->db->from('pp_employers');
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
$this->db->order_by("pp_employers.ID", "DESC");
$this->db->limit($per_page, $page);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function record_count($table_name) {
return $this->db->count_all($table_name);
}
public function get_employer_by_id($id) {
$this->db->select('pp_employers.*, pp_companies.ID AS CID,pp_companies.company_name,pp_companies.company_email,pp_companies.ownership_type,pp_companies.company_ceo,pp_companies.industry_ID,pp_companies.ownership_type,pp_companies.company_description,pp_companies.company_location,pp_companies.no_of_offices,pp_companies.company_website,pp_companies.no_of_employees, pp_companies.established_in, pp_companies.company_logo, pp_companies.company_folder, pp_companies.company_type, pp_companies.company_fax, pp_companies.company_slug, pp_companies.company_phone, pp_job_industries.industry_name');
$this->db->from('pp_employers');
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
$this->db->join('pp_job_industries', 'pp_companies.industry_ID = pp_job_industries.ID', 'left');
$this->db->where('pp_employers.ID', $id);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function get_employer_by_id_simple($id) {
$this->db->select('pp_employers.*');
$this->db->from('pp_employers');
$this->db->where('pp_employers.ID', $id);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function get_employer_by_company_id($cid) {
$this->db->select('pp_employers.*, pp_companies.ID AS CID,pp_companies.company_name,pp_companies.company_email,pp_companies.company_ceo,pp_companies.industry_ID,pp_companies.ownership_type,pp_companies.company_description,pp_companies.company_location,pp_companies.no_of_offices,pp_companies.company_website,pp_companies.no_of_employees, pp_companies.established_in, pp_companies.company_logo, pp_companies.company_folder, pp_companies.company_type, pp_companies.company_fax, pp_companies.company_phone');
$this->db->from('pp_employers');
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
$this->db->where('pp_employers.company_ID', $cid);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
public function search_all_employers($per_page, $page, $search_parameters, $wild_card='') {
$where = ($wild_card=='yes')?'where':'like';
$this->db->select('pp_employers.ID, pp_employers.dated, pp_employers.email, pp_employers.first_name, pp_employers.last_name, pp_employers.company_ID, pp_employers.sts, pp_employers.top_employer, pp_companies.ID AS CID, pp_companies.company_name, pp_companies.company_logo');
$this->db->from('pp_employers');
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'inner');
$this->db->$where($search_parameters);
$this->db->order_by("pp_employers.ID", "DESC");
$this->db->limit($per_page, $page);
$Q = $this->db->get();
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
//echo $this->db->last_query(); exit;
return $return;
}
public function search_record_count($table_name, $search_parameters) {
//return $this->db->count_all($table_name);
$this->db->like($search_parameters);
$this->db->from($table_name);
$this->db->join('pp_companies', 'pp_employers.company_ID = pp_companies.ID', 'left');
return $this->db->count_all_results();
//exit;
}
public function get_all_active_employers($per_page, $page) {
$Q = $this->db->query("CALL get_all_active_employers($page, $per_page)");
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
public function get_all_active_top_employers($per_page, $page) {
$Q = $this->db->query("CALL get_all_active_top_employers($page, $per_page)");
if ($Q->num_rows > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
public function get_company_details_by_slug($slug) {
$Q = $this->db->query('CALL get_company_by_slug("'.$slug.'")');
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
I know, i'll have to use the implode function in my controller and make the name attribute into an array, i.e = category1[], But I'm not quite familiar with how to apply it in correctly this case, as many attempts to do this have resulted in the controller not displaying anything in the page.
It would help me a great deal if i can get some help on sorting this out.
Basically, Once if the Multiple values gets inserted into the database, I'll have a search and filter running.
EDIT 1 : Added Controller and Models
try this
<div class="input-group <?php echo (form_error('category1'))?'has-error':'';?>">
<label class="input-group-addon">Category </label>
<select name="category1[]" class="js-example-basic-multiple form-control" multiple="multiple">
<option value="Engineering" <?php echo (set_value('category1')=='Engineering')?'selected':''; ?>>Engineering</option>
<option value="Management" <?php echo (set_value('category1')=='Management')?'selected':''; ?>>Management</option>
<option value="Law" <?php echo (set_value('category1')=='Law')?'selected':''; ?>>Law</option>
</select>
Turns out , while initializing your array in the public function validation in the controller section for your array attribute
Should implicitly specify '[]' in set_rules
$this->form_validation->set_rules('category1', 'Category1', 'trim|required|strip_all_tags');
to
$this->form_validation->set_rules('category1[]', 'Category1', 'trim|required|strip_all_tags');
then it started accepting my query as an array with no issues, and as for the multiple selected items into the DB
Used this code , pretty self explanatory.
$arrcategory1 = $this->input->post('category1');
foreach($arrcategory1 as $val1)
{
$categoryarr1 = $categoryarr1 . $val1. ",";
}
$categoryarr1 = substr(trim($categoryarr1), 0, -1);
and thats pretty much it, the field in category1 now shows Engineering,Management,Law as CSV

How to sort array of objects in php [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have an array like below,
[{
"name":"Daniel",
"connection_status":"1"
},
{
"name":"Danny",
"connection_status":"3"
},
{
"name":"Moris",
"connection_status":"2"
},
{
"name":"Manny",
"connection_status":"1"
}]
I want to sort my array by status like 1,2,3 in this order.
This is my code,
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->get();
$list = $query->result();
$friends = $this->checkFriends($list, $user_id);
return $friends;
}
public function checkFriends($list, $user_id)
{
$array = [];
foreach ($list as $k => $v) {
// print_r(json_encode($list));
$friends = $this->checkStatus($v->profile_id);
//print_r($friends);
$relationship = '';
$relation_id = '';
foreach ($friends as $kk => $vv) {
if ($user_id == $vv->sent_id) {
if ($vv->status == 1) {
$relationship = 1;
}
if ($vv->status == 2) {
$relationship = 2;
}
} else if ($user_id == $vv->recieved_id) {
// pending
if ($vv->status == 1) {
$relationship = 3;
$relation_id = $vv->sent_id;
}
if ($vv->status == 2) {
$relationship = 4;
}
}
}
$list[$k]->connection_status = $relationship;
$list[$k]->relation_id = $relation_id;
}
return $list;
}
public function checkStatus($id)
{
$this->db->select('*');
$this->db->from('requests');
$this->db->where('sent_id', $id);
$this->db->or_where('recieved_id', $id);
$query = $this->db->get();
$list = $query->result();
return $list;
}
connection status is not a db field.
Where $list is my o/p array.Can anyone help me.Thanks in advance.
I want to sort my array based on my connection_status.
if i understand you correctly this may help you
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status', 'asc');
$query = $this->db->get();
$list = $query->result();
return $list;
}
You can apply order by in query
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$this->db->order_by('status'); // USE ORDER BY
$query = $this->db->get();
$list = $query->result();
return $list;
}
public function getProfileDataForMySociety($user_id)
{
$this->db->select('*');
$this->db->from('profile');
$this->db->where('profile_id!=', $user_id);
$query = $this->db->order('status asc')->get();
$list = $query->result();
return $list;
}

Retrieve related post with codeigniter

I'm unable to retrieve similar post data from db with codeigniter. In my blog, I have a tags field which is keeping data like 'php,mysql,mongo,java,jquery'
I just try to get similar post which is related with current posts tags. But im not getting expected result. and the problem is in my query. Its show only three post and that is 1st, last, and number 3rd one.
[CONTROLLER]
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result();
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags; }
$match = explode(',', $tags);
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
}
return $sqlQuery->result();
}
[VIEW]
foreach($similar as $row)
{
echo($row->btitle.'<br/>');
}
Try this.
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result(); // why this line??
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags }
$match = explode(',', $tags);
$result = [];
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
if($sqlQuery->num_rows()>0)
$result[] = $sqlQuery->result();
}
return $result;
}
[VIEW]
$check = [];
foreach($similar as $row)
{
foreach($row as $data)
{
if(!in_array($data->btitle,$check))
{
$check[] = $data->btitle;
echo $data->btitle.'<br/>';
}
}
}

Get database through email id in codeigniter

Controller[In Article Page Article Properly work with pagination, store user email id in 'articles' database , now i tried to get the user firstname, and lastname from users table but not work properly ]
public function articles()
{
$data['title'] = "Articles";
$config = array();
$config["base_url"] = base_url() . "sd/articles/";
$config["total_rows"] = $this->model_users->record_count_articles();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->model_users->fetch_result_articles($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$this->load->view('sd/header',$data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/footer', $data);
} else {
$this->load->view('sd/sdheader', $data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/sdfooter', $data);
}
}
Model [ Get Users Name in Article Page ]
public function record_count_articles() {
return $this->db->where('status','1')->count_all("articles");
}
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->where('status','1')->order_by('id', 'DESC')->get("articles");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Add These Lines [ But Not Work]
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
$query = $this->db->select('firstname')->select('lastname')->where('email',$data[0]->email)->get("users");
$data['name_info']=$query->result_array();
}
return $data;
}
return false;
You have 2 problem here. please have a look on comments in code.
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//1) $data[0]->email keep repeating same email.
// inner $query variable should be different.
$innerQuery = $this->db->select('firstname,lastname')->where('email',$row->email)->get("users");
//2) you need to store query result on array.
// $data['name_info'] stores only single record.
$data[]=$innerQuery ->result_array();
}
return $data;
}
return false;
You should avoid query in loop if you can achieve it by join
EDIT: Lets try this with join
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db
->join('users u','u.email = a.email','left')
->where('a.status','1')->order_by('a.id', 'DESC')->get("articles a");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I have not tested the code. but it is better way than loop.

Categories