I am trying to update a database field based on the option that is selected in the View.
<select name="unit_type[]" id="unit_type" class="form-control" style="width:150px;">
<option value="none" selected="" disabled="">Choose</option>
<option value="Unit">Unit</option>
<option value="Sub-Unit">Sub-Unit</option>
</select>
So in this case if Unit is selected a number should be added to a particular field and if Sub-Unit is selected the number should be added to another field.
Model
public function restocks_update(){
$item_id=$this->input->post('item_id');
$qty=$this->input->post('qty');
$unit_type=$this->input->post('unit_type');
for($i=0; $i<count($qty); $i++){
if($unit_type=="Unit"){
$this->db->query("UPDATE `stock` SET `quantity_available`=`quantity_available`+'$qty[$i]' WHERE `id`='$item_id[$i]'");
}else if($unit_type=="Sub-Unit"){
$this->db->query("UPDATE `stock` SET `convert_qty_available`=`convert_qty_available`+'$qty[$i]' WHERE `id`='$item_id[$i]'");
}
}
}
The if statement is in a loop because the select is also generated dynamically via jquery. The issue at the moment is that the fields are not updating, if i remove the else if it updates only one particular field even if it doesn't satisfy the condition.
try unit_type instead of unit_type[]
Related
I have 4 tables in mySQL db :
“product” table (product_id, other fields) - stores products' list
“product-detail” table (detail_id, other fields) - stores details' list
“product_ product-detail” table (product_id, detail_id, detail_value) – each product has many different details, so they are stored in this separate table; detail_value can be any type of data: text, numeric, date/time, boolean
“option” table (option_id, other fields) – options for details’ dropdowns in the form
In my product inserting form, I have two types of fields for providing product’s details (text and select):
// product’s fields
…
…
// product_details’ fields
<input type='text' name="details">
<select name="details">
<option value="12">option 1</option>
<option value="145">option 2</option>
<option value="3456">option 3</option>
</select>
where option value is the id of the provided option (from table "option").
When the form is submitted: one record with product data is inserted in “product” table, then
details of product (2 records):
text value (1-st record) – a string,
option value (2-nd record) – a numeric value
are inserted in table “product_ product-detail”.
So my questions are:
Is it good idea to store different types of data – text and id of the option - in the field “detail_value” (table “product_ product-detail”)?
Is there a better solution to products’ details storage in db?
**Controller**
public function get($id = null)
{
$country = DB::table('countries')->lists('country_name','country_id');
if(intval($id)>0)
{
$customer = Customers::find($id);
}
else
{
$customer = new Customers();
}
return View::make('customerinfo.customer')->with('country',$country)->with('customer',$customer);
}
}
public function save($id = null)
{
if(intval($id)>0)
{
$custom = Customers::find($id);
}
else
{
$custom = new Customers();
}
$custom->name =Input::get('name');
$custom->country_id =Input::get('country_id');
$custom->save();
return Redirect::to('customer/'.$id);
}
**blade**
<div class="form-group">
<strong>Country</strong>
{{ Form::select('country_id', array(''=> '--Select--') +$country, (isset($customer->country_id)? $customer->country_id: 100), ["class"=>"form-control col", "id"=>"country"]) }}
</div>
<div class="form-group">
<strong> Name</strong>
<input type="text" class="form-control" id="name" name="name" minlength="2" value="{{ Input::old('name') != null ?Input::old('name') : $customer->name; }}">
</div>
I am having a custom table that has several items and has a column of status. I have created a dropdown using select for changing the status.
Here, what I want to do is, on change of the status select option, the value of status of that particular row should be updated in the db.
I am able to get the value of the select using jquery but I am not sure how to update the table when multiple dropdowns are being selected together.
I am having the below select options,
<select id ="update-statusDropDown">
<option name="waiting" value="waiting">Waiting</option>
<option name="due" value="due">Due Diligence</option>
<option name="escrow" value="escrow">Escrow</option>
<option name="inspection" value="inspection">Inspection</option>
<option name="closed" value="closed">Closed</option>
</select>
My jQuery is something like below,
jQuery(document).ready(function () {
jQuery('select#update-statusDropDown').change(function () {
//Selected value
var inputValue = $(this).val();
alert("value in js " + inputValue);
//Ajax for calling php function
jQuery.post('update-listing-status.php', {dropdownValue: inputValue}, function (data) {
alert('ajax completed. Response: ' + data);
//do after submission operation in DOM
});
});
});
I want to update status value in the table as per the 'inputValue' from the dropdown. Also, if multiple dropdowns are selected together, how can I update all the values together.
Please can anyone help?
The screenshot of my current table is attached.
You have registered the change event of the select using the ID of that drop down which means only the drop down with that ID will trigger the request you making via the jquery.post
instead use class attribute for the select elements and register the change event on that class
now to get the unique element you can use the data attribute of that select element and option for example data-tableid="something"
in this case you can register all change event of all select elements and be able to extract the values that is only unique to the given table or column name.
$('.class-name').on( 'change' , function(){
// Get the Select itself
var me = $(this);
var tableid = me.data('tableid');
var something = $(this).find(':selected').data('something');
console.log( tableid );
console.log( something );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="class-name" data-tableid="table-id">
<option value="the value" data-something="some value 2">The Title 2 </option>
<option value="the value" data-something="some value">The Title</option>
</select>
So now since you have the ability to uniquely identify which select is being used and which option is being used you can make the request as you wish.
This is my category list
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
The product can have multiple categories.
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
I have these two array first array categories holds all the category.
and second array holds the category which that product has.
I have select tag where all the categories are listed at the time of adding the product.user can select multiple product.
{{category.category_name}}
Now suppose I have added one product with 3 category 3,4,5 respectively.
When I trying to edit that product these 3,4,5 category must be selected because this product is related with these category. So this is my code which is not working.
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
I am confused here how to do the multiple selection when I have array with array.Categories 3,4,5 must be selected among the category.if id do
ng-selected="{{category.category_id ==5}}" like this only 5 category is get selected.how to do the multiple selection with array or multiple values?
I have a solution, use $scope.$watch
$scope.$watch('product_categories', function (nowSelected) {
$scope.selectedValues = [];
if (!nowSelected) {
// here we've initialized selected already
// but sometimes that's not the case
// then we get null or undefined
return;
}
angular.forEach(nowSelected, function (val) {
$scope.selectedValues.push(val.category_id.toString());
});
});
And apply selectedValues via following:
<select multiple ng-model="selectedValues" style="width: 50%;" size="7">
<option ng-repeat="category in categories" value="{{category.category_id}}" ng-selected="{{selectedValues.indexOf(category.category_id.toString())!=-1}}">{{category.category_name}}</option>
</select>
==> Full code at Plunker multi selection ng-selected
Hope it helps!
you forgot to write the ng-app="" in your code ...
something like this !
<div ng-app="">
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</div>
I have added
$watch for the multiple selection.
angular.module("myApp.controllers",[])
.controller("product",function($scope){
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
$scope.$watch('product_categories', function(nowSelected){
$scope.selectedValues = [];
if( ! nowSelected ){
return;
}
angular.forEach(nowSelected, function(val){
$scope.selectedValues.push( val.category_id.toString() );
});
});
});
});
and I made some changes in my view part
<select class="form-control" name="category_list[]" multiple ng-model="selectedValues" >
<option ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</select>
I put all the categories in selectedValues and assigned to select tag as ng model with multiple selection. After this change It works for me.
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>
Need to have a dynamic set of values in an select attibute, depending upon another select attribute.
e.g. there will be two dropdown attributes 1. parent dropdown, 2. child dropdown
if "A" is selected in parent dropdown then "Air","Apple","Ant" will be shown in dropdown.
if "B" is selected in parent attribute then "Ball", "Box", "Base" will be shown.
So basically values of child dropdown will be depended upon the selected value of parent dropdown.
I want to make it dynamic as options can be saved under attributes and those values are to shown in Catalog Product Edit page.
Thanks in advance.
try the below code if you have data inside select box in object or array in JS then you can filter it out easily and append it to select box
Here's Demo DEMO
var data = {
"A": ["Air", "Apple", "Ant"],
"B": ["Water", "Mango", "Fly"]
}
jQuery('#parent').on('change', function() {
var tempData = data[this.value];
var selectChild = jQuery('#child');
jQuery('option', selectChild).remove();
for (var i = 0; i < tempData.length; i++) {
var option = new Option(tempData[i], tempData[i]);
selectChild.append(jQuery(option));
}
});
<select id="parent">
<option value="">Select Parent</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<select id="child">
<option value="">Select Child</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>