Welcome Guest, Not a member yet? Register   Sign In
can i pass data from view to controller?
#1

[eluser]brian88[/eluser]
im used to grabbing data from the uri segments like -> controller/model/id
so here i would grab the id and thats how my mvc passes variables together.

But here i need to grab an id, which is "postId", without uri segments. This postId is located in my view in a hidden input field. The controller needs this id for the comments section to work properly on this blog.

Comments needs to be linked to a postId without using uri segments. How can I do this?

controller
Code:
function index() {

  // model data
  $data['posts'] = $this->main_mod->getPosts();
  // this variable doesnt work below.
  //$postId = $this->input->post('postId'); returns false...this cant work because this value is only valid on form submit. correct?

  $postId = '33'; // this is a hard coded # that works. but NEEDS TO BE DYNAMIC. I need the postId in the view

  $data['comments'] = $this->main_mod->getComments( $postId ); // comments needs a postId from view

  // views
  if($this->session->userdata('logged_in') === true) {
   $this->load->view('header_logged_in', $data);
   $this->load->view('home', $data);
   $this->load->view('footer');
  }else{
   $this->load->view('header', $data);
   $this->load->view('home', $data);
   $this->load->view('footer');
  }
} // end function

view
Code:
<div id="content" class="home cfix">
  &lt;?php foreach($posts as $p): ?&gt;

   &lt;!-- Start Post --&gt;
   <h1><a href="&lt;?php echo site_url(">id . "/" . url_title($p->title, true)); ?&gt;">&lt;?php echo $p->title; ?&gt;</a></h1>
   <p>&lt;?php echo auto_link($p->body, 'url', true); ?&gt;</p>
   &lt;!-- End Post --&gt;
   <br />
   &lt;!-- Start Comments --&gt;
   &lt;?php foreach($comments as $c): ?&gt;
   <p>&lt;?php echo $c->comment; ?&gt;: </p>
   &lt;?php endforeach; ?&gt;
   &lt;!-- End Comments --&gt;

   &lt;form action="&lt;?php echo base_url('main/add_comment_db'); ?&gt;" method="post" accept-charset="utf-8"&gt;
    &lt;textarea id="comment" class="blank" name="comment"&gt;Comment...&lt;/textarea&gt;
    &lt;input type="hidden" name="postId" value="&lt;?php echo $p-&gt;id; // i need this value for my controller ?&gt;"&gt;

    &lt;input type="submit" name="submit" value="Post"&gt;
   &lt;/form&gt;

   <hr /><div class="clear"></div>
  &lt;?php endforeach; ?&gt;

  </div>&lt;!-- #Content --&gt;

