I would like to pass two variables($crop_id and $date_id)as I click in ajax into my crtinfo.php(This will be the changing <div>). At first, $crop_id = 0 and $date_id=0 Then, as I click a crop link, it will only change $crop_id leaving date_id =0. When I click date button, it will only change $date_id, but the crop_id must remain the previous.
I have this codes:
siteController
public function actionIndex()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$data = array();
$crops = Crop::model()->findAll();
$view_crop = array();
$fcropid = array();
$y=0;
foreach($crops as $c)
{
$view_crop[$y] = strtolower($c->crop_name);
$y++;
}
sort($view_crop);
$crop_count=count($view_crop);
for($i=0, $n=1; $i<$crop_count; $i++, $n++)
{
$view_crop[$i] = ucfirst($view_crop[$i]);
$cropid = Crop::model()->find('crop_name = :cropId', array(
':cropId' => $view_crop[$i]
));
$fcropid[$i] = $cropid->crop_id;
}
//$current = $fcropid[0];
$data["crop_id"] = $fcropid[0];
$current_crop = 0;
$current_date = 0;
$data["date_id"] = 0;
$this->render('index', $data);
}
public function actionCrtinfo($crop_id, $date_id)
{
$current_crop =$crop_id;
$current_date = $date_id;
//$current = $myValue;
$data = array();
$data["crop_id"] = $current_crop;
$data["date_id"] = $current_date;
// renders the view file crtInfo
// using the default layout 'protected/views/layouts/main.php'
$this->renderPartial('crtinfo', $data, false, true);
}
This is my index.php:
//updates crop_id
for($i=0, $n=1; $i<$crop_count; $i++, $n++)
{
$view_crop[$i] = ucfirst($view_crop[$i]);
$cropid = Crop::model()->find('crop_name = :cropId', array(
':cropId' => $view_crop[$i]
));
$fcropid[$i] = $cropid->crop_id;
echo "     ".CHtml::ajaxLink ("$view_crop[$i]",
array('site/crtinfo' , 'crop_id'=>$fcropid[$i], 'date_id'=> null),
array('update' => '#data'))."<br/>";
}
//update the date
$label = ["1m", "3m", "6m", "YTD"];
$dat_mod = [0, 1, 2, 3];
for($at=0;$at<count($label);$at++)
{
//echo $sdate[$at]." ".$edate[$at];
echo " ".CHtml::ajaxButton ("$label[$at]",
array('site/crtinfo', 'crop_id'=>null, 'date_id'=>$dat_mod[$at]),
array('update' => '#data'));
echo $at;
}
<div ="changeing data">
$this->renderPartial('crtinfo', array('crop_id'=>$crop_id, 'date_id'=>$date_id));
</div>
Then the crtinfo.php
<?php
echo "Crop_id: $crop_id ";
echo "Date_id: $date_id ";
if ($crop_id != null)
$current_crop = $crop_id;
if ($date_id != null)
$current_date = $date_id;
echo "Cur_crop: $current_crop ";
echo "Cur_date:$current_date ";
?>
In my codes, if I click crop, crop_Id is equal to the clicked while date_id will be equaled to null and vice versa. Is there anyway to pass two parameers here or is there something wrong with my code?