CodeIgniter Forums

Full Version: I just don't get it, I tell ya!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]cobolCowboy[/eluser]
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:

Code:
<?php echo form_open('advertisers/$action'); ?>
    
    <fieldset>
        <p class="red floatRight clearBoth">All fields are required. </p>
        <legend>&lt;?php echo $header; ?&gt; Form</legend>
            <table>
                <tr>
                    <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('network_id'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_id'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_nme'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_logo_URL'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('affiliate_link'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('public_link'); ?&gt;</td></tr>
                <tr>
                    <td>Description</td>
                    <td>&lt;textarea name="desc_txt" value="&lt;?php echo set_value('desc_txt'); ?&gt;" rows="10" cols="100"&lt;/textarea></td>
                </tr>
                <tr class="error"><td></td><td>&lt;?php echo form_error('desc_txt'); ?&gt;</td></tr>
                <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>
                <tr class="error"><td></td><td>&lt;?php echo form_error('status_cd'); ?&gt;</td></tr>
                <tr>
                    <td></td><td>&lt;input type="submit" value="Submit" /&gt;&lt;/td>
                </tr>
            </table>
    </fieldset>        
&lt;/form&gt;

them above view back loads the form when posting error messages.
So I created this the model function...
Code:
function getAdvsWhere($field,$param)
    {
        $this->db->where($field,$param);
        $query=$this->db->get('advertisers');
        return $query->result_array();
        
    }

The controller does this...
Code:
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';
        $this->load->view('common/template',$data);
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?

El Forum

[eluser]Jelmer[/eluser]
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:
Code:
&lt;?php echo set_value('public_link'); ?&gt;
Should become:
Code:
&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:
Code:
$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.

El Forum

[eluser]cobolCowboy[/eluser]
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?

El Forum

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

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

El Forum

[eluser]cobolCowboy[/eluser]
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!

El Forum

[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.

Cheers