I want to add table structure in drop down list - php

I want to add two colums from my database table into a drop-down list.
I have a fruits table in my database, and I want to fetch the name of the fruit and it's quantity from the database and insert it into my select drop-down list. But it should be in an option tag, and it should be in table structure.
<select id="availableFruits" name="availableFruits" multiple size=7 style="width:auto;float:left;" >
<?php $fruitList=$this->fruitList;
foreach ($fruitList as $key => $val)
{
printf('<option value="%s"><table><tr><td>%s</td><td>%s</td></tr></table></option>',$val,$val,$val);
//printf('<option value="%s">%s</option>', $val, $val);
} ?>
</select>
This is my code for the drop-down list which I have written in my HTML. Here I am dynamically adding the fruit's name and quantity in the drop-down list, but it's not printing it in a table structure.
The structure should be like:
<option value="%s"><table><tr><td>%s</td><td>%s</td></tr></table></option>

For that Better to use Jquery plugin, to display the Dropdown as per your Choice
May the below link would help you
http://www.jquery4u.com/plugins/10-jquery-selectboxdrop-down-plugins/

You cannot add html elements in <option>

Related

How to append "HTML tags" before and after each dropdown list item in Yii2 Active Form

I have a list of Different colours. I have successfully created a dropdown list showing each colour name. But now my client wants to show the colour badges in front of each colour name. I have colour codes stored too in my database but is there any way I can show the colour badges in front of colour names.
I also tried ArrayHelper::map function like below
$colours_list_array = ArrayHelper::map($colours_list, 'id', 'colour_name');
But the above is showing me the error.
I also tried writing the direct html to my colour names in the database but now in the dropdown, it is showing me the exact same html tag as a String.
Can anyone please tell me how can I show the colour badges in front of the list item??
->> Below is my database table of colours
->> Here is my ActiveForm Input of Colours Dropdown
<?= $form->field($colours_model, 'colour_name')->dropDownList(
$colours_model->getColours(),
) ?>
Change function name as per your code
$colors = $colours_model->getColours(); //Assuming this function retrieving all records
$arrayColour = $arrayColorList = [];
foreach($colours as $colour){
$colour_id = $colour->getId(); //get 'id' column data
$colour_name = $colour->getColourName(); //Get 'colour_name' column data
$colour_code = $colour->getColourCode(); //Get 'colour_code' column data
$arrayColorList[$colour_id] = $colour_name;
$arrayColour[$colour_id] = ["style" => "background-color:$colour_code"];
}
$form->field($colours_model, 'colour_name')->dropDownList($arrayColorList, ['prompt' => 'Please select', 'options' => $arrayColour])->label(false);
Output:
<select>
<option value='3' style="background-color:#FFFFF">White</option>
<option value='4' style="background-color:#C0C0C0"><b>SILVER</b></option>
.
.
.
</select>
Inspect this dropdown and check whether "style" attribute is appending in each option list or not. Then, test it in Chrome browser first. Mozilla having some problem related to this.
I had similar requirement in one of the project and with same code it's working fine for me.

Yii: Change an input text field value depending on dropdown list selection

I inherited a YII based web application but I am not really familiar with Yii and don't find the solution yet. So if you can, please help me!
There is a form. In the form there is a dropdown list which get data from a db table. This table contains the name and the price of leases. The dropdown lists only the name.
<?php
$typeNames = array();
foreach(LeaseType::model()->findAll() as $lt) {
$typeNames[$lt->id] = $lt->name;
}
?>
<?php echo $form->dropDownListControlGroup($model, 'lease_type_id',
$typeNames,
array(
'empty'=>'Válassz!',
'onchange'=>'$("#model-payed").val($("#model-lease_type_id option:selected").text());',
)
); ?>
But I need to change the value of a specific input text field with the price of the selected lease.
<?php echo $form->textfield($model,'payed');?>
I can get the name of the lease but I can't set the value of the input for the price of the lease. This field has to be changable based on the customer payed all the price or just partial.
Other problem is that the name and price of the lease in different table than the value needed.
I hope all this are understandable because I am not fluent in English.
Thanks for any help!
Finally I found the solution, so share it maybe someone can use it.
In the views/_form.php I modified the dropdownlist:
<?php
echo $form->dropDownListControlGroup($model, 'lease_type_id', $typeNames, array(
'empty' => 'Válassz!',
'onchange' => '$("#model_comment").val($("#model_lease_type_id option:selected").$typeNames);',
//'onchange'=>'if($(this).val() == "no"){("#quantity").val("0"); }'
)
);
?>

