Welcome Guest, Not a member yet? Register   Sign In
Search Criteria Ignored, Only Redirects to Else Statement
#1

(This post was last modified: 06-12-2019, 12:28 PM by Josh1985.)

Hello all!

I am experiencing a weird issue with a site now. Up until my web host updated PHP to 7.3.6, I had no issues with my search feature. I have read through the changelogs on php.net all with no help pointing me in the direction of what is now broken.

Code Modules In Use:
Code:
PHP 7.3.6
CodeIgniter 3.1.9

My_Controller.php:
Code:
<?php

class MY_Controller extends CI_Controller{
function __construct() {
 parent::__construct();
 $this->load->vars(array('toc' => $this->recipe_model->read_names_categories(),
 'admin_menu_items' => array(),
 'admin' => $this->is_logged_in()));
 if($this->is_logged_in()){
  date_default_timezone_set($this->session->userdata('tz'));
 }
 else{
  date_default_timezone_set('America/New_York');
 }
}
   
function is_logged_in(){
 $logged_in = $this->session->userdata('logged_in');
  if(isset($logged_in) && $logged_in){
   return true;
  }
  else{
   return false;
  }
 }
   
function require_login(){
 if(!$this->is_logged_in()){
  redirect('login');
 }
}
   
function format_date($datestr) {
 //date_default_timezone_set('UTC');
  $date = strtotime($datestr.' UTC');
 //date_default_timezone_set('America/New_York');
  $datestr = date("n/j/Y @ g:i A T", $date);
   return $datestr;
}  
}  
//End MY_Controller.php

Recipe.php Controller:
Code:
<?php
class Recipe extends MY_Controller{

function __construct() {
 parent::__construct();
  $uri = $this->uri->uri_string();
  $this->session->set_flashdata('uri', $uri);
}

function index(){
 $recipe_linkname = $this->uri->rsegment(3);
 if(!$recipe_linkname){
  $recipe_linkname = $this->recipe_model->first_linkname();
 }
 $recipe = $this->recipe_model->read_record($recipe_linkname);
 if(count($recipe) == 0){
  show_404('');
  die();
 }
 $data = array(
    'title' => "Shirley's Recipes : ".$recipe['name'],
  'columns' => array('toc', 'recipe'),
   'recipe' => $recipe
 );
 $this->load->view('includes/template', $data);
}

function flipbook(){
 $this->require_login();
 $this->load->library('pagination');
   
       $config['base_url'] = site_url('recipe/flipbook');
     $config['total_rows'] = $this->recipe_model->num_rows();
    $config['uri_segment'] = '3';
       $config['per_page'] = 1;
      $config['num_links'] = 1;
  $config['display_pages'] = FALSE;
  $config['full_tag_open'] = '<div id="pagination">';
 $config['full_tag_close'] = '</div>';
 
 $this->pagination->initialize($config);
 $recipe = $this->recipe_model->read_records($config['per_page'], $this->uri->segment(3))->row_array();
   $data = array(
     'title' => "Shirley's Recipes : ".$recipe['name'],
     'columns' => array('toc', 'recipe_flipbook'),
      'recipe' => $recipe
 );
 $this->load->view('includes/template', $data);
}
 
function search(){
 if($this->input->post('submit')){
   $search = $this->input->post('query');
  $results = $this->recipe_model->search($search);
     $data = array(
     'title' => "Shirley's Recipes : Search Results",
     'columns' => array('toc', 'recipe_search'),
     'recipes' => $results,
  );
  $this->load->view('includes/template', $data);
  }
  else{
   redirect('recipe');
  }
}

function add(){
 $this->require_login();
 if($this->input->post('submit')){
  $data = array(
       'name' => $this->input->post('name'),
 'buttontext' => $this->input->post('buttontext'),
           'linkname' => $this->input->post('linkname'),
                'img' => $this->input->post('img'),
               'time' => $this->input->post('time'),
   'category' => $this->input->post('category'),
        'subcategory' => $this->input->post('subcategory'),
' ingredients' => $this->input->post('ingredients'),
  'equipment' => $this->input->post('equipment'),
'preparation' => $this->input->post('preparation'),
 'directions' => $this->input->post('directions')
  );
  $recipeid = $this->recipe_model->create_record($data);
  redirect('recipe/'.$this->input->post('linkname'));
 }
 else{
  $data = array(
     'title' => "Shirley's Recipes : Add New Recipe",
   'columns' => array('toc', 'admin/recipe_edit'),
    'recipe' => array('name' => '',
                      'buttontext' => '',
                        'category' => '',
                     'subcategory' => '',
                        'linkname' => '',
                             'img' => '',
                            'time' => '',
                     'ingredients' => '',
                       'equipment' => '',
                     'preparation' => '',
                      'directions' => '')
  );
  $this->load->view('includes/template', $data);
 }
}

function edit(){
 $this->require_login();
 $id = $this->uri->segment(3);
 if($this->input->post('submit')){
  $data = array(
          'name' => $this->input->post('name'),
  'buttontext' => $this->input->post('buttontext'),
    'linkname' => $this->input->post('linkname'),
           'img' => $this->input->post('img'),
           'time' => $this->input->post('time'),
      'category' => $this->input->post('category'),
   'subcategory' => $this->input->post('subcategory'),
   'ingredients' => $this->input->post('ingredients'),
     'equipment' => $this->input->post('equipment'),
   'preparation' => $this->input->post('preparation'),
    'directions' => $this->input->post('directions')
   );
   $this->recipe_model->update_record($id, $data);
   redirect('recipe/'.$this->input->post('linkname'));
 }
 else{
  $recipe = $this->recipe_model->read_record($id);
  $data = array(
   'title' => "Shirley's Recipes : Edit Recipe : ".$recipe['name'],
   'columns' => array('toc', 'admin/recipe_edit'),
        'id' => $id,
  'recipe' => $recipe
  );
  $this->load->view('includes/template', $data);
 }
}

function delete(){
 $this->require_login();
 $this->recipe_model->delete_record($this->uri->segment(3));
 redirect('/');
}
}
// End - recipe.php
?>

