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

Nodejs Events

Events in Node.js The EventEmitter Object You can assign event handlers to your own events with the EventEmitter object. In the example below we have created a function that will be executed when a "scream" event is fired. To fire an event, use the  emit()  method. var events = require('events'); var eventEmitter = new events.EventEmitter();  //Create an event handler:  var myEventHandler = function () {   console.log('I hear a scream!');  }  //Assign the event handler to an event: eventEmitter.on('scream', myEventHandler);  //Fire the 'scream' event:  eventEmitter.emit('scream');

Family get-together with Ramadan Ifthar 2017

Family get-together with Ramadan Ifthar 2017

Node.js and Raspberry Pi

Raspberry Pi is a small, multi-use computer. With Node.js you can do amazing things with your Raspberry Pi. What is the Raspberry Pi? The Raspberry Pi is a small, affordable, and amazingly capable, credit card size computer. It is developed by the Raspberry Pi Foundation, and it might be the most versatile tech ever created. Creator Eben Upton's goal was to create a low-cost device that would improve programming skills and hardware understanding. Due to the small size and price of the device, it has become the center of a wide range of projects by tinkerers, makers, and electronics enthusiasts. Raspberry Pi and Node.js The Raspberry Pi has a row of GPIO (General Purpose input/output) pins, and these can be used to interact in amazing ways with the real world. This tutorial will focus on how to use these with Node.js. Write Raspbian OS Image to MicroSD Card Before we can start using our Raspberry Pi for anything, we need to get a OS installed. ...