Welcome Guest, Not a member yet? Register   Sign In
Substitute name for id

For my first CI4 project I'm developing a document management system for home so I can store scanned paper documents etc.
The principal table is for documents (dms_documents) that has fields for file name, document date, category (image, letter, user guide etc) etc.  The categories are taken from the category table (dms_category) that populates an HTML select input box on the document add form/View.
On startup I have a View page that list all documents in a paged table View, created in my Document Controller and passed to the View thus:
class DocumentController extends BaseController
    // show documents list
    public function index(){
        $documentModel = new DocumentModel();
        $data['dms_documents'] = $documentModel->orderBy('filename', 'DESC')->findAll();
        return view('pages/document_table_view', $data);
To populate the Category select options in the document add form/View I have this in the Controller:
PHP Code:
// add document form
    public function createInCategory($name)
        $documentModel = new DocumentModel();
        $db = \Config\Database::connect();
        $queryCategory $db->query("SELECT `name` FROM `dms_category` WHERE `name` LIKE '%' ORDER BY `name` ASC");
        $data = [
        'selected_category' => $name,
        'dms_category' => $queryCategory->getResultArray(),
        'review'  => Time::today('Europe/London''en_GB')->addMonths(12),
return view('pages/document_add_form'$data);

And in the document add form I have this:
PHP Code:
<div class="form-group">
label for="category">Category<span class="requiredIcon">*</span></label>
   <select name="category" id="category" class="form-control">    
<option value="Select Category">Select Category</option>
     <?php if($dms_category): ?>
      <?php foreach($dms_category as $cat): ?>
        <option value="<?= $cat['name'?>"> <?= $cat['name'?></option>
      <?php endforeach ?>
     <?php endif ?>

This all works fine, except that I now want to introduce a foreign key and referential integrity, so that changing the category name/spelling in the dms_category table will cascade through affected records in the dms_documents table.  I know I really should change <option value="<?= $cat['name'] ?>" to <option value="<?= $cat['id'] ?>" but then my category field in the dms_documents table says '14' rather than 'Letter', and '14' is displayed in the initial document table view.
So, how do I go about substituting the category name for the id in the View?  I suspect the answer lies in the Document Controller index() function, and that I need to get the document data as an array then adjust the data that is sent to the View...

Associated array 'id' => 'name'
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

(02-21-2024, 05:06 AM)InsiteFX Wrote: Associated array 'id' => 'name'


Theme © iAndrew 2016 - Forum software by © MyBB