• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I just don't get it, I tell ya!!

I created an MVC setup that works great! Shows a form, POSTs the values then takes the data and inserts it to the database.

Now, I want to use the same form view to display the row for update purposes.

So I created an update function in the controller that successfully retrieves an array with the row in it based on $id, but I fail to understand how to pass that data to the view so that the values are pre-populated on the form.

The form view in embedded using a load into my template view by way of a content identifier that I pass to the template view. The view looks like this:

<?php echo form_open('advertisers/$action'); ?>
        <p class="red floatRight clearBoth">All fields are required. </p>
        <legend>&lt;?php echo $header; ?&gt; Form</legend>
                    <td>Network id</td>
                    <td>&lt;input type="text" name="network_id" value="&lt;?php echo set_value('network_id'); ?&gt;" size="2" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('network_id'); ?&gt;</td></tr>
                    <td>Affiliate id</td>
                    <td>&lt;input type="text" name="affiliate_id" value="&lt;?php echo set_value('affiliate_id'); ?&gt;" size="10" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_id'); ?&gt;</td></tr>
                    <td>Affiliate name</td>
                    <td>&lt;input type="text" name="affiliate_nme" value="&lt;?php echo set_value('affiliate_nme'); ?&gt;" size="75" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_nme'); ?&gt;</td></tr>
                    <td>Affiliate logo URL</td>
                    <td>&lt;input type="text" name="affiliate_logo_URL" value="&lt;?php echo set_value('affiliate_logo_URL'); ?&gt;" size="150" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_logo_URL'); ?&gt;</td></tr>
                    <td>Affiliate Link</td>
                    <td>&lt;input type="text" name="affiliate_link" value="&lt;?php echo set_value('affiliate_link'); ?&gt;" size="150" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_link'); ?&gt;</td></tr>
                    <td>Public Link</td>
                    <td>&lt;input type="text" name="public_link" value="&lt;?php echo set_value('public_link'); ?&gt;" size="150" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('public_link'); ?&gt;</td></tr>
                    <td>&lt;textarea name="desc_txt" value="&lt;?php echo set_value('desc_txt'); ?&gt;" rows="10" cols="100"&lt;/textarea></td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('desc_txt'); ?&gt;</td></tr>
                    <td>Active indicator</td>
                    <td>&lt;input type="text" name="status_cd" value="&lt;?php echo set_value('status_cd'); ?&gt;" size="1" /&gt;&lt;/td>
                <tr class="error"><td></td><td>&lt;?php echo form_error('status_cd'); ?&gt;</td></tr>
                    <td></td><td>&lt;input type="submit" value="Submit" /&gt;&lt;/td>

them above view back loads the form when posting error messages.
So I created this the model function...
function getAdvsWhere($field,$param)
        return $query->result_array();

The controller does this...
function Advertiser_Update($id)
        $this->load->helper(array('form', 'url', 'html'));
        $this->load->model('Advertiser_model', 'advs');
        $row = $this->advs->getAdvsWhere('id', $id);
        $data = array (  'network_id'           => $row[0]['network_id']
                        ,'affiliate_id'         => $row[0]['affiliate_id']
                        ,'affiliate_nme'        => $row[0]['affiliate_nme']
                        ,'affiliate_logo_URL'   => $row[0]['affiliate_logo_URL']
                        ,'affiliate_link'       => $row[0]['affiliate_link']
                        ,'public_link'          => $row[0]['public_link']
                        ,'desc_txt'             => $row[0]['desc_txt']
                        ,'status_cd'            => $row[0]['status_cd']
        $data['title']      ='-----.com : Update Advertisers';
        $data['header']     ='Update Advertiser';
        $data['action']     ='update';
        $data['segment']    ='admin/';
        $data['viewname']   ='advertiser_form';
I tried this with a harcoded URI "advertiser_update/1", the $id=1, the result array is populated correctly, but I can't get the form to pre-populate. this doesn't work and I'm lost... and I don't know how to get the controller to somehow populate the fields being referenced in the view by the set_value functions, how, where?

Take another look at the set_value() function in the User Guide (http://ellislab.com/codeigniter/user-gui...elper.html).

The set_value() function only returns a value when that value is in the POST vars or a default has been set, not when a similar named variable is passed to the View.

Since you're already passing them to the view as variables you only have to pass them to the set_value() function as the second parameter, which is the default value.

For example, this one:
&lt;?php echo set_value('public_link'); ?&gt;
Should become:
&lt;?php echo set_value('public_link', $public_link); ?&gt;

And one additional suggestion: in the controller you expect only 1 value in return with:
$row = $this->advs->getAdvsWhere('id', $id);
But because you use result_array() you have to get your results by getting the first value of the array ([0]). You could also use row_array() which only returns the first row of the result.

Thank you Jelmer!

I greatly appreciate your help, and it helped a great deal.

I have unfortunately not been able to get the set_value() function to populate the TEXTAREA in my form. It work for all INPUT TYPE="TEXT" fields but not for the TEXTAREA field.

Any clues on where I can fix this in the core logic, if indeed it needs fixing?

That's easily explained if your textarea still looks the same:
&lt;textarea name="desc_txt" value="&lt;?php echo set_value('desc_txt'); ?&gt;" rows="10" cols="100"&lt;/textarea>

That should be:
&lt;textarea name="desc_txt" rows="10" cols="100"&gt;&lt;?php echo set_value('desc_txt', $desc_txt); ?&gt;&lt;/textarea&gt;

I can't help feeling like a fool!!
I should not be taking up your time with HTML syntax.

Thank again, and have a Happy New Year!

[eluser]Kip zonder Kop[/eluser]
[quote author="cobolCowboy" date="1262239334"]I can't help feeling like a fool!!
I should not be taking up your time with HTML syntax.

Thank again, and have a Happy New Year![/quote]

You're doing pretty well for a mainframe guy. I appreciate your enthusiasm. In a couple of weeks you'll be answering my questions.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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