[SOLVED]drop down list

My problem goes like this,

  • I have a table e.g named "members" wherein, it has a column for cities, countries and states,

  • these 3 columns should be populated with data coming from 3 drop down menus,

  1. but the the list of cities, countries and states has their own tables, now i need to pull datas

from 3 different tables in order to populate the dropdown menus in my member’s _form view

what I have done so far is, generate the model for cities, countries and states

any tips, ideas, suggestions how to approach this thing ?

You have to generate the model for cities.

Than your view you can use:




$form->dropDownList($model, 'city', CHtml::listData(City::model()->findAll(), 'primaryKey', 'name'));



Where name is a field in city.

Chtml::ListData create an array using as key the field ‘primaryKey’ and as value ‘name’.

I tried your suggestion,




	<div class="row">

		<?php echo $form->labelEx($model,'City'); ?>

		<?php echo $form->dropDownList($model,'City',CHtml::listData(cities::model()->findAll(),'CityID','CityName')); ?>

		<?php echo $form->error($model,'City'); ?>

	</div>



and here’s what i got, the default layout and colors dissappeared,

by the way, my cities table and the other 2 doesn’t have a primary key on their tables, they are stand alone…does that has something to do with why my app turned to white just like that after I added the listData ?

One question:

Does your $model has an attribute ‘City’? that is in fact the ID of a city? - that is taken to select the option on the selection box.

$form->dropDownList($model,‘City’,CHtml::listData(cities::model()->findAll(),‘CityID’,‘CityName’));

One more thing, is cities or Cities a model? Why is called cities? wouldn’t be city? or City? (I recommend you call the model class as their names states as in Windows environment won’t be a problem but in Linux if your class is saved as City.php Yii won’t find it)

How is your table structured (city or cities), just to provide you with a better answer.

First of all, you should write in the index.php this lines:




error_reporting(E_ALL);

ini_set('display_errors',1);



This will show you the error you got.

Your app “turned to white” because you have some error, some class that doesn’t exist or something like that, turning on error reporting will speed up your debug.

