Search form

This is my code…




public function actionSearch()

	

	{

		$model=new anagrafe;

		

		if(isset($_POST['anagrafe']))	

		{

				$model->attributes=$_POST['anagrafe'];

				$cognome = strtoupper( $_POST['cognome']);

				$nome= strtoupper($_POST['nome']);

				$connection=new CDbConnection('oci:dbname=//oracledb:1521/oraprod','ora02','oracle');

				$connection->active=true;

				$command=$connection->createCommand("SELECT ANAGRA_ID FROM ANAGRAFE WHERE NOME='$nome' AND COGNOME='$cognome'");

				$command->execute();   

				$reader=$command->query();

				 foreach($reader as $row) 

					{

						$ID= $row['ANAGRA_ID'];

					}

				

				$this->redirect(array('view','id'=>$model->ANAGRA_ID=$ID));

		}


			$this->render('search',array(

			'model'=>$model,));

	

		}



I would add an if cycle to filter the data as if I have two people with the same name … I see them and make the user choose which to display…

Can you help me???

Take a look at the documentation about CActiveRecord, you can use the more effectively and avoid to write query.

You can do like that:




public function actionSearch()


{

	$model=new anagrafe;

	

	if(isset($_POST['anagrafe']))   

	{

		$model->attributes=$_POST['anagrafe'];

		$dataProvider=$model->search();

		

		if ($dataProvider->totalItemCount==1)

			$this->redirect(array('view','id'=>$dataProvider->data[0]->id));

	}


	$this->render(

		'search',

		array(

				'model'=>$model,

				'dataProvider'=>$dataProvider,

			)

		);


}



In the view ‘search’ you can add the code for display the found users.

This code is really very similar to the code of the actionAdmin created by crud.

If you generated the AR of anagra with gii (or Yii shell) you have the method search that create the DataProvider.

Read the documentation about CActiveDataProvider and about CActiveRecord for understand how AR works.

Where do you have take this code???

I have generated with shell model class and controller but there isn’t this code?

I can use this code for my problem?

I created a search form to filter the data in a table of members before exporting the data. I currently facing a strange problem with the advanced search form. He loses his values ​​and I does not understand why?

#view form fields :

4863

form.PNG

#Controler :

    /**


     * Méthode pour exporter les données de la table membres 


     * dans un fichier excel


     * 


     */


    public function actionExport(){


	$model=new Tblmembres('search');


	$model->unsetAttributes();  // clear any default values


	if(isset($_REQUEST['Tblmembres'])){


		$model->attributes=$_REQUEST['Tblmembres']; 


                    echo "<pre>";


                    print_r($_REQUEST['Tblmembres']);


                    echo "</pre>";


                    die();


            }


            // Afficher le formulaire pour filtrer


            $this->render('export',array(


            'model'=>$model,


	));

#Controler output after submiting the form… some fields are empty…

Array

(

[idfaculte] => 1


[idservice] => 1


[idpavillon] => 48


[idstatutemploye] => 1


[idcategoriedonnateur] => 


[lstassignation] => 


[nom] => 


[prenom] => 


[nomusager] => Nomdusager


[type_usager] => 


[no_employe] => 


[local] => 


[syndicat] => syndicat


[email] => 


[telephone] => 4196666666


[connexion] => Desactivee


[date_embauche] => 


[date_inscription] => 2013/11/11


[idmembres] => 

)

If you have an idea or suggestion why values ​​disappear.

Thank you!