I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
The output from the request on all three arrays is here: PasteBin
My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.
I don't think the HTML is very relevant, but here is a sample anyway:
<div class="well form-group ">
<div class="col-sm-2 col-md-2">
<label for="nails_staples">Nails & Staples:</label>
</div>
<div class="col-sm-4 col-md-4">
<select class="form-control product_id" name="product_code[10]">
<option selected="selected" value="">None</option>
<option value="8769">1 1/4 Coil Nails - box | $26.00</option>
<option value="6678">2" Hot Dipped Shake Nails | $135.00</option>
</select>
</div>
<div class="col-sm-1 col-md-1">
<label for="nails_req">Quantity:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control quantity" name="quantity[10]" type="text">
</div>
<div class="col-sm-1 col-md-1">
<label for="cost">Cost:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control cost" readonly="readonly" name="cost[]" type="text">
</div>
</div>
There are a few issues with this approach. Ideally you'd have separate calls to a controller for each entity you want to change, using AJAX to make each individual call as the user changes the entity. This solves the issue of attributes not being strongly tied to the entity ID (in your case, product_code).
If you're willing to make the assumption that all of the arrays match up, you should at least check that they are the same length.
Obviously I don't know all the specifics of your Model or exactly what you're looking for, but based on your paste bin, this should work.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
$count_ids = count($id);
if(count($qty) != $count_ids || count($cost) != $count_ids) throw new Exception("Bad Request Input Array lengths");
for($i = 0; $i < $count_ids; $i++){
if (empty($id[$i])) continue; // skip all the blank ones
$newProduct = new Product();
$newProduct->code = $id[$i];
$newProduct->qty = $qty[$i];
$newProduct->cost = $cost[$i];
$newProduct->save();
}
Related
I'm creating an eCommerce website using HTML and PHP. I have a table named "products" in the database.
the minimum order values of the products are all saved in this table.
I have an admin panel to set the minimum order for that particular product. The quantity of the minimum order of each and every product was saved in the MySQL database. The minimum order value of that particular product is set by the admin according to the amount of availability of the product. There are no constraints for the admin to set the minimum order in the admin panel.
My question is how do I get the minimum order($row['moq']) quantity from the "product" table in the database so that the orders(cart_qty) from the users will be restricted to a certain amount that has been set from the Admin Panel using minimum attributes.
These are the codes that I used to require users to add the quantity that they wanted to order:
<div class="clearfix"></div>
<form action="store_items.php" method="post">
<div class="col-12 form-group">
<div class="row">
<div class="col-3">
<input type="number" name="cart_qty" step="0.01" value="1" class="form-control text-center" </div>
<div class="col-9">
<input class="btn btn-block addtocart" type="submit" value="Add to cart" name="addtocart" <?php if($row[ 'product_status']==0 ) echo 'disabled="disabled"'; ?> >
</div>
</div>
This is where I add the minimum order from the admin panel. moq is the entity of the minimum order in the product table.
<div class="col-md-2">
<div class="form-group">
<label>Minimum Order Quantity*</label>
<input type="number" step="0.01" class="form-control" placeholder="Enter minimum order quantity" name="moq" value="<?php echo $row['moq'];?>" required>
</div>
</div>
How do I get the $row['moq'] to restrict the number of orders in cart_qty. I know the method of using the minimum attributes but how do I get the values from of the moq in the product table to restrict the input value of cart_qty in the user page.
I'm not using any eCommerce platform (eg. WooCommerce) to build my website. Just a pure HTML, PHP, and JavaScript.
You can use the onChange event to restrict the value using Javascript.
<input type="number" onchange="restrictValue(this, 10, 30);">
Then in Javascript you do:
function restrictValue(inputElement, minimum, maximum)
{
let value = inputElement.value;
if (value < minimum) value = minimum;
if (value > maximum) value = maximum;
inputElement.value = value;
}
You could use the onKeyUp to constantly correct any value entered, but that could be quite disruptive and irritating.
I want to add a permanent +234 to the phone number value before it is submitted into the database
I have tried to type (+234).('Alternate_Number) on the form page to see if it will pass the array into the database but it didnt work
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="Alternate_PhoneNumber">Alternate PhoneNumber :</label>
<div class="col-sm-9">
<input type="tel" name="Alternate_PhoneNumber" placeholder=" (08032xxxxxx)" class="Alternate_PhoneNumber form-control" id="Alternate_PhoneNumber" value="<?php echo set_value('Alternate_PhoneNumber', ''); ?>" minlength="11" maxlength="11">
</div>
</div>
controller
$this->form_validation->set_rules('Alternate_PhoneNumber','Alternate PhoneNumber','|numeric|exact_length[11]|xss_clean');
main model
'Alternate_PhoneNumber' => $this->input->post('Alternate_PhoneNumber'),
i want to just have the submitted value in the database to be +234 and the value submitted by the user like +2340834144706
Would this work for your case?
'Alternate_PhoneNumber' => '+234' . $this->input->post('Alternate_PhoneNumber'),
Use concatenation before saving data into database.
1) Suppose the variable $PhoneNumber = '0834144706' is your actual number.
2) And one more prefix variable called $country_code = '+234';
3) Do concatenation like this (Full Code)
$PhoneNumber = '0834144706';
$country_code = '+234';
$final_number = $country_code.$PhoneNumber; // Has value +2340834144706
and save the $final_numbervariable data into database.
I need to save multiple checkbox to single field in the database.
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="1">Raíz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="3">tronco
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="4">corteza
</label>
</div>
Controller:
$ficha_tecnica = new Ficha_Tecnica();
$options = $request->get('expresion_vegetal_id');
$ficha_tecnica->expresion_vegetal_id = $options;
$ficha_tecnica->save();
this is trying to save, the values in [""], I need only save the numbers
insert into `fichas_tecnicas` (`expresion_vegetal_id`) values (["1","3","4"])
When I try to save, show the next message
1366 Incorrect integer value: '["1","4"]' for column 'expresion_vegetal_id'
You'r cannot add that because
You can try to loop the expresion_vegetal_id still in array format.
I see in your question is you need to add that in string format.
You must loop that array first and save one by one or you can used created
I give you the sample using loop.
You code will looks like this
$ficha_tecnica = new Ficha_Tecnica();
foreach ($$request->expresion_vegetal_id as $expresion_vegetal_id) {
$ficha_tecnica->fresh();
$ficha_tecnica->expresion_vegetal_id = $expresion_vegetal_id;
$ficha_tecnica->save();
}
i hope you can find the other same way....
I have a form that is a list of check boxes. Next to each check box is a text field that goes with the check box. Below is an image of what I am talking about
The text in red is the name attribute. My question is based off of whatever the user check marks, how do I match the paired text field without doing a bunch of if statements?
I know how to just post the check boxes and just post the text fields..separately. I guess I am stuck at how to pair the results. This is what I am using to $_POST the check boxes
if (isset($_POST['family_medical_history']))
{
$_SESSION['patient_form']['family_medical_history'] = array();
foreach ($_POST['family_medical_history'] as $key => $value)
{
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST['family_medical_history'][$key];
}
}
else
{
$_SESSION['patient_form']['family_medical_history'] = "none";
}
Example HTML form
<div class="form-group col-sm-4">
<label><input type="checkbox" name="family_medical_history[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes">
</div>
You can pair them by naming the inputs similarly for each checkbox/relationship pair to produce a two-member array for each item.
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]">
</div>
In the PHP, they will become pairs with the 0 element equal to the value of the checkbox input, and the 1 element equal to the text input. You can create an array of conditions and loop through each of the named inputs. You'll have to change your if statement to something else, for example the submit button:
$conditions = array("rel_blindness" => "Blindness", "rel_cataracts" => "Cataracts", "rel_crossed_eyes" => "Crossed Eyes", "rel_glaucoma" => "Glaucoma");
if (isset($_POST['submit'])){
foreach ($conditions as $key => $value){
if ($_POST[$key][0] == $value){//if the checkbox is checked
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST[$key][1];
}
}
}
I have a button on a page that when a user pushes it, it creates another "Contact" field on the page. The Contact field allows them to add a new contact to their profile. Also, they can click the button as many times as they want, and it will create that many "Contact" fields.
The problem though is that I am having a hard time figuring how many "Contact" fileds have been added. Here is some HTML that is generated when the button is clicked:
<div class="item">
<label for="in-1v">First Name <span>*</span></label>
<div class="text">
<input type="text" id="in-1-0" name="member[0][fname]" value="" />
</div>
</div>
<div class="item">
<label for="in-2-0">Last Name <span>*</span></label>
<div class="text">
<input type="text" id="in-2-0" name="member[0][lname]" value="" />
</div>
</div>
Each time the button is clicked, name="member[0][lname]" will become name="member[1][lname]" and will continue to increment each time the button is clicked. As stated earlier, the user can do this as many times as they want on the page.
I am using PHP to loop through the multidimensional array:
$array = $_POST['member'] ;
foreach($array as $array_element) {
$fname = $array_element['fname'];
$lname = $array_element['lname'];
}
How can I use PHP to determine how many fileds have been added so I can loop through them?
Any help is greatly appreciated!
You can simply get a count like so:
$count = count($_POST['member']);
You could also then modify your loop to look like this:
// First check to see if member is set and is a valid array
if (!empty($_POST['member']) && is_array($_POST['member'])) {
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member'][$i]['fname'];
$lname = $_POST['member'][$i]['lname'];
}
}