model
Code:
// get all posts
function getComments($postId) { // need postId from view
  $q = $this->db->query("
   select comment, postId, c.id, p.id, c.date, p.userId
   from comments as c
   left join posts as p on (c.postId = p.id)
   where p.id = ?
  ", array($postId));
  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function
#2

[eluser]vitoco[/eluser]
i think you got it wrong, the controller call the view to render the html page, so the $p->id is printed as value of the hidden input, so it can be sended via POST and be readed as
Code:
$id = $this->input->post('postId');

in 'main/add_comment_db', controller 'main', method 'add_comment_db' i guess, in another call
#3

[eluser]brian88[/eluser]
in my controller i do have that line of code you just mentioned. But it returns false or null.

Prolly because im just loading the page and need that variable. im not submitting no forms so $this->input->post wont work....right?
#4

[eluser]CroNiX[/eluser]
Nope, the form needs to submit in order for there to be POST data.
#5

[eluser]Unknown[/eluser]
Maybe you can use AJAX for that.
#6

[eluser]CroNiX[/eluser]
I'd stick it in a cookie or in session, or even as the third segment in your form_open (and then you can read it as a segment too)
#7

[eluser]gRoberts[/eluser]
Seems like a lot of work to just output the posts and their comments.

As you have the list of posts your going to display, you therefore have each of the posts ID's. With this in mind, I'd collect the list of ID's from the Posts you've just got and then using a "where in" query, return all of the comments that relate to all of those Posts.

i.e.

Code:
&lt;?

$Posts = $this->main_mod->getPosts();

$PostIDs = array();
foreach($Posts as $Post)
  $PostIDs[] = $Post->id;
reset($Posts);

$Comments = $this->main_mod->getComments($PostIDs);

foreach($Posts as $Index => $Post)
{
  $Post->Comments = array();
  foreach($Comments as $Comment)
  {
   if($Comment->postId == $Post->id)
   {
    $Post->Comments[$Comment->id] = $Comment;
   }
  }
  $Posts[$Index] = $Post;
}
reset($Posts);

$this->load->view('page', array('posts' => $Posts));

?&gt;

Code:
<div id="content" class="home cfix">
  &lt;?php foreach($posts as $p): ?&gt;

   &lt;!-- Start Post --&gt;
   <h1><a href="&lt;?php echo site_url(">id . "/" . url_title($p->title, true)); ?&gt;">&lt;?php echo $p->title; ?&gt;</a></h1>
   <p>&lt;?php echo auto_link($p->body, 'url', true); ?&gt;</p>
   &lt;!-- End Post --&gt;
   &lt;? if(!empty($p->Comments)) : ?&gt;
   <br />
   &lt;!-- Start Comments --&gt;
   &lt;?php foreach($p->Comments as $c): ?&gt;
   <p>&lt;?php echo $c->comment; ?&gt;: </p>
   &lt;?php endforeach; ?&gt;
   &lt;!-- End Comments --&gt;
   &lt;? endif; ?&gt;

   &lt;form action="&lt;?php echo base_url('main/add_comment_db'); ?&gt;" method="post" accept-charset="utf-8"&gt;
    &lt;textarea id="comment" class="blank" name="comment"&gt;Comment...&lt;/textarea&gt;
    &lt;input type="hidden" name="postId" value="&lt;?php echo $p-&gt;id; // i need this value for my controller ?&gt;"&gt;

    &lt;input type="submit" name="submit" value="Post"&gt;
   &lt;/form&gt;

   <hr /><div class="clear"></div>
  &lt;?php endforeach; ?&gt;

  </div>&lt;!-- #Content --&gt;

Note: You would most certainly have to refine the above code. It may not be very efficient but it will do what your needing.
#8

[eluser]brian88[/eluser]
Thanks for the help fellas. gRoberts I'm tackling your code here and here are some of my problems.

I have my comments as a separate table in my database. so my tables are, users-comments-posts... so i cant use $post->comments because comments is in a separate table.

My main problem is
Code:
$data['comments'] = $this->main_mod->getComments($postIds);
$postIds is an array of ids... so i get a database error of

Code:
Unknown column 'Array' in 'where clause'

select comment, postId, c.id, p.id, c.date, p.userId
from comments as c
left join posts as p on (c.postId = p.id)
where p.id = Array
p.id has to be a single id. not an array of ids

look at my model in my first post again
Code:
$q = $this->db->query("
   select comment, postId, c.id, p.id, c.date, p.userId
   from comments as c
   left join posts as p on (c.postId = p.id)
   where p.id = ? // needs to be a single id
  ", array($postId));
#9

[eluser]gRoberts[/eluser]
Hi there,

As mentioned before showing any code, you would need to use the "in" statement when passing the PostID's.

You will need to change, or at least create a new method within your model to something like the following:

Code:
public function getComments($PostIDs)
{
  if(!is_array($PostIDs))
   $PostIDs = array($PostIDs);
  $this->db->select('comments.comment, comments.postId, comments.id, posts.id, comments.date, posts.userId');
  $this->db->join('posts', 'posts.id = comments.postId', 'left');
  $this->db->where_in('posts.id', $PostIDs);
  $q = $this->db->get('comments');
  $Ret = array();
  foreach($q->result() as $Comment)
   $Ret[] = $Comment;
  return $Ret;
}

Within your controller, as I had posted in my initial reply, you will see how the $Post->Comments is populated. I was getting all of the posts, then all of the comments related to those posts and then looping through the posts and assigning each posts comments to the post.

This is far better than looping through each of the posts and then querying the database for the list of comments.
#10

[eluser]brian88[/eluser]
Oh i understand, i never used a where in before...

So my next problem is that I do not have a comment column inside my posts table. So I get an error for undefined variable comment.
Code:
$p->$comments = array();
For my posts table, I do not have a comment column... are you saying I need a new column and this post will have an array of comments?

controller
Code:
function index() {

  // model data
  $data['posts'] = $this->main_mod->getPosts();

  $postIds = array();
  foreach ($data['posts'] as $p) {
   $postIds[] = $p->id;
  }
  reset($data['posts']);
  $data['comments'] = $this->main_mod->getComments($postIds);

  //var_dump($data['comments']);

  foreach ($data['posts'] as $index => $p) {
   $p->$comments = array(); // i dont not have a $p->comments
    foreach($data['comments'] as $c){
    if($c->postId == $p->id){
     $p->$comments[$c->id] = $c;
    }
   }
   $data['posts'][$index] = $p;
  }

  // views
   if($this->session->userdata('logged_in') === true) {
    $this->load->view('header_logged_in', $data);
    $this->load->view('home', $data);
    $this->load->view('footer');
   }else{
    $this->load->view('header', $data);
    $this->load->view('home', $data);
    $this->load->view('footer');
   }
} // end function




Theme © iAndrew 2016 - Forum software by © MyBB