[eluser]noolan[/eluser]
Just starting my first app with CodeIgniter.
I'm running into a strange issue.
I first noticed it when running insert statements, two duplicate entries were being added to the database.
To investigate I changed the log threshold to 4 and discovered that my controller is running twice no matter the function being called.
Heres my controller and two view files if it helps.
(its the start of a basic cms)
modify.php
Code:
<?php
class Modify extends Controller{
function Modify(){
parent::Controller();
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['title'] = 'Modify your site - Dashboard';
$data['heading'] = 'Dashboard';
$data['content'] = anchor('modify/add', 'Add New Page');
$data['query'] = $this->db->get('Content');
$this->load->view('dashboard_view',$data);
}
function add(){
// open edit_view with blank form
$data['title'] = 'Modify your site - Add';
$data['heading'] = 'Add New Page';
$data['content'] = anchor('modify', 'back');
$this->db->set('title', 'new post');
$this->db->insert('Content');
$this->db->where('id', $this->db->insert_id());
$data['query'] = $this->db->get('Content');
$this->load->view('edit_view',$data);
}
function edit(){
$data['title'] = 'Modify your site - Edit';
$data['heading'] = 'Edit Existing Page';
$data['content'] = anchor('modify', 'back');
$this->db->where('id', $this->uri->segment(3));
$data['query'] = $this->db->get('Content');
$this->load->view('edit_view',$data);
}
function save(){
$newData = array(
'parent_id' => $_POST['parent_id'],
'title' => $_POST['title'],
'author' => $_POST['author'],
'body' => $_POST['body'],
'tags' => $_POST['tags'],
'date' => $_POST['date']
);
$this->db->where('id', $this->uri->segment(3));
$this->db->update('Content', $newData);
redirect(site_url('modify'));
}
function delete(){
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('Content');
redirect(site_url('modify'));
}
}
?>
dashboard_view.php
Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h3><?php echo $heading;?></h3>
<p><?php echo $content;?></p>
<table>
<tr>
<th>id</th>
<th>date</th>
<th>title</th>
<th>edit</th>
<th>delete</th>
</tr>
<?php foreach($query->result() as $row): ?>
<tr>
<td><?=$row->id?></td>
<td><?=$row->date?></td>
<td><?=$row->title?></td>
<td><?=anchor('modify/edit/'.$row->id, 'edit')?></td>
<td><?=anchor('modify/delete/'.$row->id, 'delete')?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
edit_view.php
Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h3><?php echo $heading;?></h3>
<p><?php echo $content;?></p>
<?php foreach($query->result() as $row): ?>
<?=form_open('modify/save/'.$row->id)?>
<input type="text" name="id" value="<?=$row->id?>" disabled="disabled" />
<input type="text" name="parent_id" value="<?=$row->parent_id?>" /><br />
<input type="text" name="title" value="<?=$row->title?>" />
<input type="text" name="author" value="<?=$row->author?>" /><br />
<input type="text" name="tags" value="<?=$row->tags?>" /><br />
<textarea name="body" cols="60" rows="20"><?=$row->body?></textarea><br />
<input type="text" name="date" value="<?=$row->date?>" />
<input type="submit" value="save" />
</form>
<?php endforeach; ?>
</body>
</html>
Any help would be appreciated