Javascript Code In Yii

i wrote the following code in pure PHP and jquery code and i want to reverse it and rewrite it as yii code

i am fresh in yii and need help

in my view:




<script type="text/javascript">	

 

			$(document).ready(function()

			{				

				$("#btnFilter").click(function()

				{

					

					var cycleID	= $("#CycleID").val();

					var classID = $("#stclass").val();

					var sectionID = $("#sectionID").val();

					var majorID = $("#majorID").val();

					

					

					var dataString = "cycleID="+cycleID+"&classID="+classID+"&sectionID="+sectionID+"&majorID="+majorID;

					$.ajax({

						type: "GET",

						url: "<?php echo Yii::app()->request->baseUrl; ?>/protected/views/assignment/dd.php", // Name of the php files

						data: dataString,

						success: function(html)

						{

							$("#getStudents").html(html);

						}

					});	

					


					//alert(cycleID + ' ' + classID + ' ' + sectionID + ' ' + majorID);

					

				});

				

			});

</script>

<div  style="float:left">

        <span>Cycle:</span>	

		<div style="width:350px;" class="combostyle">

		

		<?php

			echo CHtml::dropDownList('CycleID','', $cycleList1,

				array(

					'prompt'=>'Select Cycle',

					  'ajax' => array(

                        'type'=>'POST',

                        'url'=>CController::createUrl('Usersectionsecurity/getClassByCycle'), 

                        'dataType'=>'json',

                        'data'=>array('CycleID'=>'js:this.value'),  

                        'success'=>'function(data) {

                            $("#stclass").html(data.dropdownclasses);

                            $("#periodID").html(data.dropdownperiod);

                        }',

            ))); 

								

		?>

		</div>

	</div>

	

	<div style="float:left">

        <span>Class:</span>

		<div style="width:150px;" class="combostyle">

		<?php 

			echo CHtml::dropDownList('stclass', '', CHtml::listData(Stclass::model()->findAll(), 'Class_Code', 'E_Class_Desc'), 

				array(

					'prompt'=>'Select Class',

						'ajax' => array(

							'type'=>'POST', //request type

							'url'=>CController::createUrl('Assignment/getStudentByClass'), //url to call.

							'update'=>'#'.CHtml::activeId($model,'studentID'), 

						)));

		?>

		</div>

	</div>	

	<div style="float:left">

		<span>Major:</span>

		<div style="width:150px; " class="combostyle">

			<?php echo CHtml::dropDownList('sectionID', '', 

              array('01' => 'A', '02' => 'B'));?>

		</div>

	</div>

	

	<div>

		<span>Section:</span>

		<div style="width:150px; " class="combostyle">

			<?php echo CHtml::dropDownList('majorID', '', 

              array('3333-13' => 'English', '3333-12' => 'Frensh'));?>

		</div>

	</div>

	

	<input type="button" id="btnFilter" value="Filter the list">

	<div id="getStudents"></div>



i created a file named dd.php and it contains:




<?php

$cycleID=$_GET['cycleID'];

$classID=$_GET['classID'];

$sectionID=$_GET['sectionID'];

$majorID=$_GET['majorID'];


function connect() {


	mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

	mysql_select_db("basdb");

}

connect();

$sql = "SET NAMES utf8";

$result = mysql_query($sql);

$sql1="SELECT * FROM `student` WHERE `Group_Code`='$cycleID' and `Class_Code`='$classID'";

//$sql1=mysql_query("SELECT * FROM `student` WHERE `Group_Code`='$majorID' and `Class_Code`='$majorID' and `Section_Code`='$majorID' and `Major_Code`='$majorID'");

echo $sql1;

//$result1 = Yii::app()->db->createCommand($sql1)->queryRow();

$result1 = mysql_query($sql1);


$str="";

$str1="";


echo "<select name='studentID'>";

echo "<option value='Model'>Model</option>"; 


while ($row = mysql_fetch_array($result1)) {

	$fullName = $row['E_Child_Name'] . " " . $row['E_Father_Name'] . " " . $row['E_Family_Name'];

  echo "<option value='" . $row['Student_Number'] . "'>" . $fullName . "</option>";

  //$str=$str . "\"$row[0]\"".",";

//$str1=$str1 . "\"$row[1]\""."," ;

 }

echo "</select>";

	




?>



For jquery code do it something like this in yii




<?php

Yii::app()->clientScript->registerScript('script',"jquery code");

?>




In your php code you mostly handling sql queries so for yii have a look at this

http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder and

http://www.yiiframework.com/doc/guide/1.1/en/database.dao

PHP code:

  1. sql is set in the main.php ( under protected/config/ folder ) - you dont need to handler it yourself in yii

  2. you can query the way you wrote in your question , but i suggest using a model.

for that create under models a php class called "student" , extending ActiveForm.

  1. render the page from the controller when sending it the model ( you just got from the DB )

    

   class SiteController extends Controller


      $model = Student::model->findByPk($id);


       public function actionIndex(){

           $this->render('view_name', array('student' => $model));

       }

    }