the model does have a ‘City’ Attribute (if you’re referring to the the ‘City’ beside the, dropDownList($model,

the cities is not the actual model name, I just typed it that way on the forum to mask the actual model name.

if you’re referring to that ‘City’ beside the $model, should I replace it with something else ?

this is how the part of my model is structured




	public function attributeLabels()

	{

		return array(

			'MemberShipID' => 'Member Ship',

			'CompanyID' => 'Company',

			'CompanyName' => 'Company Name',

			'ContactName' => 'Contact Name',

			'AddressLine1' => 'Address Line1',

			'AddressLine2' => 'Address Line2',

			'City' => 'City',

			'Region' => 'Region',

			'Country' => 'Country',

			'ZipCode' => 'Zip Code',

			'TelephoneNumber' => 'Telephone Number',

			'MobileNumber' => 'Mobile Number',

			'FaxNumber' => 'Fax Number',

			'WebSiteAddress' => 'Web Site Address',

			'CompanyNumber' => 'Company Number',

		);

	}



I did your suggestion but it is still white

CityID exists in your cities model?

Please check its attributes as the query you do is the most simplest of them all ‘findAll()’, if your attributes are correct (cityID, cityName) I truly don’t know where is the problem.

Instead of

error_reporting(E_ALL);

ini_set(‘display_errors’,1);

Change the error reporting in your php.ini file to E_ALL and restart your web server. I found that in some environments I couldn’t do that from the code.

I got a question with this line




<?php echo $form->dropDownList($model,'City',CHtml::listData(Cities::model()->findAll(),'CityID','CityName')); ?>



that ‘City’ inbetween the $model and CHtml, should I replace it with the ‘CityID’ attribute of the Cities model ?

or just leave it like that?, because I’m pulling the cities data from the Cities model and it is a separate model from the script view who’s trying to use this CHtml helper … (i don’t really know how this thing works despite of reading the dropDownList/listData manual )

That city should be a property of $model.

I mean, that this:




<?php echo $form->textField($model,'City'); ?>



Should work, display a textbox and save the city. This City should be a property of company

If the textbox work, you can check if this produce a correct array:


<?php print_r(CHtml::listData(Cities::model()->findAll(),'CityID','CityName'));?>

Here, cityId and citiName shoud be property of cities.

If this too works, then even the dropdownlist should display.

Anyway, if you continue to get simply white page, it means that your error loggin is not set on, and this will slow you down a lot. Invest some time to make it work, it will be very helpful.

I actually checked my php.ini file, all error related stuff is ‘on’ ever since, my YII debug is on, including

the log thing from the main.php file…

this one works, as per the Company model + the it’s City Attribute, it showed a blank text field as expected




<?php echo $form->textField($model,'City'); ?>



this one doesn’t., it showed a white page only




<?php print_r(CHtml::listData(Cities::model()->findAll(),'CityID','CityName'));?>

[code]


I checked my php_error log, this is what I found out


[code]

CActiveRecord->findAll() C:\xampp\htdocs\yii\myapp\protected\views\wsmembersdetails\_form.php:45



wherein, line 45 is this one




<?php print_r(CHtml::listData(Cities::model()->findAll(),'CityID','CityName'));?>



does that mean, it’s not being able to pull data from that Cities model, that’s why it’s acting weird?

Exactly.

Does work this?


Cities::model()->findAll();

It retrives and array of cities?

If yes, are CityID and CityName columns of the table cities?

Do you actually have a model Cities? If so, could you check its properties?

Can you, in order for us to help you… post its structure as you did with the $model? Don’t worry about masking its name buddy… Here you find only people who wishes to help.

it doesn’t retrieve anything, it still white page.

by the way, my Cities Table has Cities model only, it doesn’t have a CRUD nor Controller generated from gii,

do you think the crud and controller does matter ? or the model alone should be enough to pull datas from that table ?

this is the contents of the cities model ( i masked the real model name )




<?php


/**

 * This is the model class for table "Cities".

 *

 * The followings are the available columns in table 'cities':

 * @property integer $CountryID

 * @property integer $RegionID

 * @property integer $StateID

 * @property integer $CityID

 * @property string $CityCode

 * @property string $CityName

 *

 * The followings are the available model relations:

 */

class Cities extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Cities the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'cities';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('CountryID, RegionID, StateID, CityID', 'numerical', 'integerOnly'=>true),

			array('CityCode', 'length', 'max'=>10),

			array('CityName', 'length', 'max'=>50),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('CountryID, RegionID, StateID, CityID, CityCode, CityName', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'CountryID' => 'Country',

			'RegionID' => 'Region',

			'StateID' => 'State',

			'CityID' => 'City',

			'CityCode' => 'City Code',

			'CityName' => 'City Name',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('CountryID',$this->CountryID);

		$criteria->compare('RegionID',$this->RegionID);

		$criteria->compare('StateID',$this->StateID);

		$criteria->compare('CityID',$this->CityID);

		$criteria->compare('CityCode',$this->CityCode,true);

		$criteria->compare('CityName',$this->CityName,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

}



mmm… this is very weird. Have you double checked its table name? I just cannot believe this simple request doesnt bring any records at all.

You dont need a controller for that but… lets play a bit:

a ) put this on your rules:

array(‘CityID, CityName’, ‘safe’),

– sometimes I had this problem too and was because of the rules

b ) on your view (right at the top) do:

var_dump(Cities::model()->findAll());

die();

And tell me what it does.

I did your suggestion., the page color went back to normal, but the ‘City’ field is blank (ofcourse)

but the var_dump(Cities::model()->findAll());

is blank too.

ok, the real table name is gg_t_worldareascities and the real model name generated from gii is GgTWorldareascities ( do you think it causes the problem ?, i mean, the real table name got underscores and the generated model from gii doesn’t have )

The table name is not causing the problem, with you have done we have isolated the problem and we know now that there is no problems with your dropdown list but with your findAll()… Now, lets see its SQL.

In order to view the SQL, we are going to activate CWebLogRoute in your config/main.php file:




<?php


    array(


        'components'=>array(


            // ... 


            'log' => array(


                    array(


                        'class'=>'CWebLogRoute',


                    ),


                ),


            ),


            // ...


        )




Then, call again the view above. Remember, we just want to see one line Cities::model()->findAll();, so reduce the amount of code to that line. Execute and check its results.

Importante note: CWebLogRoute is for development process, on production remove it.

I removed all the code within the _form view and placed this alone




var_dump(GgTWorldareascities::model()->findAll());



here’s what i got

no errors or pink alerts in the weblogs of yii

it should have worked right ?

I also tried generating another model version wherein, I typed the gg_t as prefix inside the gii prefix box of the model generator…the model generated was Worldareascities…but still same thing happened

You have to put it in php tags:




<?php var_dump(GgTWorldareascities::model()->findAll()); ?>



thanks for reminding about the php tags, but still no data is being pulled out,

i wonder what’s wrong with it, it’s just a matter of pulling out an array

from that table via the model generated, and this simple var_dump() doesn’t show

anything aside from the white page

Could be there is an error in your controller or view. Can you post them?

Marco