/    Sign up×
Community /Pin to ProfileBookmark

Help me with this before I go mad?

Here is my DB query loop. What I want to do is calculate the total delivery price for the order. The trouble is each product has a cut off point where the total price of that product once met will no longer add more but only for that product.

Forget the quantities they order at present, if they had 2 rows with the same productid and another product with another id. The first item can’t cross a certan threadhold for delivery. Once it does, it stays at that threshold price and then the postage of the next iem is added.

[code=php] require(‘inc_dbconnect.php’);
$currentquery = “select * from cart, items, products
where cart.Cart_ID = ‘”.$cartid.”‘
AND items.Cart_ID = cart.Cart_ID
AND items.Product_ID = products.Product_ID
Order by products.Product_ID asc”;

$result = mysql_query($currentquery);
$num_results = mysql_num_rows($result);

for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$dbitemprice = stripslashes($row[‘Item_Price’]);

$cur_productid = stripslashes($row[‘Product_ID’]);

$pricedel = stripslashes($row[‘Product_DeliveryPrice’]);
$cap = stripslashes($row[‘Product_DeliveryCap’]);

$post_total = $post_total + $pricedel;

}[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 05.2011 — What might help would be to take your query results and put them into an array of arrays, where the first array key would be the product ID. That way, you can do a foreach loop on the main array, knowing that on each iteration you'll then be working with a sub-array of elements all for the same product ID, allowing you do do whatever counting/totaling is needed within that one product before moving on to the next product ID.
[code=php]
$result = mysql_query($currentquery);
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[$row['Product_ID']][] = $row;
}
// then . . .
foreach($data as $prodID => $product) {
$data[$prodID]['total'] = 0;
foreach($product as $item) {
// do stuff, and update $data[$prodID]['total'] based on that stuff
// maybe use a counter variable to know if this is the first or subsequent item
// for this product ID
}
}
[/code]
Copy linkTweet thisAlerts:
@sanchez_1960authorAug 05.2011 — Here's what I got so far

[code=php] require('inc_dbconnect.php');
$currentquery = "select * from cart, items, products
where cart.Cart_ID = '".$cartid."'
AND items.Cart_ID = cart.Cart_ID
AND items.Product_ID = products.Product_ID
Order by products.Product_ID asc";

$result = mysql_query($currentquery);
$num_results = mysql_num_rows($result);

for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$dbitemprice = stripslashes($row['Item_Price']);
// $dbitemprice = $dbitemprice * stripslashes($row['Item_Quantity']);
$cartprice = $cartprice + $dbitemprice;
$cartprice = round($cartprice,2);
$sel_optionid = stripslashes($row['Option_ID']);

$cur_productid = stripslashes($row['Product_ID']);
if ($sel_optionid != '0') { $weight = find_option_weight($sel_optionid); } else {
$pricedel = stripslashes($row['Product_DeliveryPrice']);
$cap = stripslashes($row['Product_DeliveryCap']); }

echo $pricedel.'<br>';
$total_counter = $total_counter + $pricedel;





if ($last_productid == $cur_productid || !$last_productid) {
$sep_counter = $sep_counter + $pricedel;
if ($sep_counter >= 15.00) {
$amount_over = $amount_over + $price_del;
}
else {
}


}
else {
$sep_counter = 0;
}

$last_productid = stripslashes($row['Product_ID']);


$total_counter = $total_counter - $amount_over;



}

$total_pricedel = $total_counter;[/code]
Copy linkTweet thisAlerts:
@sanchez_1960authorAug 05.2011 — Anyone? Pulling my hair out!
Copy linkTweet thisAlerts:
@sanchez_1960authorAug 07.2011 — Never did resolve this ?
Copy linkTweet thisAlerts:
@criterion9Aug 08.2011 — Never did resolve this ?[/QUOTE]

Where are you stuck? I have to admit I didn't exactly read into the code you posted. Are you getting an error? Perhaps some unexpected results?
Copy linkTweet thisAlerts:
@sanchez_1960authorAug 08.2011 — Fixed this. Came back to it with a clear head after the weekend and gave it another go.

Used a $go = 'Yes' variable in the loop. When it reached 15.00, I'll set Go to "No". When the product changed, I changed Go back to yes.

Worked a treat. This sort of thing is simple to me but last week my brain was fried.
×

Success!

Help @sanchez_1960 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,
)...