i want casecading in cakephp. I am creating city manager where I have two things zones and states as a dropdown. if i select zone then states should come according to selected zone.please help me what should be the code for model view and controller.I am new in cakephp. table for cities table is given below
CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`total_sample` int(11) NOT NULL,
`population` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `cities`
--
INSERT INTO `cities` (`id`, `zone_id`, `state_id`, `name`, `code`, `total_sample`, `population`, `is_active`, `created`, `modified`) VALUES
(1, 2, 2, 'avadi', '15018', 10, 100, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00');
Please help me with code for controller, view and model.
You can do this by 2 ways
For Option 2: Html
echo $this->Form->input('zone_id', ['options' => $zons, 'empty' => '','id'=>'zoneDropdown']);
echo $this->Form->input('state_id', ['options' => $state, 'empty' => '','id'=>'stateDropdown']);
echo $this->Form->input('city', ['options' => [], 'empty' => '','id'=>'cityDropdown']);
Ajax Script for Html
$('#zoneDropdown, #stateDropdown').on('change', function (evt) {
ZONE = $("#zoneDropdown").val();
STATE = $("#stateDropdown").val();
$.ajax({
type: "GET",
url: baseURL+"cities/getCitiesOptions/"+ZONE+"/"+STATE,
dataType: "json",
success : function(returnData) {
$("#cityDropdown").val(returnData);
}
});
});
and the controller Function need to add
Public function getCitiesOptions($zone_id=null, $state_id=null){
$conditions = [];
if (!empty($zone_id)) {
$conditions['Cities.zone_id'] = $zone_id;
}
if (!empty($state_id)) {
$conditions['Cities.state_id'] = $state_id;
}
$cities = $this->Cities->find('all',['conditions'=>$conditions])->all()->toArray();
// make Options for dorpdown
$html = '';
foreach ($cities as $key => $value) {
$html .= '<option value="'.$value['code'].'">'.$value['name'].'</option>';
}
return $html;
}