Search Results with Pagination

Joined
Oct 25, 2024
Messages
2
Reaction score
0
I am working on an old Codeigniter 3.1.9 site in PHP 8.2.12. My question is:

Is it possible to have search results from a database be split up into say 20 results per page plus 2 more heading rows to compile the entire page(s) with CI's pagination? If so, how would I set up the pagination feature in CI3 to create these "pages"?

I have a total of 58 recipe records in my database in a table called "recipe". In this table I have 14 fields: id, name, buttontext, linkname, time, img, ingredients, equipment, prepreparation, directions, category, subcategory, updated, & keywords.

Of these fields, I am only including 4 in my search at present. They are id, name, category, & subcategory.

The problem I have is that whatever is searched for has no result row limitations for how long the search results can be. So lets say someone searches for something as generic as "recipe" or "recipes", the search will return every recipe record. Which is what I want, but I would prefer it not overwhelm the user with all 58 records being printed out at once.

So, that brings me to my question:

Is it possible to have these search results be output to the results container for up to say 20 results at a time, and be able to go page by page for the remainder of the results? If so, how?

I already am using pagination in CI3 on an admin section for a recipe flip book. That however is set up to paginate based on all fields for each recipe and not a list of all recipes provided the search term is "recipe" or "recipes".

I am still new to PHP and this project was dumped in my lap by a programmer to figure out. Let me just say, trying to decipher this monster of a site, has been quite the challenge. I have kept it running since Codeigniter 1.83. Please be kind and patient as I navigate my way through this.

Also, I didn't initially write the code for this site so I am having to reverse engineer nearly everything inside of it. I will provide any files you might need to see upon request. I just don't want to make my post any longer than it already is and overwhelm you all with needless code.

That being said, here is some of the code you might need:

Recipe.php Controller (for the flip book and search)
PHP:
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'] = 10;
 $config['display_pages'] = TRUE;
 $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', 'admin/page/recipe_flipbook'),
  'recipe' => $recipe,
  'updated' => $this->format_date($recipe['updated'])
 );  
 $this->load->view('templates/main', $data);
}
     
function search(){
 if($this->input->method() === 'post'){          
  $search = $this->input->post('query');        
  $results = $this->recipe_model->search($search);
  $data = array(
   'title' => "Shirley's Recipes:Search Results",
   'columns' => array('toc', 'public/page/search_results'),
   'recipes' => $results,  // $results is being renamed $recipes. I don't really know why though yet...
   'search' => $search
  );
  $this->load->view('templates/main', $data);
 }
 else{          
  redirect('recipe');
 }
}

Recipe_model Model
PHP:
function search($search){      
 $terms = explode(' ', $search);
 $match = " ";      
 foreach($terms as $term){    
  $match .= $term;
 }      
 $querystr = "SELECT id, name, category, subcategory, keywords, MATCH(name, category, subcategory, keywords) AGAINST('".$match."') as score FROM recipe WHERE MATCH(name, category, subcategory, keywords) AGAINST('".$match."') ORDER BY name;";
 $q = $this->db->query($querystr);  
 return $q->result();
}

search_result.php View
PHP:
<link rel="stylesheet" type="text/css" href="/assets/css/public/page/searchresults.css" media="screen" />
<div id="sitecontent" class="grid-item2 grid-container">
 <?php
  if($admin){
   $this->load->view('admin/nav/notesnav');
  }
 ?>
 <div id="contents" class="grid-item1 grid-container scrollbar <?php if($admin){echo 'grid-item2';} ?>">
  <div id="spiral" class="grid-item1">
   <img class="spiral" src="/assets/img/lines/spiral1trans.png"/>
  </div>
  <div id="searchresults" class="grid-item2 grid-container">
   <h1>Search Results</h1>
   <div id="results" class="grid-item1 grid-container">
    <div id="resultsheading" class="grid-item1 heading">Your Search Results for:</div><div id="searchterm" class="grid-item"><?= $search ?></div>
    <div id="numheading" class="grid-item3 heading">#:</div>
    <div id="resultlistheading" class="grid-item4 heading">Results:</div>
    <div id="categoryheading" class="grid-item5 heading">Category:</div>
    <div id="subcategoryheading" class="grid-item6 heading">Subcategory</div>
    <?php
     if(!empty($recipes)):
      foreach ($recipes as $key => $recipe):
    ?>
    <div class="grid-item num"><?= $key + 1 ?></div>
    <div class="grid-item result"><a href="/recipe/<?= $recipe->id ?>"><?= $recipe->name ?></a></div>
    <div class="grid-item result"><?= $recipe->category ?></div>
    <div class="grid-item result"><?= $recipe->subcategory ?></div>
    <?php endforeach; ?>
    <?php else: ?>
    <div class="grid-item no-result">No matching recipes</div>
    <?php endif; ?>
   </div>
  </div>
 </div>
 <div id="updated" class="grid-item2">Updated 05/27/2023 @ 2:34 EDT</div>