Yii Dropdownlist - showing the column values from a table

I want to add a dropdown list in my page which should show the values from a table column, How can I do this using Yii framework?
I have a table(table name is program) with columns
id, program_name, is_active
I have created a new controller and a view associated with it, I need to show a dropdown list in that view with the values populated from program_name
I would solve this at model level. e.g.
In Machine model, I would define a getter :
public function getCompleteMachineName ()
{
return $this->merk->name.' '.$this->name;
}
And, in your listData :
Chtml::listData(Machine::model()->with('merk')->findAll(...),
'machine_id', 'completeMachineName')
All you need to do is use CHtml::dropDownList, possibly with CHtml::listData to ease the process of creating a value=>display array for the <options> tag.
Example (see comments in code):
echo CHtml::dropDownList(
'somename',// for "name" attribute of <select> html tag,
// this also becomes the "id" attribute, incase you don't specify
// it explicitly in the htmlOptions array
'', // the option element that is to be selected by default
CHtml::listData( // listData helps in generating the data for <option> tags
Program::model()->findAll(), // a list of model objects. This parameter
// can also be an array of associative arrays
// (e.g. results of CDbCommand::queryAll).
'id', // the "value" attribute of the <option> tags,
// here will be populated with id column values from program table
'program_name' // the display text of the <option> tag,
// here will be populated with program_name column values from table
),
array('id'=>'someid'), // the htmlOptions array, whose values will be
// generated as html attributes of <select> and <option> tags
);
Edit Incase you don't have a CActiveRecord model for program table, you could use direct sql, replace Program::model()->findAll() in the above sample with:
Yii::app()->db->createCommand()->select('id, program_name')->from('program')->queryAll()
Also if you want to use program_name column values as the value attribute for the option tag, then you can use:
CHtml::listData($data,'program_name','program_name') // replaced the 'id' with 'program_name'

Country selection for a web form?

How to create a HTML dropdown with list of countries , is there any web services which i can use ? without entering manual.
You could use this jQuery plugin:
JQuery Autocomplete Plugin extension for country list
Get an array of country names like this one.
Then use this in your file:
<select name="countries">
<?php
$countries = array(
'AF'=>'AFGHANISTAN',
'AL'=>'ALBANIA',
'DZ'=>'ALGERIA',
'AS'=>'AMERICAN SAMOA',
...rest of data from site link above
);
foreach($countries as $key => $array) {
?><option value="<?=$key?>"><?=$value?></option>
}
</select>
?>
Explanation:
Creates a array of country names, then goes through one at a time and writes each country name into an <option> within your <select></select> tags.

Update a database item with price incl/excl taxes depending on a radio button

I am making a kind of checkout form. At the top I have two radio buttons, one that should show prices without tax when clicked, and another to show prices when clicked.
<form id="f-p" method="post" action="#####">
<label for="exkl">Exkl. moms</label><input name="moms" id="exkl" type="radio"value="exkl" checked="checked"/>
<label for="inkl">Inkl. moms</label><input name="moms" id="inkl" type="radio" value="inkl"/>
</form>
Then I have prices below in tables. Those prices all come from a MySQL database. Now I can, separately, query the database for product IDs:
if($_POST['moms'] == "inkl") {
$inkl_query ="SELECT `product_id` FROM `######products_fieldvals` WHERE `fielddef_id`=4";
$iresult = mysql_query($inkl_query);
but I want to cycle through and compare product IDs between two tables, and if they match, to isert the price for that item from the first table into the second one, and to toggle back for the other radio. How can I do this? I want to use:
if($list1ID = $list2ID) {
INSERT INTO products(price)
SELECT value FROM products_fieldvals WHERE fieldval_id="4",
}
but that isn't working. Can someone please help?
if($list1ID == $list2ID) {
mysql_query("INSERT INTO products(price) SELECT value FROM products_fieldvals WHERE fieldval_id='4'");
}

Categories