Wednesday, November 19, 2014

#codingexercise
Int GetCountDuplicatesMode (int [] A)
{
if (A == null) return 0;
return A.CountDuplicatesMode();
}

In today's post we continue to look at the PHP Implementation of a class based views which are not tied to model. A PHP controller that takes a single input for say a string to do CRUD operation may look something like this:

<?php
require __DIR__ . '/crud.php';
#require_once 'crud.php'
class ExportsController extends AppController {
        public $helpers = array('Html', 'Form');
        public $exports = NULL;
public function index() {
            $orm = new ORM();
            $result = json_decode($orm->list(), true);
            $exports = $result['exports'];
            $ret = array();
            foreach($exports as $export) {
                   array_push($ret, $export['input']);
            }
            $this->set('exports', $ret);
        }
        public function add($export = null) {        
if(isset($this->request->data['input'])) {            
$orm = new ORM();            
$result = json_decode($orm->create($this->request->data['input']));            
if (strpos($result, 'success') !== false){                
$this->Session->setFlash(__('Your export has been saved.'));
                return $this->redirect(array('action' => 'index'));            
}
            $this->Session->setFlash(__('Unable to add your export.'));
         }        
}
: similar for modify or delete
}
Notice we are not using ids of the items in the list and this is usually not recommended. The RESTAPI url comes out to be /exports/modify/9 for example. This is preferable way for REST based API. It doesn't mean that we cannot get the exports working the way we have it. It merely suggests that conforming to the ID based helps with keeping track of the collection when it changes. 

Int GetCountDuplicatesMedian (int [] A)
{
if (A == null) return 0;
return A.CountDuplicatesMedian();
}

No comments:

Post a Comment