Recipe_model
Code:
<?php
class Recipe_model extends CI_Model {

function create_record($data){
 if(isset($data['buttontext']) and $data['buttontext'] == ''){
  $data['buttontext'] = NULL;
 }
 $this->db->insert('recipe', $data);
 return $this->db->insert_id();
}
 
function read_records($limit, $offset){
 $this->db->order_by('name', 'asc');
 if(isset($limit) && isset($offset)){
  $this->db->limit($limit, $offset);
 }
 $q = $this->db->get('recipe');
 return $q;
}
 
function read_record($key){
 if(is_numeric($key)){
  $this->db->where('id', $key);
 }
 else{
  $this->db->where('linkname', $key);
 }
 $q = $this->db->get('recipe');
 return $q->row_array();
}
 
function first_linkname(){
 $this->db->select('linkname, name');
 $this->db->order_by('name', 'asc');
 $this->db->limit(1);
 $q = $this->db->get('recipe');    
 return $q->row()->linkname;
}

function read_names(){
 $this->db->select('linkname, name');
 $this->db->order_by('name', 'asc');
 $q = $this->db->get('recipe');
 return $q->result();
}
 
function read_names_categories(){
 $this->db->select('buttontext, linkname, name, category, subcategory');
 $this->db->order_by('name', 'asc');
 $q = $this->db->get('recipe');
 return $q->result();  
}

function search($search){
 $terms = explode(" ", $search);
 $match = "";
 foreach($terms as $term){
  $match .=  $term;
 }
 $querystr = "SELECT *, MATCH(name, linkname, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') as score FROM recipe WHERE MATCH(name, linkname, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') ORDER BY score DESC;";
 $q = $this->db->query($querystr);
 return $q->result();
}

function update_record($id, $data){
 if(isset($data['buttontext']) and $data['buttontext'] == ''){
  $data['buttontext'] = NULL;
 }
 $this->db->where('id', $id);
 $this->db->update('recipe', $data);
}
 
function delete_record($id){
 $this->db->where('id', $id);
 $this->db->delete('recipe');
}
 
function num_rows(){
 return $this->db->get('recipe')->num_rows();
}
}
// End - recipe_model.php
?>

Recipe_search View (This should load whether or not results were found but it NEVER loads)
Code:
<div id="recipelist" class="content">
<h1>Recipe Search</h1>
 <div class="recipesearch">
  <ol>
   <?php if(!empty($recipes)): foreach($recipes as $recipe): ?>
    <li><a href="/recipe/<?=$recipe->id ?>"><?=$recipe->name ?></a><span class="confidence"><?=$recipe->score ?></span></li>
    <?php endforeach; ?>
  </ol>
 </div>
 <?php else: ?>
 <h3>No matching recipes</h3>
 <?php endif; ?>
</div>

I hope that is all the code you need to help me. If not, I will be happy to provide more.

A brief overview of the issue is if someone comes to the site to search for a recipe, no matter what they put in and submit; the search redirects them back to the first alphabetical recipe on the site. No matter what is entered.

What used to happen when a search was performed is the site would open up the "Recipe_search.php" View and show them that. Now, all I get is the first alphabetical recipe page. What am I missing here?

Thank you in advance!
Reply


Messages In This Thread
Search Criteria Ignored, Only Redirects to Else Statement - by Josh1985 - 06-12-2019, 12:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB