/    Sign up×
Community /Pin to ProfileBookmark

Dynamically change a PHP Variable based on the Number in a Quantity field

Hi everyone,

I am trying to create a basic quote system that pulls products from my Database and allows a user to select multiple products via a Checkbox and then receive a instant PDF quote once they have selected the products and submitted the form.

So far, the form works brilliantly. However, other employees of the business I work for requested that I add a Quantity box next to each product so they can input the number they require which will then update the price to however many they request. For example, Product A is £20, if they type 4 in the Quantity box, then Product A becomes £80… Here is my form so far.

[code=php]
<?php
$servername = ‘REMOVED’;
$username = ‘REMOVED’;
$password = ‘REMOVED’;
$dbname = ‘REMOVED’;

try {
$conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare(“SELECT id_product, price, unity FROM ps_product”);
$stmt->execute();
$product = $stmt->fetchAll();

echo ‘<div class=”table-responsive”>’;
echo ‘<table class=”table table-striped”>’;
echo ‘<th>Checkbox</th><th>Product Reference</th><th>Product Name</th><th>Price</th><th>Unit</th><th>Quantity</th>’;
foreach ($product as $rowProd) {
$stmt = $conn->prepare(“SELECT reference, price FROM ps_product_attribute WHERE id_product = “. $rowProd[“id_product”] .””);
$stmt->execute();
$attribute = $stmt->fetchAll();

$stmt = $conn->prepare(“SELECT name FROM ps_product_lang WHERE id_product = “. $rowProd[“id_product”] .””);
$stmt->execute();
$name = $stmt->fetchAll();

foreach ($name as $rowName) {
$productName = $rowName[‘name’];
}

foreach ($attribute as $rowAttr) {
if($rowAttr[“price”] == 0 OR $rowAttr[“price”] < $rowProd[“price”]) {
$rowAttr[“price”] = $rowAttr[“price”] + $rowProd[“price”];
}

if($rowProd[“unity”] == false) {
$rowProd[“unity”] = 1;
}

$reference = ‘Reference’;
$price = ‘Price’;
$unit = ‘Unit’;

print ‘<tr>’;
print ‘<td><input type=”checkbox” name=”product[]” value=”‘. $rowAttr[“reference”] .’;’. $productName .’;’. number_format($rowAttr[“price”], 2) .’;’. $rowProd[“unity”] .'”></td>’;
print ‘<td>’. $rowAttr[“reference”] .'</td>’;
print ‘<td>’. $productName .'</td>’;
print ‘<td>&pound;’. number_format($rowAttr[“price”], 2) .'</td>’;
print ‘<td>’. $rowProd[“unity”] .'</td>’;
print ‘<td><input type=”number” name=”quantity” min=”1″ style=”padding: 0px 5px; max-width: 75px;” required /></td>’;
print ‘</tr>’;
}
}
echo ‘</table>’;
echo ‘</div>’;
}

catch(PDOException $e) {
echo “Error: ” . $e->getMessage();
}

$conn = null;
?>
[/code]

I got as far as adding: <td><input type=”number” name=”quantity” min=”1″ style=”padding: 0px 5px; max-width: 75px;” required /></td> and then was left confused at what I would do next.

Any help would be greatly appreciated.

Thanks.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 19.2017 — I think you'll want to use array notation for the quantity input element's name attribute, so that you can pair it up with the item in question. In fact, I'd recommend using the item id as the array index, instead of using empty square brackets, just to make sure, something like...
[code=php]
print '<td><input . . . name="quantity['.$rowName['id_product'].']" . . . /></td>';
[/code]
×

Success!

Help @Ashbridge spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...