i have a countries, states, cities tables , and I need 3 dropdown menus
wherein, if i select a country like e.g United States, the states dropdown menu will automatically show only the states that are under United States, and next, if I select a State like e.g California, the Cities drop down menu will only show off the Cities under California
am currently having problem implementing this in yii. I have this _form file
where these 3 dropdown should be included. I have a partial code, but I can't figure out how to make this work out
this is from the Controller
public function actionDynamicstates()
{
$sql = "SELECT StateName FROM gg_t_worldareasstates ".
"WHERE CountryID = :countryid";
$command = Yii::app()->createCommand($sql);
$command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);
$data = $command->execute();
$data = CHtml::listData($data,'StateID','StateName');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
this is from the _form view file
<div class="row">
<?php
$country = new CDbCriteria;
$country->order = 'CountryName ASC';
echo $form->dropDownList($model, 'CountryID',
CHtml::listData
(
Worldareascountries::model()->findAll($country),
'CountryID',
array
(
'ajax' => array
(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
'update' => '#StateID',
)
)
)
);
echo $form->dropDownList('StateID','', array());
?>
</div>
I changed that those codes above into this one
<div class="row">
<?php echo $form->labelEx($model,'Country'); ?>
<?php
$country = new CDbCriteria;
$country->order = 'CountryName ASC';
?>
<?php
echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
'update' => "#StateID"
)
)
);
?>
<?php echo $form->error($model,'CountryID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'State'); ?>
<?php
$state = new CDbCriteria;
$state->order = 'StateName ASC';
?>
<?php
echo $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamiccities'),
'update' => '#CityID'
)
)
);
?>
<?php echo $form->error($model,'StateID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'CityID'); ?>
<?php echo $form->dropDownList($model,'CityID','',array());?>
<?php echo $form->error($model,'CityID'); ?>
change this line
$data = $command->execute();
to
$data = $command->query();
this problem was solved, it's on the yii wiki docs
Related
I have a problem with my form. When i input the form and submit, the form didn't save to my database.
In my form showing code transaction, and email customer. And if customer buying product in my website.
I just confirmation the order with click form code transaction.
In form code transaction showing, code transaction and email customer. And me input the logistic name, no. order of logistic to customer tracking their order, and choice the order packet has sending.
But in my problem the form succes save the order packet information "has sending" , but for logistic name and no. order of logistic didn't save.
This is my view.
<div id="<?php echo $row['codetrans'] ?>" class="reveal-modal">
<h3>Detail Order</h3>
<?php echo form_open('order/edit_stats_order');
$codetrans = array(
'name' => 'codetrans',
'class' => 'column',
'size' => '30'
);
$email = array(
'name' => 'email',
'class' => 'column',
'size' => '30'
);
$no_order = array(
'name' => 'no_order',
'class' => 'column',
'size' => '30'
);
$log = array(
'FedEx' => 'FedEx',
'RPX' => 'RPX');
?>
<span>Code :</span>
<?php echo form_input($codetrans,$row['codetrans']) ?><br/>
<span>Email :</span>
<?php echo form_input($email,$row['email']) ?><br/>
<span>Logistic Name :</span><br/>
<?php echo form_dropdown('log', $log,$row['log']); ?><br/><br/>
<span>No. Order :</span>
<?php echo form_input($no_order,$row['no_order']) ?><br/>
<?php $options = array(
'0' => 'Not Sendirng',
'1' => 'Has Sending'); ?><br/>
<?php echo form_dropdown('stats_order', $options,$row['stats_order']); ?><br/>
<?php echo form_hidden('input_hidden_id',$row['codetrans']);?>
<?php echo form_submit('submit', 'Submit', 'class=searchbutton'); ?>
<?php echo form_close();?>
<a class="close-reveal-modal">×</a>
</div>
<?php
}
?>
</ul>
<?php echo anchor('order/stats_order', 'Open All Stats', 'class="btn btn_aS"' ); ?>
</div>
This my controller.
public function edit_stats_order() {
$this->load->model('Order_model');
$codetrans = $this->input->post('codetrans');
$email = $this->input->post('email');
$log = $this->input->post('log');
$no_order = $this->input->post('no_order');
$stats_order = $this->input->post('stats_order');
if($email !='' && $codetrans !='' && $log !='' && $no_order !=''){
$this->Order_model->edit_stats_order($codetrans,$email,$log,$no_order);
redirect('order/main');
}
else{
echo "<script>alert('Check Again!');</script>";
echo "<script>location.href = document.referrer</script>";
}
}
This is my model.
public function edit_stats_order($codetrans,$email,$log,$no_order){
date_default_timezone_set('xxxxx');
$data['email'] = $email;
$data['no_order'] = $no_order;
$data['log'] = $log;
$data['stats_order'] = $stats_order;
$data['date'] = date('Y-m-d');
$this->db->where('codetrans',$codetrans);
$this->db->update('ordering',$data);
}
Why the fault in my code?
Thanks
You are trying to use $stats_order in model function which is not defined in model. So you have to pass $stats_order to model function.
Add one more parameter to the function call for $stats_order in order to pass it to the model.
$this->Order_model->edit_stats_order($codetrans,$email,$log,$no_order,$stats_order);
Also add one more parameter in model function for the same. Change the first line of the function in model to,
public function edit_stats_order($codetrans,$email,$log,$no_order,$stats_order){
I Have written the following function in the Categories Controller. which check the categories in the database and returns a response. Now if the parent_cat_id: "1" then I want to show the select2 field from the Form
public function parent_categories(){
$table = "store_categories";
$selectData = "id AS ID, cat_title AS TEXT,parent_cat_id";
$search = $this->input->get('q');
$where = array('status' => "enabled");
if(isset($search) && !empty($search)){
$field = "Title";
$Result = $this->Common_model->select_fields_where_like_join($table,$selectData,"",$where,FALSE,$field,$search);
}else{
$Result = $this->Common_model->select_fields_where_like_join($table,$selectData,"",$where);
}
if(empty($Result)){
$emptyArray = array(
array(
'ID' => 0,
'TEXT' => "No Record Found"
)
);
print json_encode($emptyArray);
return;
}
print json_encode($Result);
}
This is the select2 Field in form
I have tried something like this but I am sure this not works. I have write this <?php if(parent_cat_id == "1"){?> for you people to understand what i am saying that how to perform action like this on JSON response
<?php if(parent_cat_id == "1"){?>
<div class="col-md-4">
<div class="form-group">
<label for="Header Text" class="control-label">Parent Category</i>
</label>
<select name="parent_categories" id="parent_categories" class="form-control select2" ></select>
</div>
<!-- /.form-group -->
</div>
<?php } ?>
This is the JSON Response
{
ID:"2",
TEXT:"waqas",
parent_cat_id:"1"
}
$emptyArray = array(
array(
'ID' => 0,
'TEXT' => "No Record Found"
)
);
$json = json_decode(json_encode($emptyArray));
echo $json->parent_cat_id;
First off you need to use json_decode instead of encode in order to be able to work with the json data. Json decode will turn it into a object or an associative array for you which you can then use as you please.
Since i've to position checkboxes in design I'm not using MultiCheckbox and using Checkbox instead in zend. I've seen some zf1 solutions but didnt found any zf2 or zf3 solutions.
My Php Code
$languages = new \Zend\Form\Element\Checkbox('languages[]');
$languages->setLabel('English');
$languages->setValue('1');
This produce the following output
<?php
echo $this->formRow($form->get('languages[]'));
//It produce the following
?>
<input type="checkbox" name="languages[]" value="1" checked="checked">
How can I add more items with name "languages[]" without writing direct HTML code ?
You can use a Form collection for this:
1) In your form:
use Zend\Form\Element\Collection;
...
$languages = new \Zend\Form\Element\Checkbox();
$languages->setLabel('English');
$languages->setValue('1');
$languages->setName('language');
$collection = new Collection();
$collection->setName('languages');
$collection->setLabel('Language Collection');
$collection->setCount(2);
$collection->setTargetElement($languages);
$collection->populateValues(array(
1, 0
));
$this->add($collection);
2) Do not forget to prepare your form in your controller action:
$form->prepare();
3) Finally, in your view, get all elements of the collection and render them separately:
<?php $elements = $form->get('languages')->getElements(); ?>
<?php //echo $this->formCollection($form->get('languages')); ?>
<?php echo $this->formRow($elements[0]); ?>
<?php echo $this->formRow($elements[1]); ?>
You can use MultiChebox
https://framework.zend.com/manual/2.2/en/modules/zend.form.element.multicheckbox.html
It works similar to radio form element. Your example implementation
use Zend\Form\Element;
$languages = new Element\MultiCheckbox('languages');
$languages->setLabel('Used languages');
$languages->setValueOptions([
'en_US' => 'english',
'de_DE' => 'german',
'pl_PL' => 'polish',
]);
$languages->setValue('en_US'); //default value; you can use array
My solution (Method 2)
Method 1:
Give name as "language[1]" instead of "language[]". By using this I can call the elements in view seperately.
Creating form.
$language1 = new \Zend\Form\Element\Checkbox('language[1]');
$language2 = new \Zend\Form\Element\Checkbox('language[2]');
$form = new Form('Set');
$form->add($name)
->add($language1)
->add($language2)
->add($submit);
In view file
<div><?php echo $form->get('language[1]');?></div>
<div><?php echo $form->get('language[2]');?></div>
Edit: Method 2
Using fieldset
//Create form
$languages = [
['id' => 1, 'language' => 'English'],
['id' => 2, 'language' => 'Malayalam'],
] ;
$fieldSet = new \Zend\Form\Fieldset('languages') ;
foreach( $languages as $one ) {
$c = new \Zend\Form\Element\Checkbox($one['id']);
$c->setLabel($one['language']) ;
$fieldSet->add($c) ;
}
//Add collection of checkboxes to form
$form->add($fieldSet) ;
In view file
<?php
$language = $form->get('languages') ;
?>
<div class="form-group row">
<label class="control-label col-sm-2" >Choose Languages</label>
<div class="col-sm-10">
<?php foreach($language as $one ) { ?>
<?php echo $this->formCheckbox($one); ?> <span> <?php echo $this->formLabel( $one ) ; ?> </span>
<?php echo $this->formElementErrors($one); ?>
<?php } ?>
</div>
</div>
One of my Wordpress themes is showing the following error:
Fatal error: Call to undefined method WP_Error::get_items() in /home1/mywebsite/public_html/learnphp123.com/wp-content/themes/econews/admin/IDE_admin.php on line 88
The code for the file is (you can see "get_items()" on line 88 .... actually the third line in the code):
if($feed) {
$html = '<ul>';
foreach ($feed->get_items() as $item){
$html.="<li><span>".$item->get_date('d M Y')."</span> ".$item->get_title()."</li>";
}
Can anybody tell me what to use in the place of "get_items()"?
Thank you very much for your help!
The total code of the file is given below and I am only getting the error on line 88 related to feed get_items ().
<?php
/*
Copyright 2010, idesigneco.com
http://idesigneco.com
*/
define('IDE_ADMIN_FEED', 'http://idesigneco.com/wp_pub.php?id=ide_admin_newsfeed.xml&theme='.IDE_CODE.'&ver='.IDE_VERSION);
// load form generator and validator
include IDE_ADMIN_PATH.'IDE_FormGenerator.class.php';
// initialize the admin form
$Form = new IDE_FormGenerator(array(
'name' => 'admin',
'method' => 'post',
'action' => ''
));
// setup the form fields
$Form->elements(
null,
array(
'admin_action' => array(
'type' => 'hidden', 'value' => 'admin',
'error' => 'Your settings have been updated.'
)
)
);
// load theme specific options
include IDE_ADMIN_PATH.'/IDE_admin_options.php';
$Form->elements(
null,
array(
'submit' => array(
'type' => 'submit', 'value' => 'Save', 'label' => ''
),
)
);
$Form->printErrors(true);
// ________________________________________________________
// process the form data
if(!empty($_POST['admin_action'])) {
// unchecked checkbox options don't show up, so fill them with predefined key names
ide_save_options( array_merge( array_fill_keys($IDE_checkboxes, ''),
stripslashes_deep($_POST)
)
);
$Form->setErrors(array('admin_action'));
}
// ________________________________________________________
// populate the form with saved options
$Form->data($IDE_options);
// ________________________________________________________
// print out the admin area
ide_admin_header();
$Form->generate();
ide_admin_footer();
// ________________________________________________________
// idesigneco news feed
function ide_admin_news() {
$time = ide_option('admin_newsfeed_time');
$html = ide_option('admin_newsfeed');
// feed expired, update
if(!$html || !$time || ($time+(5*3600)) < time() ) {
if(function_exists('fetch_feed')){
$feed = fetch_feed(IDE_ADMIN_FEED);
if($feed) {
$html = '<ul>';
foreach ($feed->get_items() as $item){
$html.="<li><span>".$item->get_date('d M Y')."</span> ".$item->get_title()."</li>";
}
$html.= '</ul>';
} else {
$html = 'Updates unavailable at this time';
}
} else {
// deprecated feed api
if(function_exists('fetch_rss')){
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss(IDE_ADMIN_FEED);
$html = '<ul>';
foreach ($rss->items as $item){
$html.="<li><span>".date('d M Y', strtotime($item['pubdate']))."</span> ".$item['title']."</li>";
}
$html.= '</ul>';
}
}
ide_save_option('admin_newsfeed_time', time());
ide_save_option('admin_newsfeed', $html);
}
echo $html;
}
// admin header
function ide_admin_header() {
?>
<div id="ide_admin">
<div class="left">
<h1 class="ide_title"><?php echo IDE_NAME.' '.IDE_VERSION; ?></h1>
<div class="clear"> </div>
<?php
}
// admin footer
function ide_admin_footer() {
?>
</div><!-- left //-->
<div class="right">
<div class="idesigneco">
<img src="<?php echo IDE_ADMIN_STATIC.'/idesigneco.png'; ?>" alt="Theme by iDesignEco" title="Theme by iDesignEco" />
</div>
<div class="news">
<h2>iDesignEco Updates</h2>
<?php ide_admin_news(); ?>
</div>
</div><!-- right //-->
<div class="clear"> </div>
</div><!-- ide_admin //-->
<?php
}
?>
This probably happens when getting data for $feed fails, so it becomes WP_Error class that does not have get_items() method. Try to modify the code like this:
if (! is_wp_error( $feed )) {
$html = '<ul>';
foreach ($feed->get_items() as $item){
$html.="<li><span>".$item->get_date('d M Y')."</span> ".$item->get_title()."</li>";
}
UPDATE:
Just as I assumed, the $feed gets its data from fetch_feed(), so the solution should still be to replace:
if($feed) {
with:
if (! is_wp_error( $feed )) {
I had created two database tables as,
Country
State
In Country table i had countryid & cname field. In State table i had state_id, state_name & country_cid.
Now i need to make a dependent dropdown i.e. when i select country dropdown box, then the particular state for that country should be displayed. I had done below coding. But it displays only country and not displays state.
This is in views/sample/register.php file.
<div class="row">
<?php echo $form->labelEx($model,'countryid');
$opts = CHtml::listData(Country::model()->findAll(),'countryid','cname');
echo $form->dropDownList($model,'country_id',$opts,
array(
'prompt'=>'Select Country',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('SampleController/dynamicSubcategory'),
array('coountry_id'=>'js:this.value'),
'dataType' => 'JSON',
'success'=>'js:function(data)'
. '{'
. 'var html="<option value=>-----Select city-----</option>";'
. '$.each(data,function(i,obj)'
. '{'
. 'html+="<option value=\'"+obj.id+"\'>"+obj.name+"</option>";'
. '});'
. '$("#state_id").html(html);'
. '}'
)));
echo $form->error($model,'country_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state_id');
echo CHtml::dropDownList('state_id','', array());
echo $form->error($model,'state_id'); ?>
</div>
This is in controllers/SampleController.php file
public function actiondynamicSubcategory()
{
$countryId=$_POST['coountry_id'];
$criteria=new CDbCriteria();
$criteria->select=array('state_id,state_name');
$criteria->condition='country_cid='.$countryId;
$criteria->order='state_name';
$cityAry= State::model()->findAll($criteria);
$ary=array();
foreach($cityAry as $i=>$obj)
{
$ary[$i]['state_id']=$obj->id;
$ary[$i]['state_name']=$obj->name;
}
echo json_encode($ary);
}
I had created Country model & State model. I had analyzed many sites. But i can't get right. Please anybody help.
You can try below code which I have used in one of my projects -
**Code in the view file :**
<?php $arrOptionsCountry = array('prompt'=>'Select Country','ajax'=> array('type'=>'POST','url' => ApplicationConfig::getURL('', 'site','SearchValueStates'),'update'=>'#Airports_state','beforeSend'=>'stateLoading'));
echo $form->dropDownList($model,'country',$arrCountryList,$arrOptionsCountry,array('class'=>'span11 customDropdown1_select'));
$arrOptionsState = array('prompt'=>'Select State','ajax'=> array('type'=>'POST','url' => ApplicationConfig::getURL('', 'site', 'SearchValueCities'),'update'=>'#station_name','beforeSend'=>'cityLoading'));
echo $form->dropDownList($model,'state',$arrStatesList,$arrOptionsState);
echo $form->dropDownList($model,'city',$arrCityList);
?>
**Site Controller :**
public function actionSearchValueStates()
{
$arrParam =array();
if(isset($_POST['Airports']['country']))
{
$Term = $_POST['Airports']['country'] ;
$case = "STATE-LIST";
$prompt = "Select State";
$arrParam['id'] = isset($Term)?$Term:null ;
$data = States::getList($case,$arrParam);
$data = CHtml::listData($data,'id','name');
echo CHtml::tag('option',array('value'=>''),$prompt,true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name));
}
}
Yii::app()->end();
}
I got the answer for my question.
This is in views/sample/register.php file
<div class="row">
<?php echo $form->labelEx($model,'country');
$opts = CHtml::listData(Country::model()->findAll(),'countryid','cname');
echo $form->dropDownList($model,'country_id',$opts,
array(
'prompt'=>'Select Country',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Sample/dynamicSubcategory'),
'update'=>'#state_name',
'data'=>array('country_id'=>'js:this.value'),
)));
echo $form->error($model,'country_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state_id');
echo CHtml::dropDownList('state_name','', array(), array(
'prompt'=>'Select State'
));
echo $form->error($model,'state_id'); ?>
</div>
This is in controllers/SampleController.php file
public function actiondynamicSubcategory()
{
$data=State::model()->findAll('country_cid=:countryid',
array(':countryid'=>(int) $_POST['country_id']));
$data=CHtml::listData($data,'state_id','state_name');
echo "<option value=''>Select State</option>";
foreach($data as $value=>$state_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($state_name),true);
}
This code works fine for dependent dropdown in yii