Hi everybody!
First of all, I don’t know if this is the right forum to leave this comment, but I haven’t found any better one. Please tell me if I’m wrong.
I’ve been working for some years with .net, asp… and I’ve done just a few things in native php. I’m a complete newbie in the world of php-frameworks and MVC, so I have questions that may be silly for you
I’m working in a search engine that works this way:
In my main page, I have 2 dropDownLists in a CActiveForm:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'buscador-form',
'action'=>'actividades/buscar',
'method'=>'post',
'enableAjaxValidation'=>true,
'enableClientValidation'=>false));
?>
<div id="buscador">
<label for="Actividades_Actividad" class="required">Selecciona tu actividad</label>
<?php echo $form->dropDownList(Actividades::model(), 'IdActividad', CHtml::listData(Actividades::model()->findAll(), 'IdActividad', 'Actividad'), array('options'=>array('42'=>array('selected'=>true)),'name'=>'actividad')); ?>
<br />
<br />
<label for="Municipios_Municipio" class="required">Selecciona tu Municipio</label>
<?php echo $form->dropDownList(Municipios::model(), 'IdMunicipio', CHtml::listData(Municipios::model()->findAll(" IdIsla = 1 "), 'IdMunicipio', 'Municipio'), array('options'=>array('24'=>array('selected'=>true)),'name'=>'municipio')); ?>
<br />
<br />
<?php
$this->widget('ext.EChosen.EChosen', array('target' => 'select', 'useJQuery' => true, 'debug' => true,));
$this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit','type'=>'info','label'=>'Encontrar','loadingText'=>'Buscando...','htmlOptions'=>array('id'=>'buttonStateful','style'=>'margin-left:132px')));?>
</div>
<?php
$this->endWidget();
?>
Using CActiveForm’s action property, I send the search params to my action “buscar” in my “actividades” controller:
public function actionBuscar()
{
if(isset($_POST['municipio']) and isset($_POST['actividad'])) {
$dataProvider=new CActiveDataProvider('Vwactividades', array(
'criteria'=>array(
'condition'=>'IdMunicipio = '.$_POST["municipio"].' AND IdActividad = '.$_POST["actividad"]
)
));
}
$this->render('buscar',array(
'dataProvider'=>$dataProvider,
));
}
I get the POST values, perform a search on a db view, and render the view "buscar":
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'busqueda-grid',
'dataProvider'=>$dataProvider,
'filter'=>null,
'columns'=>array(
array(
'name'=>'Nombre',
'header'=>'Establecimiento',
'type'=>'raw',
'value'=>'"<nobr>".$data->Nombre."</nobr>"'
),
array(
'name'=>'Dias',
'header'=>'Días',
'type'=>'raw',
'value'=>array(Dias::model(), 'getDias')
),
'Mensualidad',
array(
'name'=>'Valoracion',
'header'=>'Valoración',
'type'=>'raw',
'value'=>array(Clientes::model(), 'getValoracion')
),
),
));
?>
The problem is when I try to order the grid by clicking the headers. I receive this alert:
Error 500: <h1>PHP Error [8]</h1> <p>Undefined variable: dataProvider</p>
I understand the problem is when pagination recalls "buscar" action:
.../actividades/buscar?Vwactividades_sort=Nombre
and of course… as $_POST is empty, can’t re-create $dataProvider.
I’ve fixed it temporarily making the search params being sent by GET, but I don’t like it. I would prefer the URL to keep as:
/actividades/buscar
…instead of…
/actividades/buscar?municipio=24&actividad=42
Can somebody help me? Maybe I’m doing it wrong from the beginning
Thanks in advance!