I am trying to add comments on a joke thread, but I am having trouble grabbing the joke_id which should be used to amend the comments on the post.
The problem i face is, the thread with no comments on will not add the joke_id to the database, and the thread with comments on works fine. I have no idea why.
This is a working example of what I am talking about:
http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/14
This thread is fully working (i just emphasised the joke_id for test purposes)
and this thread does not work:
http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/4
The comment and name is added to the database, but the joke_id is down as '0' and i have no idea why.
Here is my code in the view:
<?php
//assigning values
foreach($results as $row){
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
$date_added = $row->date_added;
$vote = $row->vote;
?>
}
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea required="yes" class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
<?php
echo form_close(); ?>
Here is my controller:
public function index(){
$this->getComment();
}
public function getComment(){
$data['results'] = $this->comments_m->getComments();
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('content/comment_box', $data);
$this->load->view('template/footer');
}
public function insertComment(){
$data = array (
'user' => 'user',
'comment' => 'comment',
'joke_id' => 'joke_id',
'id_post' => 'joke_id'
);
$joke_id = $this->input->post('joke_id');
$this->comments_m->insertComment($data);
redirect(base_url('read/joke/'.$joke_id));
}
}
model:
//gets the comments
function getComments ($joke_id){
$query = $this->db->query("SELECT c.name, j.*, co.* FROM jokes j LEFT JOIN category c ON c.category_id = j.category_id LEFT JOIN comments co ON co.joke_id = j.joke_id WHERE j.joke_id = '$joke_id' ") or die("No results found" );
if ($query->num_rows > 0) {
return $query->result();
}
}
//inserts the comments
function insertComment (){
$data = array (
'user' => $this->input->post('name-com'),
'comment' => $this->input->post('the-new-com'),
'joke_id' => $this->input->post('joke_id'),
'id_post' => $this->input->post('joke_id')
);
if(strlen($data['user']) < 1){
$data['user'] = "Guest";
}
$this->db->insert('comments', $data);
}
comments table:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(40) NOT NULL,
`comment` text NOT NULL,
`joke_id` int(11) NOT NULL,
`id_post` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
jokes table:
CREATE TABLE IF NOT EXISTS `jokes` (
`joke_id` int(11) NOT NULL AUTO_INCREMENT,
`joke` varchar(1024) NOT NULL,
`category_id` int(11) NOT NULL,
`vote` int(255) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Any help as to why the joke_id doesn't insert/show on pages with no jokes would be great.
Thank you