Welcome Guest, Not a member yet? Register   Sign In
CRUD help
#1

[eluser]HSKrustofsky[/eluser]
I know there are a ton of posts on this, but cant seem to find one that will help me out. I am relatively new to CI, and need some help. I am creating a site to where the customer can input information in a form, submit it, and it automatically inputs the information in its perspective places.

Well, I got it to where the information can be manually inserted in phpmyadmin, but I am trying to make kind of like an admin panel to make it as easy as possible for the customer.

My problem is one, when I add a new post, it won't redirect me back to the panel view. Two, I have to manually go to the panel view when adding, and then when I do I see that it has added a post with a 0(added on top of the post I initially added). Three, I cannot seem to be able to update information. Can anyone help me? Here is the code I have:

admin_model.php:
Code:
<?php

class Admin_model extends Model {
    
    function Admin() {
        parent::Model();
    }
    
    function create($data) {

        $this->db->insert('data', $data);
        return $this->db->insert_id();

    }

    function retrieve() {

        $query = $this->db->get('data');
        return $query->result();

    }

    function update($data) {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->update('data', $data);

    }

    function delete() {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');

    }
    
}

admin.php
Code:
<?php

class Admin extends Controller {
    
    function Admin() {
        parent::Controller();
    }
    
    function index() {
    
        $data = array();
        $this->load->model('admin_model');
    
        if($query = $this->admin_model->retrieve()) {
            $data['records'] = $query;
        }

        $data['title'] = "Admin";
        $this->load->view('panelview', $data);

    }

    function create() {

        $data = array(
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
        );
        
        $this->load->model('admin_model');
        $this->admin_model->create($data);
        $this->load->view('addnews', $data);

    }

    function delete() {
        
        $this->load->model('admin_model');
        $this->admin_model->delete();
        $this->index();

    }

    function update() {

        $data = array(
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
        );
        
        $this->load->model('admin_model');
        $this->admin_model->update($data);
        $this->load->view('editnews', $data);

    }

}

addnews.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
        <h1 align="center">Add News</h1>
        <table cellpadding="5px" cellspacing="0" border="1" bordercolor="#000" align="center">
            <tr>
                <td>
                    &lt;?= form_open('../admin/create') ?&gt;
                        <ul>
                            <li>
                                <label>Title:</label>
                                &lt;?= form_input('title') ?&gt;
                            </li>
                            <li>
                                <label>Content:</label>
                                &lt;?= form_input('contents') ?&gt;
                            </li>
                            <li>
                                &lt;?= form_submit('', 'Submit') ?&gt;
                            </li>
                        </ul>
                    &lt;?= form_close() ?&gt;
                </td>
            </tr>
        </table>
    &lt;/body&gt;
&lt;/html&gt;

editnews.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
        <h1 align="center">Edit News</h1>
        <table cellpadding="5px" cellspacing="0" border="1" bordercolor="#000" align="center">
            <tr>
                <td>
            &lt;?php echo form_open('../admin/update'); ?&gt;

                <p>
                    <label for="title">Title:</label>
                    &lt;input type="text" name="title" id="title" /&gt;
                </p>
                <p>
                    <label for="contents">Content:</label>
                    &lt;input type="textarea" name="content" id="contents" /&gt;
                </p>
                <p>
                    &lt;input type="submit" value="Submit" /&gt;
                </p>

                &lt;?php echo form_close(); ?&gt;
                </td>
            </tr>
        </table>
    &lt;/body&gt;
&lt;/html&gt;

If anyone could help me that would be great.
#2

[eluser]HSKrustofsky[/eluser]
Me again! Well, I figured out how to redirect pack to the panel view. Still can't figure out how to update, and for some reason, when I add a post, right above each new post I do, it adds a post with 0 in it. Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB