yii multi & reverse dependant dropdown menu - ajax

Since this is my first question, do tell me if i miss anything..
I have 3 dropdown menu which are dependant..
1st dropdown is to select table names which will automatically update 2nd dropdown menu which will list attributes names based on the selected table name.. this are successfully implemented..
in 2nd dropdown menu, after selecting the attributes names, user will need to click on the submit button.. when submitting, it will update 2nd dropdown menu to exclude the previously selected attribute names, and will send the selected attribute names to the 3rd dropdown menu..
Updating the 2nd dropdown menu to exclude the previously selected attributes are successfully implemented..
But updating the 3rd dropdown menu are no success..
Do guide me where I'm missing.. I'm still new with Yii so pls be patient with me ok =D
Codes on the controller:
public function actionLoadColumn()
{
$getCol = array();
$col = array();
$getTblNm[] = $_POST['tableNm'];
if(isset($_POST['columnDD'])){
$getCol[]=$_POST['columnDD'];
}
foreach($getCol as $getColKey=>$getColVal){
foreach($getColVal as $getColKey1=>$getColVal1){
$col[]=$getColVal1;
}
}
$getAttr = array();
foreach($getTblNm as $tblKey=> $tbl){
foreach( $tbl as $data){
$model1 = new $data;
$getColumn = $model1->attributes;
foreach($getColumn as $key=>$val){
$getAttr[]=$key;
}
$getAttr = array_combine($getAttr, $getAttr);
unset($getAttr['created_by'],$getAttr['created_at'],$getAttr['updated_by'],$getAttr['updated_at']);
foreach($col as $colKey=>$colVal){
unset($getAttr[$colVal]);
}
}
}
//var_dump($getAttr);
foreach($getAttr as $value=>$city_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);
var_dump($col);
if(isset($_POST['columnDD'])){
echo CHtml::dropDownList('selCol','', $col,//CHTML::listData($getTNm,'tableNm','tableNm'),
array(
'multiple'=>'multiple',
'size'=>15,
//'prompt'=>'SELECT TABLES',
'ajax' => array(
'type'=>'POST',
//'url'=>CController::createUrl('loadcolumn'),
//'update'=>'#columnDD',
//'data'=>array('tableNm'=>'js:this.value'),
)
)
);
}
//foreach($col as $cVal=>$cKey)
// echo CHtml::tag('option', array('value'=>$cVal, 'update'=>'selCol'),CHtml::encode($cKey),true);
}
codes on the view:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'base-company-reg-form',
'enableAjaxValidation'=>false,
));
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx----START----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo '<table><tr><td>1. Select tables</td><td></td><td>2. Select columns</td><td></td><td>3. Arrange columns</td></tr><tr><td>';
echo CHtml::dropDownList('tableNm[]','', $getTNm,//CHTML::listData($getTNm,'tableNm','tableNm'),
array(
'multiple'=>'multiple',
'size'=>15,
//'prompt'=>'SELECT TABLES',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadcolumn'),
'update'=>'#columnDD',
//'data'=>array('tableNm'=>'js:this.value'),
)
)
);
echo '</td><td>';
echo'=';
echo '</td><td>';
echo CHtml::dropDownList('columnDD[]','', array(), array('multiple'=>'multiple', 'size'=>'15'));
echo '</td><td>';
echo CHtml::ajaxSubmitButton('>>>',Yii::app()->createUrl('baseCentre/loadcolumn'),
array(
'type'=>'POST',
'update'=>'#columnDD',
)
);
echo '</td><td>';
//echo CHtml::dropDownList('selCol[]','', array(), array('multiple'=>'multiple', 'size'=>'15'));
echo '</td></tr></table>';
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx----END----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
?>
<?php
$this->endWidget();
?>
</div>

Related

Yii2 Dependant drop down not working when we open update page

