Welcome Guest, Not a member yet? Register   Sign In
Passing Variables from View to Model Through Controller
#1

[eluser]lenwood[/eluser]
Hi All, I'm in the middle of building a blog with CI, and I'm stuck with writing to multiple tables. I have a many-to-many relationship between blog entries and categories. I've spent the better part of today trying to get it to update both the entries table, and the relations table.

Here's what's passed to the controller from my view (a form in the admin section):
Code:
<?php echo form_open('admin/blogback/submit_new'); ?>
<p>&lt;input type="text" size="35" name="title" /&gt;&lt;/p>
<p>&lt;textarea name="body" id="body"&gt;&lt;/textarea></p>
<p>Select Categories:</p>
<p>&lt;?php foreach($categories as $row): ?&gt;
&lt;input type="checkbox" name="c_id" value="&lt;?php echo $row-&gt;id; ?&gt;" />&lt;?php echo $row->name; ?&gt;<br />
&lt;?php endforeach; ?&gt;</p>
<p>&lt;input type="submit" value="Submit" /&gt;&lt;/p>
&lt;/form&gt;

Here's my controller:
Code:
function submit_new()
{
    $this->load->model('blogback_model');
    $x = $this->input->post('title');
    $ptitle = url_title($x, 'dash', TRUE);
    $data['date_time'] = standard_date('DATE_W3C', time());
    $data['post_title'] = $x;
    $data['post_slug'] = $ptitle;
    $data['body'] = $this->input->post('body');
    $this->blogback_model->submit_entry($data);
    $poca['post_slug'] = $ptitle;
    $poca['cat'] = $this->input->post('c_id');
    $this->blogback_model->submit_poca($poca);
}

The blog entry gets posted to the entries table, but I haven't yet figured out how to access the 'post_slug' or 'cat' within the submit_poca function. My intent is to read the entries table to find the id of the post associated with my post_slug (the newly written blog entry) and then save 'post_id' and 'c_id' to the post_cat table.

How do I access post_slug and cat within the model? Am I making this too complicated?

Thanks in advance.
#2

[eluser]pickupman[/eluser]
You would want to retrieve your id you just inserted record.
Code:
$data['body'] = $this->input->post('body');
$this->blogback_model->submit_entry($data);
$post_id = $this->db->insert_id(); //Get inserted id

$post = $this->blobback_model->get($post_id)->row(0); //get info from inserted record
$post_slug = $post->post_slug;
$post_cat = $post->cat;

$poca['post_slug'] = $ptitle;
$poca['cat'] = $this->input->post('c_id');
$this->blogback_model->submit_poca($poca);
#3

[eluser]lenwood[/eluser]
Thanks very much for the assist pickupman. The two things that made this come together for me are the array helper and:
Code:
$this->db->insert_id();




Theme © iAndrew 2016 - Forum software by © MyBB