CodeIgniter Forums
Brainstew over form population - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Brainstew over form population (/showthread.php?tid=24911)



Brainstew over form population - El Forum - 11-24-2009

[eluser]CodeTroll[/eluser]
My goal is to use the same view/template for editing and creating a blog entry. I started out with form validation as per the documentation and that works fine when submitting the form.
My next step was to prepopulate the form with data from the database.

As can be seen from the 'blog_entry' => $this->blog_model->get_entry($blogid) the result array is one level lower than the other fields in $data. I guess that is the problem, but I am
not sure how to fix it..

I could get each field from the database array and add it manually to the $data array, but if an easier way exists I would appreciate it Smile


Relevant database fields:
Code:
blogid
titel
tekst

Sorry some of the text is in danish - but it should be understandable anyway (rediger=edit)
Snippet from controller:
Code:
function rediger($blogid) {
        $site_title = $this->config->item('sb_site_title')."Blog - rediger";
        $data = array ('site_title' => $site_title,
                       'local_menu' => $this->_get_local_menu(),
                       'blog_entry' => $this->blog_model->get_entry($blogid)
        );
        $this->parser->parse('blog/blog_rediger', $data);
    }


Snippets from view/template blog_rediger.php
Code:
<?php echo validation_errors(); ?>
<?php echo form_open('blog/gem'); ?>
<input type="hidden" name="blogid" value="<?php echo set_value('blogid'); ?>" />
Titel<br />
&lt;input type="text" name="titel" value="&lt;?php echo set_value('titel'); ?&gt;" size="80"  maxlength="150"/&gt;
<br />Tekst<br />
&lt;textarea name="tekst" cols="80" rows="15"/&gt;&lt;?php echo set_value('tekst'); ?&gt;&lt;/textarea&gt;
<div>&lt;input type="submit" value="Gem indl&aelig;g" /&gt;&lt;/div>
&lt;/form&gt;



Brainstew over form population - El Forum - 11-24-2009

[eluser]jedd[/eluser]
I think, but am not completely sure, that you may just be looking for a reminder that the set_value() function takes two parameters.


Brainstew over form population - El Forum - 11-24-2009

[eluser]CodeTroll[/eluser]
Hmm I already tried that - it may just be my lack of array handling in PHP that fries my brain.

blog_entry is the key and then I have the row as the value - but can I dig that out in a oneliner or have I missed something?

(still sifting through the Bamboo application to get some ideas before I get too far in my own code ramblings Smile)


Brainstew over form population - El Forum - 11-24-2009

[eluser]Flemming[/eluser]
if in doubt with arrays, just use your old friend print_r($array) in your view (in your case, print_r($blog_entry) - this will give you a clue how to extract the data that is contained in $blog_entry


Brainstew over form population - El Forum - 11-24-2009

[eluser]CodeTroll[/eluser]
Problem is solved.. well - sorta..

I changed from $this->Parser-parse('blog_edit',$data) to $this->load->view('blog_edit',$data)
and my data came out... so it seems there is a difference as how $data is available in views and in templates..

Hmmmm