Yii2 Dependant drop down not working when we open update page.My depend drop down value came from database. When I create entry It works Very well with the condition. but I when go for Update then Pre selectect value does not show. i.e. Which I save while creating. This is my _form.php
$arrCountry = \yii\helpers\ArrayHelper::map($modelCountry, 'country_id', 'country');
echo $form->field($model, 'country_id')->dropDownList($arrCountry,
['prompt'=>'-Choose a country-',
'onchange'=>'
$.post( "'.Yii::$app->urlManager->createUrl('country/lists?id=').'"+$(this).val(), function( data ) {
$( "select#city_name" ).html( data );
});
']);
$dataPost=\yii\helpers\ArrayHelper::map(City::find()->asArray()->all(), 'city_id', 'city_name');
// print_r($dataPost);die;
echo $form->field($model, 'city_id')
->dropDownList(
$dataPost,
['id'=>'city_name']
);
2.Controller
public function actionLists($id)
{
$countCity =City::find()
->where(['city_id' => $id,'status'=>'not assign'])
->count();
$arrCity = City::find()
->where(['city_id' => $id,'status'=>'not assign'])
->orderBy('city_id DESC')
->all();
if($countCity>0){
foreach($arrCity as $arrCity){
echo "<option value='".$arrCity->city_id."'>".$arrCity->city_name."</option>";
}
}
else{
echo "<option>-</option>";
}
}

Yii2 dropDownList get selected value

I have dropDownList in my view ( data from db) :
use yii\widgets\ActiveForm;
\app\assets\HelloAsset::register($this);
$form = ActiveForm::begin();
$users=\app\models\User::find()->all();
$items=\yii\helpers\ArrayHelper::map($users,'id','username');
echo $form->field($model, 'username')->dropDownList($items);
echo \yii\bootstrap\Html::submitButton('Send');
ActiveForm::end();
I want to get data from 'username' in my controller:
$model=new Data();
if ($model->load(\Yii::$app->request->post())){
echo 'it is status:'.$model->username;
}else {
return $this->render('simple', ['model' => $model]);
}
But data is null.
How I must get data in right?
Please, help me solve this problem.

Dependanat Drop down is not working when it has only one value in yii framework

I am using dependant dropdownlist for get value for subject dropdown when select value from 'Grade' dropdown. It is working fine. But the problem is when there are only one value in grade dropdown the subject dropdown is not updated.
this is my code:-
Grade DropDown----------
($data is consist of grade)
<?php echo CHtml::dropDownList('myDropDown1','',$data,array(
'id'=>'gr',
'empty' => '(Select Grade)',
'style'=>'width:200px',
'ajax' =>
array(
'type'=>'POST', //request type
'url'=>CController::createUrl('sub'), //action to call
'update'=>'#cls', // which HTML element to update
)
)); ?>
Subject Dropdown (which depend on grade dropdown)------------
<?php echo CHtml::label('Subject',''); ?>
<?php echo CHtml::dropDownList('myDropDown3','',array(),array(
'id'=>'sub',
'prompt'=> 'Please select a class', 'style'=>'width:150px',
'style'=>'width:200px',
)); ?>
<?php //echo $form->error($model,'sub_id'); ?>
In controller-------------------
public function actionClass()
{
$grd = $_POST['myDropDown1'];
$c_id = TbClass::model()->findAll('id=:id',
array(':id'=>$grd,));
$data3 = CHtml::listData($c_id,'id','grade');
$grd2 = array_shift($data3);
$sub1 = TbClass::model()->findAll('grade=:grade',
array(
':grade'=>$grd2,
));
$data4 = CHtml::listData($sub1,'id','class');
foreach($data4 as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
This code is working fine. Problem is when grade has only one value in the dropdown , cannot update the subject dropdown.
I think your problem is in array_shift,
have you checked the value of $grd2 ?

Wordpress : Update Loop with Form

I have a photo gallery I'm developing using WP as a means for content management. I'm relying heavily on some jQuery plugins to manage the UI styling and manipulation (via sorting). My problem is there are too many damn posts! 737, and each has a thumbnail which is displayed on the page. This inevitably bogs down any browser. Especially since the sorting plugin "clones" the images when it sorts them.
The posts are built using a Wp_query script;
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 15; // (-1 shows all posts) SETS NUMBER OF IMAGES TO DISPLAY!
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'post_type' => array ('post'),
'orderby' => 'rand',
'order' => 'ASC',
'paged' => $paged,
'posts_per_page' => $post_per_page
);
$pf_categorynotin = get_post_meta($wp_query->post->ID, true);
if($pf_categorynotin){
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $pf_categorynotin,
'operator' => 'NOT IN'
)
); //category__in
}
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
//Begin LOOP #1
if( have_posts() ) :
echo '<ul id="applications" class="applications pf_item3">';
$r = 0;
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_cat = array();
$post_cat = wp_get_object_terms($post->ID, "category");
$post_cats = array();
$post_rel = "";
for($h=0;$h<count($post_cat);$h++){
$post_rel .= $post_cat[$h]->slug.' ';
$post_cats[] = $post_cat[$h]->name;
}
$r++;
echo'<li data-id="id-'. $r .'" data-type="'.$post_rel.'" >';
if (get_post_meta($post->ID, 'port_thumb_image_url', true)) { ?>
<a class="tozoom" href="<?php echo get_post_meta($post->ID, 'port_large_image_url', true); ?>" rel="example4" title="<?php echo $post->post_title; ?>">
<img src="<?php echo get_post_meta($post->ID, 'port_thumb_image_url', true); ?>" class="portfolio_box" alt="<?php the_title(); ?>" width="199px" height="134px" /></a>
<?php } ?>
</li>
<?php endwhile ?>
</ul>
and the items are sorted by their html5 tags with a menu in the sidebar.
You can see it working here;
http://marbledesigns.net/marbledesigns/products
Right now it randomly loads 15 posts and displays them. I want to be able to reload posts based on my selection from the menu (via categories) and then update the list of images without refreshing the page. I want to be able to change not only which posts from which category are displayed, but also the posts per page as well.
I think AJAX is the way to do this, but I don't want to undo a whole bunch of code in the menu in order to get it working. Right now the menu is styled radio buttons. Isn't there a way I can take the same input from that form and update the parameters of the loop?
Looking for an efficient solution! Please help!
I'm going to abandon this method for reasons of time restriction. I've decided to use AJAX as a way to pull the list form the linked page and display it on the current one.
I followed this tutorial,
http://www.deluxeblogtips.com/2010/05/how-to-ajaxify-wordpress-theme.html
changed a couple names in the ajax.js script and it worked great!

Issue with active record update in CodeIgniter

I have a form that submits data to a database in a CodeIgniter app (CI v. 2.0.2). We are allowing users to "edit" records by having them resubmit a record with new values and then doing an update. On submit, the form calls the vote controller's create method. Inside the create method, we check to see if there's already a record based on an entry code and the user's ID. If there is, we update; otherwise we create a new record. The creation works just fine; it's only the update I'm having an issue with. Here's the code.
view
<div id="vote_form">
<?php
$hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));
echo form_open('vote/create');
$entry_code_data = array(
'name' => 'entry_code',
'id' => 'entry_code',
'value' => set_value('entry_code')
);
echo form_hidden($hidden);
$score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
?>
<p><label for="entry_code">Entry code: </label><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, ''); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
</div>
controller
function create() {
$id = $this->input->post('entry_code');
$judge_id = $this->input->post('dot_judge_id');
$data = array(
'entry_code' => $id,
'dot_judge_id' => $judge_id,
'q1' => $this->input->post('q1'),
'q2' => $this->input->post('q2'),
'q3' => $this->input->post('q3'),
'q4' => $this->input->post('q4'),
'q5' => $this->input->post('q5'),
);
//first check to see if there's already a record for this judge/entry
//if so, update. Otherwise, insert
$vote_id = $this->vote_model->getEntryById($id, $judge_id);
if($vote_id) {
log_message('debug', 'vote id exists: '.$vote_id);
$this->vote_model->updateRecord($data, $vote_id);
}
else {
log_message('debug', 'vote id does not exist; creating new');
$this->vote_model->createRecord($data);
}
/*
after submission, go to another page that gives choices - review entries, submit another entry, log out
*/
$data['msg'] = "Entry submitted";
$this->menu();
}
model
function getEntryByID($id, $judge_id) {
//determine if record already exists for entry/judge
$sql = 'SELECT vote_id from dot_vote WHERE entry_code = ? AND dot_judge_id = ?';
$query = $this->db->query($sql, array($id, $judge_id));
if($query->num_rows() == 1) {
$row = $query->row();
return $row->vote_id;
}
else {
return false;
}
}
function createRecord($data) {
$this->db->insert('dot_vote', $data);
return;
}
function updateRecord($data, $vote_id) {
log_message('debug', 'vote id is passed: '.$vote_id);
$this->db->where('vote_id', $vote_id);
$this->update('dot_vote', $data);
}
I know it's making it into the updateRecord method because the log_message output is in my log file displaying the correct vote_id (the auto-increment field of the returned record). But what I get in the browser is the following:
Can anyone point me in the right direction here? I have error display on in my config file, and have gotten SQL errors when they occur, so I don't think this is a SQL error. But I have no idea what could be happening in that tiny little function that's causing this. My next step is to skip the active record update and just use a standard query, but I'd like to know what's causing the problem here, even if I can get it to work the other way.
This line stood out:
$this->update('dot_vote', $data);
Do you mean this?
$this->db->update('dot_vote', $data);

Resources