0
我有這個代碼可以在Yii CGridView中創建和填充表格。CButtonColumn批准按鈕執行PHP代碼而不被點擊
我的問題是位於'approve'=>array('options'=>array('onclick'))
中的功能,即使沒有單擊批准按鈕,也會爲每次刷新頁面調用該功能。
我確定通過打印計數器的值發生了錯誤。計數器只應在每次刷新頁面時單擊批准時才增加1。
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'registrants-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'user_id',
'first_name',
'middle_name',
'last_name',
'gender',
'shirt_size',
'receipt_number',
'category',
array(
'class'=>'CButtonColumn',
'template'=>'{approve}, {update},{delete}',
'buttons'=>array(
'approve' => array(
'label'=>'Approve',
'options'=>array(
'onclick'=> $model->approveRegistrants($model->user_id, $model->category),
//ending approve-option array
),
//ending approve-button array
),
//ending buttons array
)
//ending table-last-column array
),
//ending table-columns array
),
//ending zii.widgets.grid.CGridview
));
?>
這是我在我的模型中的功能。
public function approveRegistrants($user_id, $category){
$db = new PDO('mysql:host=localhost; dbname=secret; charset=utf8', 'Andy', '*****');
$getCounter = "SELECT registrants FROM counter order by registrants desc limit 1;";
$bool = false;
$show = '0';
do{
$result = $db->query($getCounter);
// $registrants = $db->query($getCounter);
// $result->setFetchMode(PDO::FETCH_ASSOC);
// $registrants = '1';
foreach ($result as $value){
$registrants = $value['registrants'];
echo 'hello'.$registrants.'</br>';
}
// $registrants = $result['registrants'];
// print_r($registrants);
$max_registrants = '3400';
if($max_registrants > $registrants){
// pdo that will use $updateCounterByOne
$updateCounterByOne = "UPDATE counter set registrants = registrants + 1 WHERE registrants = ". $registrants .";";
$updateCounter = $db->prepare($updateCounterByOne);
$updateCounter->execute();
// return affected rows
$returnAffectedRows = $updateCounter->rowCount();
$bool = true;
// break;
}
else{
echo "No more slot Available";
// break;
}
}while($returnAffectedRows == '0');
if($bool = true){
//sql syntax
$selectApprovedUser = "SELECT user_id FROM registrants WHERE user_id = '". $user_id ."';";
//pdo that will use $selectApprovedUser
$updateApprovedUser = "UPDATE registrants set approved = 'YES' where user_id = ". $selectApprovedUser .";";
$updateApproved = $db->prepare($updateApprovedUser);
$updateApproved->execute();
//pdo that will use $insertApprovedUser
$insertApprovedUser = "INSERT INTO approved_registrants (user_id, category, approved_date) VALUES ('".$user_id."', '".$category."', 'curdate()');";
$insertApproved = $db->prepare($insertApprovedUser);
$insertApproved->execute();
//execute trial
$selectSomething = "SELECT registrants from counter where tandem = '0'";
$doSelect = $db->prepare($selectSomething);
$doSelect->execute();
$hello = $doSelect->fetchAll();
echo $hello[0]['registrants'];
}
}
我試圖做到的,是當確認按鈕被點擊它將得到user_id
,並會做PDO作爲更新命令,比如和插入。