Insert and Edit with Codigniter

Asked

Viewed 98 times

1

It’s not really a question but whether you can point me in a direction to take on a subject.

I took a project to fix and the developer used Codeigniter, but the project is quite large and with that we have several files.

The big problem is that for each Product, Category, User, etc the developer has created a view to edit the data (edit_user.php) and a page to insert a new user (new_user.php), what is the difference between the two:

The only difference is that in the first view the INPUT fields have the value ($user[0]->txtNome and in the second view the value fields are empty

The problem with this is that if I change or insert a field for the user, I have to change the two pages, lots of work.

One solution would be to put an IF in all inputs, but this could become giant, dependent on the amount of form fields.

Another output would be to create an Objet with the name of all fields and assign the value equal to NULL, which would be great too.

The question is:

  • Is there any way in Codeigniter we create this dynamically, ie using a set_fields, creating an empty object, etc.

I would not like to use dynamic CRUD like GROCERY, so if anyone has any suggestions or a direction they could give me, I appreciate the help of all

  • put an excerpt from the view code

1 answer

0

Friend, as you are using Codeigniter, I do not know if there is an effective solution in it. Maybe there’s a library or helper that can help you with that.

But I believe you can solve this problem simply by using a isset, through ternary condition, to know whether or not to print.

If your form looks like this:

<input name="nome" value="<?= $dados->nome ?>">

You can simply make one of the options below

<input name="nome" value="<?= isset($dados->nome) ? $dados->nome : null ?>" >

<input name="nome" value="<?php isset($dados->nome) and print $dados->nome ?>" >

I think editors like Sublime Text could help make this change faster.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.