• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
questions about forms

#1
[eluser]piranha_swe[/eluser]
Hi

My second post now, thanks for the reply in the first one.

I've been working on a form and redoing it too many times now. I'm not sure how CI wants me to deal with forms.

Here is what I want to do:
I have a form, includes fileuploads, checkboxes, selects and several input fields.

I want to be able to enter all data in the fields, press "create entry" and have it posted and I want all fields (except file) to have the posted data in the fields.
On the right hand of the form I have a list of the last 20 entries that I can click and populate the fields so I can edit my previous posts and click "update entry".

My problem is that I'm used to setting value="<?php echo $posted[3] ?>" for example.
This way I can load from DB into $posted array and populate fields or I can post and have my post array sent into the $posted array to repopulate fields.

However it seems like CI wants me to do it in a diffrent way because the value="<?php echo $posted[3] ?>" gives me notice errors when the array is empty.


I tried using the form helper class and set_select and set_value and so on. But I ran into trouble, actually it didnt do anything at all for me, but even if it had worked for my posting I guess my loading from DB wouldnt have been able to populate the fields?

So right now I just spent alot of time on this but in the end I get notice errors. Do you shut notice errors off? Or do you play by their rules?

How could I accomplish this using CI style programing?

#2
[eluser]comex[/eluser]
If I understand your question correctly...
Either check if the array is set with isset(), or check num_rows() after fetching data from the DB?

Good luck Smile

#3
[eluser]piranha_swe[/eluser]
True, I could use isset on every echo.

So that leads me to another question, I always used variables before they have been set, this obviously causes errors in CI. Does it matter if you do it?

Is there any point spending time on checking variables with isset if it doesnt matter if they are set or not? echoing an empty variable seems okay?

#4
[eluser]comex[/eluser]
Well, in your example with the array $posted.
I'm assuming that you're looping through $posted 20 times and echo each value?
If you'd just do a foreach-loop or check the size of the array before and loop through the available entries.

Or have you hard-coded the 20 echos? =)

I always check if a variable is set before doing anything with it. But I guess if you're not suffering any visual problems, why not? =)
You could be at a performance loss though, but I'll leave that up to the experts to decide =)

#5
[eluser]piranha_swe[/eluser]
hehe I have hardcoded 20 echos.

I try to create as much automated as possible but creating forms I havent found any good way loop them.
If you have any suggestion how to write something like this but that has 12 entries with diffrent type of form objects in some automated way I'd love to learn it.

I dont think an empty echo is much problem performence wise, not that I have noticed atleast.

<table cellspacing="1" bgcolor="#3a3a3a" cellpadding="4" width="700">
<tr>
<td colspan="2"><font class="orangetext_big"><b>Add new record</b></font></td>
</tr>

<tr>
<td bgcolor="#878787" align="left">Record label: </td>
<td bgcolor="#878787" align="left">&lt;input type="text" name="post[]" size="30" value="&lt;?php echo $posted[1]; ?&gt;" /&gt;
</td>
</tr>
<tr>
<td bgcolor="#878787" align="left">Artist: </td>
<td bgcolor="#878787" align="left">&lt;input type="text" name="post[]"size="30" value="&lt;?php echo $posted[2]; ?&gt;" /&gt;
</td>
</tr>

#6
[eluser]comex[/eluser]
One option is to create an array containing each field description and its type.
Eg.

$my_forms = array( array('desc'=>'Record label', 'type'=>'input'), [...] );

And then loop through $my_forms and just echo the corresponding HTML-code for each type.
Of course, this assumes that all of your fields follow the same "design pattern".
For the application I'm doing right now I wrote an model that generates forms from arrays.
Eg.

$forms = array( array('type'=>'input', 'name'=>'lala', 'style'=>'width: 1em;'), [...] );
echo $this->Tablegen->generate_forms($forms);

This has really cut the time spent HTML crunching. So if you're planning on having a lot of forms, something along those lines is(IMHO) worth looking at.

#7
[eluser]kgill[/eluser]
[quote author="piranha_swe" date="1249941567"]True, I could use isset on every echo.

So that leads me to another question, I always used variables before they have been set, this obviously causes errors in CI. Does it matter if you do it?

Is there any point spending time on checking variables with isset if it doesnt matter if they are set or not? echoing an empty variable seems okay?[/quote]

This isn't CI having issues with your unset variables, it's PHP - incidentally, what you do has always thrown errors you've just never seen them because error reporting was likely set to suppress warnings & notices. CI on the other hand displays everything unless you specifically tell it not to. See the user guide section on error handling & PHP's documentation regarding error reporting if you want to turn off notices.

#8
[eluser]Jondolar[/eluser]
When using a form for both add and edit or to display both blank and filled text, you just need to create the array in your controller with blank items when you know the form is supposed to be blank.

Basically, instead of populating your array with posted data, populate it with blanks the first time through. This will prevent the errors.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.