Yii Framework Forum: How to fill data in Dropdownlist from Database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to fill data in Dropdownlist from Database Rate Topic: -----

#1 User is offline   Rahul Kumar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 29-January 11

Posted 08 April 2011 - 10:39 AM

Hello Folks,

I am not able to fill data from table (tbl_country).

I am creating a user registration form and I have country dropdownlist. I don't know how to fetch data from database and fill dropdownlist.

I used this link for help (http://yiitutorials....n-list-with-yii) but nothing happened. :(

Please help.
0

#2 User is offline   nickcv 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 154
  • Joined: 23-November 10

Posted 08 April 2011 - 12:35 PM

the guide you linked it's pretty much self explanatory.

what kind of error do you get?
sometimes it's just like teaching pigs how to fly

Posted Image
Posted Image looking for some invite keys? welcome on B4K!
0

#3 User is offline   Antonio Ramirez 

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

Posted 08 April 2011 - 02:05 PM

The guide is pretty good and if you followed should work. I have seen that you have a table prefix, have you set that up? Is your country pointing to '{{country}}' and table prefix on your config file 'main.php' set to 'tbl_'?

To fill out the dropdown is as easy as calling CHtml::listData(Country::model()->findAll()) on the data parameter of the dropdownlist function... make sure your object returns something...
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
1

#4 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 16 November 2012 - 08:02 AM

View PostAntonio Ramirez, on 08 April 2011 - 02:05 PM, said:

The guide is pretty good and if you followed should work. I have seen that you have a table prefix, have you set that up? Is your country pointing to '{{country}}' and table prefix on your config file 'main.php' set to 'tbl_'?

To fill out the dropdown is as easy as calling CHtml::listData(Country::model()->findAll()) on the data parameter of the dropdownlist function... make sure your object returns something...

I am newbie . i want to register client information, it use drop down list for state from database table states.
My problem is that when i clik link to create client...a new dialog form open and displays nothing???????
when i remove the dropdown code it works fine..but i want to use dropdown in field of state of client form.

clientcontroller.php (actioncreate() snippet in controller file of client.php)
public function actionCreate()
{
$model=new client;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['client']))
{
$model->attributes=$_POST['client'];
if($model->save())
{
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Client successfully added"
));
exit;
}
else
$this->redirect(array('view','id'=>$model->id));
}
}

if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form', array('model'=>$model), true)));
exit;
}
else
$this->render('create',array('model'=>$model,));
}



In client/admin.php (a link is to create client,only snippet is here ).

<?php echo CHtml::link('Add New State', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; font-size:20px; text-decoration: underline;',
'onclick'=>"{addstate(); $('#dialogstate').dialog('open');}"));
?>
</div><!-- add-form -->

<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogstate',
'options'=>array(
'title'=>'Create Number Assignment',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>470,
),
));
?>

<div class="divForForm"></div>

<?php $this->endWidget();?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'assignment-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'state',
'code',
'status',

array(
'class'=>'CButtonColumn',
),
),
));
?>



<script type="text/javascript">
// here is the magic
function addstate()
{
<?php echo CHtml::ajax(array(
'url'=>array('states/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogstate div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogstate div.divForForm form').submit(addstate);
}
else
{
$('#dialogstate div.divForForm').html(data.div);
setTimeout(\"$('#dialogstate').dialog('close') \",3000);
}

} ",
))
?>;
return false;

}

</script>

plz help in dropdown list to generate?
0

#5 User is offline   parkerfrank 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 02-October 12

Posted 19 November 2012 - 12:45 AM

you can do it like this

DataTable dtMaker = objMakers.GetAllMakers();
ddlMakerID.DataSource = dtMaker;
ddlMakerID.DataTextField = "MakerName";
ddlMakerID.DataValueField = "MakerId";
ddlMakerID.DataBind();
0

#6 User is offline   skapeyi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 22-November 13
  • Location:Uganda

Posted 22 November 2013 - 09:51 AM

View Postnickcv, on 08 April 2011 - 12:35 PM, said:

the guide you linked it's pretty much self explanatory.

what kind of error do you get?

Hi, i followed the guide. the dropdown list was generated like twice when i was using the form then i started throwing and array to string conversion error.

Any help will be greatly appreciated

Attached File(s)

  • Attached File  yii!.PNG (36.52K)
    Number of downloads: 23
  • Attached File  yii1.PNG (59.34K)
    Number of downloads: 23

0

#7 User is offline   Emilio D. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-September 13

Posted 27 November 2013 - 12:57 AM

This is what I did in my web application

<?php echo $form->dropDownList($model,'country', CHtml::listData(Country::model()->findAll(), 'id', 'country')); ?>
0

#8 User is offline   Abhishek Shah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 20-July 12
  • Location:New Jersey,USA

Posted 27 November 2013 - 04:23 AM

View Postskapeyi, on 22 November 2013 - 09:51 AM, said:

Hi, i followed the guide. the dropdown list was generated like twice when i was using the form then i started throwing and array to string conversion error.

Any help will be greatly appreciated

In dropdown first two parameters are from field of table which you want to save,
i.e.$model ,farmer
$model as your object referring your table in which you want to save data & farmer->field in which you want to save id of farmer table.
<?php echo $form->dropDownList($model,'farmer', CHtml::listData($models, 'farmer_id', 'name')); ?>
Feel free to ask for help,
Abhishek.
Freelancer.
Yii Basic Gii With Export Pdf Excel
0

#9 User is offline   skapeyi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 22-November 13
  • Location:Uganda

Posted 27 November 2013 - 05:07 AM

View PostAbhishek Shah, on 27 November 2013 - 04:23 AM, said:

In dropdown first two parameters are from field of table which you want to save,
i.e.$model ,farmer
$model as your object referring your table in which you want to save data & farmer->field in which you want to save id of farmer table.
<?php echo $form->dropDownList($model,'farmer', CHtml::listData($models, 'farmer_id', 'name')); ?>

Thank you very much..solved a problem i have tried to fix for 3days straight.
Thanks again..
0

#10 User is offline   mandela 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-July 14

Posted 22 July 2014 - 11:47 AM

Hello every, please i just started using yii and am having some difficulties doing a dropDownList from DB
i have two tables party and election_party with a relationship between the tables. i want it that when i select a party from the party table and add to the election party table it, the ID of the party from the party table will be saved in the the election_party table. will appreciate any guidance.
0

Share this topic:


Page 1 of 1
  • 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