Skip to main content

MVC Database

MVC database Edit/update/delete 


Controllers/EditDelete

<?php



class EditDelete extends CI_Controller {

    public function index() {

        $data = array(
            "header" => "Result set of Users",
            "data" => $this->db->get('users')->result()
        );
        $this->load->view('EditDeleteView', $data);
    }

    public function edit($id = "") {
        $this->load->library('form_validation');
        $data = array(
            "header" => "Result set of User Edit",
            "data" => $this->db->get_where('users', array('idu' => $id))
        );
        $this->load->view('EditView', $data);
    }

    public function update() {

   /*     $id = $this->input->post('id');
        $data = array(
            "username" => $this->input->post('username'),
            "email" => $this->input->post('email')
        );

        $this->db->update('users', $data, array('idu' => $id));
*/
     
            $this->load->library('form_validation');
     
        $FormRules = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'required'
        ));


        $this->form_validation->set_rules($FormRules);

        if ($this->form_validation->run() == TRUE) {
            echo "Success";
        } else {
            echo validation_errors();
        }
     
     
    }

}

Views/EditDeleteView


<table border="1">
    <caption>
        <th colspan="5"> <?php echo $header; ?></th>
    </caption>
    <tr>
       
        <th>No</th>
        <th>Username</th>
        <th>Password</th>
        <th>Email</th>       
        <th>Operation</th>
    </tr>
    <?php
    $i = 1;
    foreach ($data as $res) {
        ?>

        <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $res->username; ?></td>
            <td><?php echo $res->password; ?></td>
            <td><?php echo $res->email; ?></td>
            <td><a href="<?php echo base_url('index.php/editdelete/edit/'.$res->idu); ?>">Edit</a> | Delete</td>
        </tr>
        <?php
    }
    ?>
</table>

View/EditView

<html><head><script src="http://localhost/js/jquery.min.js" type="text/javascript"></script>
<script>

$(document).ready(function(){
    $('form.jsForm').on('submit',function(form){
        form.preventDefault();
        $.post('editdelete/update',$('form.jsForm').serialize(),function(data){
                console.log(data);
                $('div.jsError').html(data);
            });
    });
});

</script>
    </head>
    <body>
<?php echo form_open('editdelete/update/', array('class' => 'jsForm')); ?>

<div class="jsError"></div>
<table border="0">



    <?php
    if ($data->num_rows() != 0) {
        $row = $data->row();
        ?>

        <tr>
            <td>Username</td>
        <input type="hidden" name="id" value="<?php echo $row->idu; ?>" >
        <td><input type="text" value="<?php echo $row->username; ?>" name="username"></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><!-- input type="text" value="<?php echo $row->password; ?>" name="password" --><?php echo $row->password; ?></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><input type="text" value="<?php echo $row->email; ?>" name="email"></td>
    </tr>

    <tr>
        <td colspan="2"><input type="submit" value="Save"></td>
    </tr>
    </tr>

<?php } ?>

</table>

<?php echo form_close(); ?>


    </body>
</html>


Comments

Popular posts from this blog

What is Google Adwords Certification Program

What is Google Adwords Certification Program? Google Certification Program is a program through which an individual can earn one of the most valuable status of being a google adwords qualified individual or a company can earn a google adwords certified partner status. Google Adwords Certification wiil show your knowledge, skills and proficiency in google adwords latest tools and it will show that an individual or a company know how to design, implement and manage an adwords campaign and they will use best practices and techniques and follow google adwords policies and guidelines which is one of the most important thing in eyes of Google. Google has launched two types of certification program for Adwords: Individual Qualification. Company Certification. Individual Qualification Individuals who want to add more value to there resume or an individual who want learn google adwords and get certified can study and get Google Adwords Qualified Individual also known as Google Adwo...

Variables in PHP

A variable is a storage area. You put things into your storage areas (variables) so that you can use and manipulate them in your programmers. Things you'll want to store are numbers and text. Variable in PHP You can't begin their names with an underscore (_), or a number. But most other characters are fine. Example 1 : A person is going to be collecting the books. But we can specify how many books he will be collecting. If you have five books   to give him, then you do the "Answer"   is like this: Answer is = 5 Books Ok, now going to the subject …   you're studying   PHP, so there's something missing. Two things, actually. First, your people (variables) need a dollar sign at the beginning (people are like that). So it would be this: $ books = 10 Examples 2 : try to do $books=10+20+30; What is your answer : answer must be 60 Example 3 : Test your mind Find the total books ? $english_book=10; ...

Ministry of Higher Education Project : SLIATE (live project)

ABOUT SLIATE Our Mission  "Creating Excellent Higher National and National Diplomates with Modern Technology for Sustainable Development" Our Vision "To Become the Centre of Excellence in Technological Education" As per the recommendations of the Committee appointed by Prof. Wiswa Waranapala, Deputy Minister of Higher Education in 1994, the Sri Lanka Institute of Advanced Technical Education (SLIATE) was formed in 1995, under the Sri Lanka Institute of Advanced Technical Education Act No. 29 of 1995, In 2001 the name of the institution was amended as Sri Lanka Institute of Advanced Technological Education, (SLIATE). It functions as an autonomous Institute for the management of Higher National and National Diploma courses. The main purposes of establishing SLIATE were to reform and restructure the entire technical and vocational education system in relation to the changing needs of economic development, to meet manpower requirements of natio...