Welcome Guest, Not a member yet? Register   Sign In
Controller executing twice - Solved
#1

[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>&lt;?php echo $heading;?&gt;</h3>
<p>&lt;?php echo $content;?&gt;</p>

<table>
    <tr>
        <th>id</th>
        <th>date</th>
        <th>title</th>
        <th>edit</th>
        <th>delete</th>
    </tr>
&lt;?php foreach($query->result() as $row): ?&gt;
    <tr>
        <td>&lt;?=$row->id?&gt;</td>
        <td>&lt;?=$row->date?&gt;</td>
        <td>&lt;?=$row->title?&gt;</td>
        <td>&lt;?=anchor('modify/edit/'.$row->id, 'edit')?&gt;</td>
        <td>&lt;?=anchor('modify/delete/'.$row->id, 'delete')?&gt;</td>
    </tr>
&lt;?php endforeach; ?&gt;
</table>
&lt;/body&gt;
&lt;/html&gt;


edit_view.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h3>&lt;?php echo $heading;?&gt;</h3>
<p>&lt;?php echo $content;?&gt;</p>
&lt;?php foreach($query->result() as $row): ?&gt;
&lt;?=form_open('modify/save/'.$row->id)?&gt;

&lt;input type="text" name="id" value="&lt;?=$row-&gt;id?&gt;" disabled="disabled" />
&lt;input type="text" name="parent_id" value="&lt;?=$row-&gt;parent_id?&gt;" /><br />
&lt;input type="text" name="title" value="&lt;?=$row-&gt;title?&gt;" />
&lt;input type="text" name="author" value="&lt;?=$row-&gt;author?&gt;" /><br />
&lt;input type="text" name="tags" value="&lt;?=$row-&gt;tags?&gt;" /><br />
&lt;textarea name="body" cols="60" rows="20"&gt;&lt;?=$row->body?&gt;&lt;/textarea&gt;&lt;br />
&lt;input type="text" name="date" value="&lt;?=$row-&gt;date?&gt;" />
&lt;input type="submit" value="save" /&gt;
&lt;/form&gt;

&lt;?php endforeach; ?&gt;
&lt;/body&gt;
&lt;/html&gt;


Any help would be appreciated
#2

[eluser]WanWizard[/eluser]
Check for missing files in your page (images, css files, etc) that trigger a rewrite to index.php.
#3

[eluser]Eric Barnes[/eluser]
Just to add, it would be a very good idea to be using the form validation to be sure of the data you are receiving. And also try and turn off url rewriting to see if that is culprit as WanWizard said.
#4

[eluser]noolan[/eluser]
WanWizard
Quote:Check for missing files in your page (images, css files, etc) that trigger a rewrite to index.php.
I'm not calling any other files. Unless there is something that CI looks for by default.


Eric Barnes
Quote:Just to add, it would be a very good idea to be using the form validation to be sure of the data you are receiving.
Thanks for the advice. I plan to implement form validation eventually. (After i get the basics working)


-
Quote:And also try and turn off url rewriting to see if that is culprit as WanWizard said.
Do you mean in my php config file?
#5

[eluser]Eric Barnes[/eluser]
Yea and through htaccess. Like wanwizard said if an image or something else is not found it could be reloading that page.
#6

[eluser]noolan[/eluser]
I have not implemented a htaccess file yet but I will try disabling url rewriting
#7

[eluser]WanWizard[/eluser]
you can add
Code:
log_message('debug', 'URI: '.$this->uri->uri_string());
to your controller constructor. This will log the requested URI to the log file. Maybe that will help you find the culprit.
#8

[eluser]noolan[/eluser]
Thanks thats useful.

For now my problem seems to be solved.
I've been messing around with so many settings that I can't say for sure what fixed it.
I will reply again if I run into it again.




Theme © iAndrew 2016 - Forum software by © MyBB