Yii Framework Forum: [SOLVED]drop down list - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

[SOLVED]drop down list help Rate Topic: -----

#1 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 02:10 AM

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 ?
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 11 November 2010 - 02:30 AM

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'.
0

#3 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 02:48 AM

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,

Posted Image

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 ?
0

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,461
  • Joined: 04-October 10

Posted 11 November 2010 - 05:00 AM

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.
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 11 November 2010 - 05:17 AM

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.
0

#6 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 05:25 AM

View PostAntonio Ramirez, on 11 November 2010 - 05:00 AM, said:

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.


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',
		);
	}


View Postzaccaria, on 11 November 2010 - 05:17 AM, said:

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.


I did your suggestion but it is still white
0

#7 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,461
  • Joined: 04-October 10

Posted 11 November 2010 - 06:30 AM

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.
0

#8 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 08:51 AM

View PostAntonio Ramirez, on 11 November 2010 - 06:30 AM, said:

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 )
0

#9 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 11 November 2010 - 09:05 AM

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.
0

#10 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 09:31 AM

View Postzaccaria, on 11 November 2010 - 09:05 AM, said:

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?
0

#11 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 11 November 2010 - 10:01 AM

Exactly.

Does work this?

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


It retrives and array of cities?

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

#12 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,461
  • Joined: 04-October 10

Posted 11 November 2010 - 10:13 AM

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.
0

#13 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 10:14 AM

View Postzaccaria, on 11 November 2010 - 10:01 AM, said:

Exactly.

Does work this?

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


It retrives and array of cities?

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


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 ?



View PostAntonio Ramirez, on 11 November 2010 - 10:13 AM, said:

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.


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,
		));
	}
}

0

#14 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,461
  • Joined: 04-October 10

Posted 11 November 2010 - 10:50 AM

View Postsasori, on 11 November 2010 - 10:14 AM, said:

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 ?



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.
0

#15 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 11 November 2010 - 10:59 AM

View PostAntonio Ramirez, on 11 November 2010 - 10:50 AM, said:

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.

Quote

Have you double checked its table name?


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 )
0

#16 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,461
  • Joined: 04-October 10

Posted 12 November 2010 - 01:05 AM

View Postsasori, on 11 November 2010 - 10:59 AM, said:

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.
0

#17 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 12 November 2010 - 01:14 AM

View PostAntonio Ramirez, on 12 November 2010 - 01:05 AM, said:

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
Posted Image

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
0

#18 User is offline   Marco W. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-October 10
  • Location:Hamburg, Germany

Posted 12 November 2010 - 01:52 AM

View Postsasori, on 12 November 2010 - 01:14 AM, said:

I removed all the code within the _form view and placed this alone
var_dump(GgTWorldareascities::model()->findAll());



You have to put it in php tags:
<?php var_dump(GgTWorldareascities::model()->findAll()); ?>

0

#19 User is offline   sasori 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 310
  • Joined: 05-November 10

Posted 12 November 2010 - 01:56 AM

View PostMarco W., on 12 November 2010 - 01:52 AM, said:

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
0

#20 User is offline   Marco W. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-October 10
  • Location:Hamburg, Germany

Posted 12 November 2010 - 02:06 AM

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

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users