Welcome Guest, Not a member yet? Register   Sign In
[PROBLEM SOLVED] Is my condition correct? (keyword & country field)
#1

[eluser]solid9[/eluser]
Is these conditions correct?

Code:
function frontpage_records($per_page, $keyword, $country) {
if($country == 'all' OR $country == '' AND empty($keyword)) {
  $query = $this->db->select('swapid, date_posted, offering, seeking, photo, video, country')
    ->from('swap')
   ->limit($per_page, $this->uri->segment(3))
   ->order_by('swapid', 'DESC')
   ->get();
} else {
  $query = $this->db->select('swapid, date_posted, offering, seeking, photo, video, country')
    ->from('swap')
   ->where('country', $country)      
   ->like('offering', $keyword)
   ->limit($per_page, $this->uri->segment(3))
   ->order_by('swapid', 'DESC')
   ->get();
}

if ($query->num_rows() > 0)
{
  return $query->result_array();
} else {
  return FALSE;
}
}


Can you guys please check.
I'm a little confused because it doesn't do what it suppose to do.

Thanks in advanced.


#2

[eluser]solid9[/eluser]
I don't know the flaws.
If suppose to filter both keyword and by country.

It's a search box.
#3

[eluser]PhilTem[/eluser]
You may want to make the $keyword and $country argument optional or set it to an initial value like

Code:
function frontpage_records($per_page, $keyword = '', $country = 'all') {
  // your code
}

Then it should work as expected.

PS: Furthermore you should not make the limit option dependent on an URI-segment but have it rather as an additional argument to your method

Code:
function frontpage_records($country = 'all', $keyword = '', $per_page = FALSE, $offset = FALSE) {
  // your code with the appropriate logic on $per_page and $offset of course ;) like setting some default values if $per_page or $offset is empty or not use $db->limit() if either parameter is not set
}
#4

[eluser]solid9[/eluser]
Let say I have 3 laptop

1. laptop from japan
2. laptop from angola
3. laptop not specified.

If I entered 'laptop' from search box.
then it should display all 3 laptops.
this works fine.

But if I entered 'laptop' and selected the dropdown box 'japan'
It should display one laptop (from japan).
this does not work.

Why?

thanks in advanced.

#5

[eluser]solid9[/eluser]
@PhilTem

I will carefully review your advice.
I must be doing a little mistake somewhere.
#6

[eluser]solid9[/eluser]
I'm trying to echo the country variable but it's empty.
Code:
function frontpage_records($per_page, $keyword='', $country='all') {
  echo 'country: ';
  echo $country;
  if($country == 'all' AND empty($keyword)) {
   $query = $this->db->select('swapid, date_posted, offering, seeking, photo, video, country')
      ->from('swap')
     ->limit($per_page, $this->uri->segment(3))
     ->order_by('swapid', 'DESC')
     ->get();
  } else {
   $query = $this->db->select('swapid, date_posted, offering, seeking, photo, video, country')
      ->from('swap')
     ->where('country', $country)      
     ->like('offering', $keyword)
     ->limit($per_page, $this->uri->segment(3))
     ->order_by('swapid', 'DESC')
     ->get();
  }

  if ($query->num_rows() > 0)
  {
   return $query->result_array();
  } else {
   return FALSE;
  }
}


why?

#7

[eluser]PhilTem[/eluser]
Do you provide the required argument to your method-call in your controller? Post the content of your controller for this logic, maybe we can find the error there Wink
#8

[eluser]solid9[/eluser]
@PhilTem

Okay here it is,
Code:
function front_paginator() {
  //$this->data['header_message'] = '';
  
  if($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
   $this->data['member_menu'] = $this->load->view('member_menu', null, TRUE);
  } else {
   $this->data['member_menu'] = '';
  }
  
     $this->data['header'] = $this->load->view('header', $this->data, TRUE);
     $this->data['header_ad'] = $this->load->view('header_ad', $this->data, TRUE);  
  
  $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="2" cellspacing="0" class="fp">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th class="fp">',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td class="fp">',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td class="fp">',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
    
  $this->table->set_template($tmpl);  
  
  $this->table->set_heading('Photo/Video', 'Offering', 'Seeking');
  $config['base_url'] = 'http://barterswapping.com/main/front_paginator';

  $keyword = $this->input->post('keyword');
  $config['total_rows'] = $this->db->get('swap')->num_rows();

  $config['per_page'] = 20;
  $per_page = 10; //just the reflection of perpage
  $config['num_links'] = 20;
  $config['full_tag_open'] = '<div id="pagination">';
  $config['full_tag_close'] = '</div>';
  
  $this->pagination->initialize($config);
  //$this->data['records'] = $this->db->get('swap', $config['per_page'], $this->uri->segment(3));
  $userid = $this->session->userdata('user_id');
  $per_page = $config['per_page'];
  
  //catch keyword for validation.
  $country = $this->input->post('country');
  echo 'country_post:'. $country;
  echo '<br>';
  
  $this->form_validation->set_rules('keyword', 'Keyword', 'required');
  $this->form_validation->set_rules('keyword', 'country', 'required');

  if ($this->form_validation->run() == TRUE)
  {
   //unset($config['total_rows']);
   unset($config['total_rows']);
   $config['total_rows'] = $this->bs_model->front_paginator_total_rows($per_page, $keyword, $country);
   $this->data['records'] = $this->bs_model->frontpage_records($per_page, $keyword, $country);
  }
  else
  {
   //display message in header
   $this->data['header_message'] = 'Keyword not valid!';
   $this->data['records'] = $this->bs_model->frontpage_records($per_page, $keyword, $country);
  }
  
     $this->data['midnav'] = $this->load->view('body_m_frontpage', $this->data, TRUE);  
     $this->load->view('front_page', $this->data);
}


Thanks in advanced dude.
#9

[eluser]solid9[/eluser]
Okay I already found the problem.

It is a wrong spelling from the view form.

#10

[eluser]solid9[/eluser]
Okay everything is working fine now.

@PhilTem

Thanks again for your effort.




Theme © iAndrew 2016 - Forum software by © MyBB