</div>
recipe_flipbook.php View
PHP:
<link rel="stylesheet" type="text/css" href="/assets/css/admin/core/admin.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/assets/css/admin/page/flipbook.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/assets/css/admin/core/pagination.css" />

<div id="sitecontent" class="grid-item2 grid-container">
 <?php $this->load->view('admin/nav/notesnav'); ?>
 <div id="contents" class="grid-item2 grid-container scrollbar">
  <div id="spiral" class="grid-item1">
   <img class="spiral" src="/assets/img/lines/spiral1trans.png"/>
  </div>
  <div id="recipe-flipbook" class="grid-item2 grid-container">
   <div id="title" class="box grid-item1">
    <h1><?=$recipe['name']; ?></h1>
    <h3><?=$recipe['time']; ?></h3>
   </div>
   <div id="ingredients" class="box grid-item2">
    <h2>Ingredients:</h2>
    <?php
      if(isset($recipe['ingredients']) && $recipe['ingredients'] != ''){
       echo $this->mkdn->translate($recipe['ingredients']);
      }
      else{
       echo '<br><br><br><br><br><br><br><br><br><br><br>';
      }
     ?>
    </div>
    <div id="equipment" class="box grid-item3">
     <h2>Equipment:</h2>
     <?php
       if(isset($recipe['equipment']) && $recipe['equipment'] != ''){
        echo $this->mkdn->translate($recipe['equipment']);
       }
       else{
        echo '<br><br><br><br><br><br><br><br><br><br><br>';
       }
      ?>
     </div>
     <div id="prepreparation" class="box grid-item4">
      <h2>Pre-Preparation:</h2>
      <?php
        if(isset($recipe['prepreparation']) && $recipe['prepreparation'] != ''){
         echo $this->mkdn->translate($recipe['prepreparation']);
        }
        else{
         echo '<br><br><br><br><br><br><br><br><br><br><br>';
        }
       ?>
      </div>
      <div id="directions" class="box grid-item5">
       <h2>Directions:</h2>
       <?php
        if(isset($recipe['directions']) && $recipe['directions'] != ''){
         echo $this->mkdn->translate($recipe['directions']);
        }
        else{
         echo '<br><br><br><br><br><br><br><br><br><br><br>';
        }
       ?>
      </div>
     </div>  
     <script type="text/javascript" src="/assets/js/confirmdelete.js"></script>
     </br>
    </div>
    <?php
     echo $this->pagination->create_links();  
     if(isset($updated)){
      echo '<div id="updated" class="grid-item4 noprint">Updated '.$updated.'</div>';
     }
     if($admin){
      echo '<div id="optionbuttons" class="grid-tem5 noprint">';
      echo anchor('recipe/edit/'.$recipe['id'], img(array('src'=>'./assets/img/icons/edit.png', 'alt'=>'Edit', 'title'=>'Edit')));
      echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
      echo '<a href="javascript:confirmdelete(\'Delete recipe?\',\''.base_url().'recipe/delete/'.$recipe['id'].'\')">'.img(array('src'=>'./assets/img/icons/delete.png', 'alt'=>'Delete', 'title'=>'Delete')).'</a>';
      echo '</div>';
     }
    ?>
  </div>

Any help anyone could offer would be greatly appreciated! Thank you all in advance
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,949
Messages
2,570,111
Members
46,683
Latest member
Elane75F41

Latest Threads

Top