Welcome Guest, Not a member yet? Register   Sign In
Retaining search variables to return to filtered view
#1

(This post was last modified: 04-18-2024, 05:50 AM by pchriley.)

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
Reply
#2

Your answer is to use sessions.
https://www.codeigniter.com/user_guide/l...sions.html
Reply
#3

(04-23-2024, 03:20 AM)Bosborne Wrote: Your answer is to use sessions.
https://www.codeigniter.com/user_guide/l...sions.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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB