CodeIgniter Forums
Retaining search variables to return to filtered view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Retaining search variables to return to filtered view (/showthread.php?tid=90685)



Retaining search variables to return to filtered view - pchriley - 04-18-2024

Hello,
My document management system opens by default to a paged table showing all rows in the documents table.  
Controller:
PHP Code:
public function index(){
        $documentModel = new DocumentModel();
        $db = \Config\Database::connect();
        $queryStatus $db->query("SELECT * FROM dms_status");
        $queryCategory $db->query("SELECT * FROM dms_category");
        $queryDoctype $db->query("SELECT * FROM dms_doctype");
$data = [
 
'dms_documents' => $documentModel->orderBy('subject DESC, document_date')->findAll(),
 
'statusShow' => $queryStatus->getResultArray(),
 
'categoryShow' => $queryCategory->getResultArray(),
 
'doctypeShow' => $queryDoctype->getResultArray(),
 ];
    return view('pages/document_table_view'$data);
    

View:
PHP Code:
<?php if($dms_documents): ?>
                            <?php foreach($dms_documents as $dms_document): ?>
                            <tr>
                            <td><?php echo $dms_document['subject']; ?></td> 
<td><a href="<?php echo base_url('document-edit-form/'.$dms_document['docID']);?>"</a><?php echo $dms_document['description']; ?></td> 
                       

The 'Description' column for each row is an href link to that record, and opens - routed via the Controller - an edit form for that record.  The edit form allows me to amend/save, ignore or delete the record.  The edit form action then returns me to the original table view.
That all works fine, but I've now introduced a search form so that the resulting version of the table view is filtered according to the search criteria (currently just a simple 'LIKE'):

PHP Code:
[code]$data = [
            'field' => $this->request->getVar('field'),
            'searchtext' => $this->request->getVar('searchtext'),
            ];
        
        $documentModel 
= new DocumentModel();
    $db = \Config\Database::connect();
    $builder $db->table('dms_documents');
        $queryStatus $db->query("SELECT * FROM dms_status");
        $queryCategory $db->query("SELECT * FROM dms_category");
        $queryDoctype $db->query("SELECT * FROM dms_doctype");
        
        $builder
->like($data['field'], $data['searchtext'], 'both');
        $results $builder->get();
    
        $filterData 
= [
        'filter_validation' => $this->validator,
        'filter_dms_documents' => $results->getResultArray(),
        'filter_statusShow' => $queryStatus->getResultArray(),
        'filter_doctypeShow' => $queryDoctype->getResultArray(),
      'filter_categoryShow' => $queryCategory->getResultArray(),
        'filterText' => $query,
        'isFiltered' => TRUE,
        ];
        
        
return view('pages/document_table_view_filter'$filterData);[/code

This works, but what I cannot fathom out is how to return from the edit form to a table view reflecting the search criteria (also taking into account any changes made via the edit form) rather than to the full dataset.  
Thanks


RE: Retaining search variables to return to filtered view - Bosborne - 04-23-2024

Your answer is to use sessions.
https://www.codeigniter.com/user_guide/libraries/sessions.html


RE: Retaining search variables to return to filtered view - pchriley - 04-25-2024

(04-23-2024, 03:20 AM)Bosborne Wrote: Your answer is to use sessions.
https://www.codeigniter.com/user_guide/libraries/sessions.html

Excellent!  Thank you so much - I now understand that I can add the data passed to the table view to the session and recall it as needed.  I had previously assumed that sessions were for holding login data etc.

Cheers