How To Display Database Values Into The View Page

Hi, i need to display my db values in the view page. And My Database is…




CREATE TABLE IF NOT EXISTS `users_stats_info` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `loginId` int(11) NOT NULL,

  `height` float DEFAULT NULL,

  `weight` float DEFAULT NULL,

  `chest` float DEFAULT NULL,

  `waist` float DEFAULT NULL,

  `hips` float DEFAULT NULL,

  `biceps` float DEFAULT NULL,

  `bmi` varchar(50) DEFAULT NULL,

  `createrId` int(11) DEFAULT NULL,

  `CreatedDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,

  `updaterId` int(11) DEFAULT NULL,

  `updatedDate` datetime DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `userid` (`loginId`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;



ANd my view page is like this…




<html>

	<head>

    	<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

     	<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<!-------------------------------------------------Script for onchange------------------------------------------------------------------>

     	<script type="text/javascript">

        	function myfunction(v){

             	alert(v);


                	$.ajax({

                        	url: '<?php echo Yii::app()->createAbsoluteUrl("usersStatsInfo/graph"); ?>',

                        	type: 'POST',

                    	//   async: false,

                    	data: 'ans='+v ,

                    	success: function(res)

                    	{


                        	alert(res);

                        	alert("inside success");


                    	}

                    	});

           	}

    	</script>

</head>

<body>

<table border="0">

	<tr>

		<td align="left" valign="middle" width="15%" class="fields">

                    	<select name="bodyStats" title="Select Stats" onchange="myfunction(this.value)">

                        	<option value="56">Weight</option>

                        	<option value="60">height</option>

                        	<option value="bmi">BMI</option>

                        	<option value="chest">Chest</option>

                        	<option value="waist">Waist</option>

                        	<option value="hips">Hips</option>

                        	<option value="biceps">Biceps</option>

                    	</select>

                    	</td>

		</tr></table>

</body>

</html>



And My controller is like this…




public function actionMyGraph() {

      	$model = new UsersStatsInfo();

       	if(Yii::app()->request->isAjaxRequest) {

            	if(isset ($_POST['ans'])) {

                	$result = $_POST['ans'];

                	echo $result;

                	//$this->render('myGraph', array('model'=>$model, false, true));

            	}

        	} 

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

    	}




find the results in the view page

or

find the results in the controller action and pass the variable to view page

then display it!! what is the confusion?

I tried like that but i’m not getting …

if your qn is ajax related, the make the changing part of the view file to a separate file and renderPartial the page, in the action

where ajax calling?

is it a dropdown?

yes sir… i’m calling ajax for dropdown

i dont know the structure of the ur view

i think

make this part as a single file,

then renderPartial it in the action, with the find values!!


        <option value="56">Weight</option>

                                <option value="60">height</option>

                                <option value="bmi">BMI</option>

                                <option value="chest">Chest</option>

                                <option value="waist">Waist</option>

                                <option value="hips">Hips</option>

                                <option value="biceps">Biceps</option>

that is the basic idea.




echo CHtml::dropDownList('ur-name','', array(1=>'USA',2=>'France',3=>'Japan'),

array(

'ajax' => array(

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

'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.


'update'=>'#div-id', //selector to update


))); 




<div id="div-id"></div>

in controller

find the values with post value if dropdown according to your use

then renderPartial the page, pass the result to the view file

Hi sir, based on select option i need to display the values. And i tried your way, i’m getting error like this…

"Object of class UsersStatsInfo could not be converted to string"

where?

your dropdown code pls

NOw i’ getting Undefined index: height




echo CHtml::dropDownList('UsersStatsInfo','', array(1=>'height',2=>'weight',3=>'chest'),

        	array(

            	'ajax' => array(

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

            	'url'=>CController::createUrl('usersStatsInfo/myGraph'), //url to call.


            	'update'=>'#td-id', //selector to update


        	)));

     	

      	$height = $_POST['height'];

      	$weight = $_POST['weight'];

      	$chest = $_POST['chest'];

      	

      	echo $height;

      	echo $weight;

      	echo $chest;



you will get the post value as $_POST[‘UsersStatsInfo’]

controller action code ?

what is your purpose? what you want to display in the view page?

if your required dropdown need these options




                                <option value="56">Weight</option>

                                <option value="60">height</option>

                                <option value="bmi">BMI</option>

                                <option value="chest">Chest</option>

                                <option value="waist">Waist</option>

                                <option value="hips">Hips</option>

                                <option value="biceps">Biceps</option>



then the post values are 56 or 60 or bmi or chest or…or… etc

what you want to find with this dropdown?

Based on select option I need to display database value

so find it using findByAttributes() or something like that

what value? what is your expected output? what you want to show?

If i select "height" in the dropdown the height value 50or something whatever in db that value i need to show in my view page. if i select weight, the weight value i need to show in my view page

ok all weight values?/all height value?

so you have to find it in the action…

pls test with something echo in the action.