Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP find and save/add operation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 103
    Comment on it

    Here is small example which will give you a basic idea about CakePHP find & save operation.

    <?php
    class PagesController extends AppController {
    
        public $name = 'Pages';
        public $uses = array('User');
        public function show() {
        if (!empty($this->request->data)) {
    
          $this->User->save($this->request->data);
        }
     
        $Users = $this->User->find('all');
        $this->set('Users', $Users);
        $this->render('Add');
        }
    }

    find () function: find all users from database.
    set() function: Pass user data to view.
    render() function: show output in add.ctp instead of default show.ctp
    save() function: save requested data in DB.

     

    Add.ctp

    <?php
    echo $this->Form->create('User');
    echo $this->Form->input('firstname');
    echo $this->Form->input('lastname');
    echo $this->Form->input('email');
    echo $this->Form->end('Add');
    ?>
    <hr/>
    <h1>Current Users</h1>
    
    <table>
    <tr><th>First Name</th><th>Last name</th><th>Email</th></tr>
    <?php
    foreach ($Users as $User):
    ?>
    <tr>
    <td><?php echo $User['User']['firstname']; ?></td>
    <td><?php echo $User['User']['lastname']; ?></td>
    <td><?php echo $User['User']['email']; ?></td>
    </tr>
    <?php
    endforeach;
    ?>
    </table>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: