[eluser]Unknown[/eluser]
Hello Everyone, i´m new to CI, and i wish somebody could help me. I created a table with and id and sometext...also created a model who retrieves information, a controller and a view...the thing is that in the view i put an input for deleting the information retrieved from the database...
So my error is that it doesnt matter which delete input i click...it deletes the last row in the table...please somebody help...thank you everyone... sorry for my bad english... :S
Controller
Code:
<?php
class Jokes extends CI_Controller
{
function __construct(){
parent::__construct();
$this->load->model("MJokes");
}
public function index(){
$data['chistes'] = $this->MJokes->obtenerTodos();
$this->load->view("jokes",$data);
}
public function addJoke(){
$this->load->view("addjoke");
}
function createJoke(){
if ($this->input->post('joketext')){
$this->MJokes->agrChiste();
redirect('jokes/index','refresh');
}
}
function deleteJoke($id){
if ($this->input->post('id')){
$this->MJokes->brrChiste($id);
}
redirect('jokes/index','refresh');
}
}
?>
Model
Code:
?php
class MJokes extends CI_Model
{
function __construct(){
parent::__construct();
}
function obtenerTodos(){
$keri = $this->db->query("SELECT id, joketext FROM joke");
return $keri->result_array();
}
function agrChiste(){
$data = array('joketext' => $_POST['joketext'],
'jokedate' => $_POST['jokedate'],
// 'authorname' => $_POST['authorname'],
// 'authoremail' => $_POST['authoremail']
);
$this->db->insert('joke',$data);
}
function brrChiste($id){
$this->db->delete('joke', array('id' => $id));
}
}
?>
The View
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jokes</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
</head>
<body>
<div>
<header>
<h1>The funniest jokes of all time!!!</h1>
</header>
<section>
<?php
foreach($chistes as $joke): ?>
<?php echo form_open('jokes/deleteJoke');?>
<blockquote><p>
<?php
echo $joke['joketext'];
$data = array(
'name'=>'id',
'value'=>$joke['id']
);
echo form_input($data);
echo form_submit('delete','Borrar Chiste');
?>
</p></blockquote>
<?php endforeach; ?>
</section>
<aside>
<a >Cuéntanos un chiste.</a>
</aside>
<footer>
<p>
© Copyright by Sistemas
</p>
</footer>
</div>
</body>
</html>