Welcome Guest, Not a member yet? Register   Sign In
Form used in new and edit mode
#1

[eluser]Samuurai[/eluser]
Hi everyone,

Just wondering if I am doing this in a really stupid way or not, can someone please tell me if this is the best way to use a form for both registering and for editing the users details later?

I load the view with the $new or $edit variable set, depending on what mode I want to be in. If not variable is set, the form loads in view mode onle.

Then in the view, my code looks like this:
Code:
<?=if(isset($new)):?>
    <?=form_input('first_name')?>
<?=elseif(isset($edit)):?>
    <?=form_input('first_name', $row->first_name)?>
<?=else:?>
    <?=$row->first_name?>
<?=endif;?>
#2

[eluser]bretticus[/eluser]
Don't pass your row to the view. Instead create variables in the controller (like $first_name) and set them to an empty string when not in edit mode. You can still use one view without having 40 if else then blocks in it Smile Pass through the form action as a variable too and you can process the same view form differently (insert/update.)
#3

[eluser]Samuurai[/eluser]
Great! That's good advice!

Should I make a new view for displaying the data?
#4

[eluser]bretticus[/eluser]
[quote author="Samuurai" date="1253748597"]
Should I make a new view for displaying the data?[/quote]

Yes, since you'd end up changing the view entirely, you should just have another view. Of course it really wouldn't have hurt to have had a duplicate view of your form in the first place.
#5

[eluser]Samuurai[/eluser]
Ok, brilliant.. thanks for that!
#6

[eluser]Samuurai[/eluser]
I think i'm having a bit of a dull moment, but I was looking at my code and wondering if I do really need a foreach when displaying the query results.

My model does the query and returns $query->result();

I then pass that to my view as $data->result

Then in my view I do a

foreach($result as $row)

Then I access the data using $row->firstname or whatever.

I want to pass the data to my views from the controller.. I've tried various differnt combinations of $result->row()->firstname, but I keep getting errors like "Call to member function on a non-object.

Do I really need the foreach?
#7

[eluser]wabu[/eluser]
Hello, if this is for a single row, you can do the following in your model:

Code:
return $query->row();
#8

[eluser]Samuurai[/eluser]
Ah right... I think I've got it now.

I then did $row = $result[0] And I can access everything using $row->firstname!

Woohoo!

thanks everyone for your help!




Theme © iAndrew 2016 - Forum software by © MyBB