extracting values from string/array
06-12-2008, 09:43 AM
|
extracting values from string/array
|
Posts: 5
Name: Brian Chesworth
|
Hi all, a novice here, seeking to learn, tied by deadline... the usual..!
The Situation:
A person buys a product, an order is placed, the buyer is returned from the cart to a "thankyou" page, the thankyou page is required to:
a) update a mysql db table (..._cartorders.orders) creating a log of all data
for the order (customer id, order id, etc)
b) update the stock level for each product purchased (by deducting the
purchased quantity from the existing stock level) in another table on
another db (..._eshop.products)
c) assesses whether, as a result of the purchase, the product is:
i) (stock>0) 'in stock' - the website shows "instock"
ii) (stock<=0) 'out of stock' -the website shows "available to order"
- if the resultant stock level is <=1 then an email is sent to the
administrator identifying the product and its calculated stock level,
informing them that its time to replenish.
The problem:
Due to shopping cart design, the product information recieved by the 'thankyou' page is contained within a string in the format:
$cart=(description(D):quantity(Q):unitprice(UP):sh ipping(S)  roductcode(P))
the more products added to the cart from the site, the longer the string gets, each seperate product added is seperated by '~', hence, for MULTIPLE products, the string would appear like:
$cart=(D:Q:UP:S:P~D:Q:UP:S:P~D:Q:UP:S:P~D:Q:UP:S:P ~D:Q:UP:S:P)
in order to fullfill the given situation above, it is necessary to extract the individual values from $cart so that:
(P) can be used to identify each unique product
(Q) can be used to set a new stock level for each unique product
(D) can be used to furnish the 'replenish stock email' if required
-(UP) & (S) are not required.
Tried Solutions:
1) I have successfully created a log of each order in ..._cartorders.orders.
2) I have successfully updated ..._eshop.products with a new stock level for
a unique product using (P) for the product_id and 'stocklevel-(Q)' for a
SINGLE $cart order (by creating an array and extracting (array => P) and
(array => Q) and updating the correct db table accordingly.
3) by querying the updated db entry, I can establish cases for stock>0,
stock<=0 and send email if stock <=1, thus solving the Situation Criteria.
----------------BUT ONLY FOR A SINGLE PURCHASE..!-----------------
The moment a second product is added to the order, the solution fails..!
The Code (excluding cases & html):
PHP Code:
<?php require_once 'config.php'; $dt=(str_replace("~","<br />",$cart)); mysql_query("REPLACE INTO orders (username,id,ip,date,method,cart,discount,subtotal,shipping,tax,total,shipping_zone,inv_name,inv_company,inv_addr1,inv_addr2,inv_state,inv_zip,inv_country,del_name,del_addr1,del_addr2,del_state,del_zip,del_country,tel,fax,email,message,sd) VALUES ('$username',$id,'$ip','$date','$method','$dt','$discount','$subtotal','$shipping','$tax','$total',$shipping_zone,'$inv_name','$inv_company','$inv_addr1','$inv_addr2','$inv_state','$inv_zip','$inv_country','$del_name','$del_addr1','$del_addr2','$del_state','$del_zip','$del_country','$tel','$fax','$email','$message','$sd')"); $_POST['cart']; { $ct=(str_replace("~",":",$cart)); $details=(explode(":",$ct)); $det[description]=$details[0]; $qty[qty]=$details[1]; $price[price]=$details[2]; $ship[ship]=$details[3]; $scode[scode]=$details[4]; $sql = mysql_query ("update memorydaddy_co_uk_eshop.tbl_product set stock = stock - $details[1] where pd_id = '$details[4]'")or die (mysql_error()); } ?>
-Probably not the neatest of methods, however....
Any thoughts out there...?
|
|
|
|
06-12-2008, 10:32 AM
|
Re: extracting values from string/array
|
Posts: 265
Name: Lucas
|
Instead of replacing all the ~'s with :'s,
You should explode the string based on ~, then cycle through each element in the array, exploding THAT element based on : to form a new array
Make sense?
|
|
|
|
06-12-2008, 10:45 AM
|
Re: extracting values from string/array
|
Posts: 5
Name: Brian Chesworth
|
Cheers, nyef, that makes perfect sense 
|
|
|
|
06-12-2008, 04:29 PM
|
Re: extracting values from string/array
|
Posts: 5
Name: Brian Chesworth
|
Hi again, I'm back..!
basically same/similar problem, I've altered the script, (the if/else echos are just to test), I've tried to change the 'str_replace' and 'explode' variables in an attempt to cycle through the script for each product in the order, however the second array doesn't number itself as the first. Obviously something simple I'm overlooking, however I'm going blind trying to see it.
The script that works for a single product in the order:
PHP Code:
<?php //connect to db & select table, etc require_once 'config.php'; //create log of all data posted for each order //including date formating $date=gmdate(DATE_RFC822); $dt=(str_replace("~","<br />",$cart)); mysql_query("REPLACE INTO orders (username,id,ip,date,method,cart,discount,subtotal,shipping,tax,total,shipping_zone,inv_name,inv_company,inv_addr1,inv_addr2,inv_state,inv_zip,inv_country,del_name,del_addr1,del_addr2,del_state,del_zip,del_country,tel,fax,email,message,sd) VALUES ('$username',$id,'$ip','$date','$method','$dt','$discount','$subtotal','$shipping','$tax','$total',$shipping_zone,'$inv_name','$inv_company','$inv_addr1','$inv_addr2','$inv_state','$inv_zip','$inv_country','$del_name','$del_addr1','$del_addr2','$del_state','$del_zip','$del_country','$tel','$fax','$email','$message','$sd')"); //start the section dealing with stock management $_POST['cart']; { $ct=(str_replace("~",":",$cart)); $details=(explode(":",$ct)); $det[description]=$details[0]; $qty[qty]=$details[1]; $price[price]=$details[2]; $ship[ship]=$details[3]; $scode[scode]=$details[4]; //updating the stock levels; mysql_query ("update memorydaddy_co_uk_eshop.tbl_product set stock = stock - $details[1] where pd_id = '$details[4]'")or die (mysql_error()); } //setting the availability notice according to stocks remaining; $sql = mysql_query("SELECT stock FROM memorydaddy_co_uk_eshop.tbl_product WHERE pd_id = '$details[4]'") or die (mysql_error()); while(list($stock)=mysql_fetch_array($sql)) if ($stock <= 0) { mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'out of stock' where pd_id = '$details[4]'"); echo "none left : $stock"; } //sending an email when stocks are too low; elseif ($stock <= 5) { mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'orders only' where pd_id = '$details[4]'"); $MFrom = "Stock Admin"; $MTo = "brianchesworth@gmail.com"; $MailSubject = "PRODUCT LOW IN STOCK"; mail($MTo,$MailSubject," The Product: $details[0] with ID: $details[4] THERE IS/ARE ONLY - $stock - OF THESE ITEMS LEFT IN STOCK FAILURE TO MANUALLY UPDATE THE DATABASE STOCK LEVELS FOR THIS ITEM WILL RESULT IN IT BEING UNAVAILABLE FOR PURCHASE ","From: " . $MFrom); echo "running out : $stock"; } else{ mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'available' where pd_id = '$details[4]'"); echo "plenty left : $stock"; } ?>
everything works fine for the first product, ie array[0], array[1], array[2], array[3], array[4], however any additional products are ignored, or if i explode the '~' and remove the str_replace() then I get mysql_errors.
The snippet of the script dealing with the array:
PHP Code:
$_POST['cart']; { $ct=(str_replace("~",":",$cart)); $details=(explode(":",$ct)); $det[description]=$details[0]; $qty[qty]=$details[1]; $price[price]=$details[2]; $ship[ship]=$details[3]; $scode[scode]=$details[4];
Any Ideas out there..?
|
|
|
|
06-12-2008, 04:39 PM
|
Re: extracting values from string/array
|
Posts: 265
Name: Lucas
|
This might help:
Code:
$ct=(explode("~",$cart));
for ($counter=0;$counter<count($ct);$counter+=1){
$details=(explode(":",$ct[$counter]));
$det[$counter]=$details[0];
$qty[$counter]=$details[1];
...
}
Last edited by nyef; 06-12-2008 at 04:40 PM..
|
|
|
|
06-12-2008, 05:32 PM
|
Re: extracting values from string/array
|
Posts: 5
Name: Brian Chesworth
|
Thanks for the response nyef,
I just tried your suggestion, but didn't produce any results, the test browser went blank.
I've just reset back to the script I posted last, with the following differences:
PHP Code:
$ct=(str_replace("~",":",$cart)); $details=(explode(":",$ct)); print_r($details); echo "<br />"; echo "<br />"; echo "$details <br />"; echo "<br /><br />"; echo "$cart <br />";
I added the 'print_r($details)', 'echo "$details"' and 'echo "$cart"' to view the results that are presented, they being:-
'print_r $details' gives:
Array ( [0] => Sandisk 2GB SD Card [1] => 1 [2] => 9.21 [3] => 0 [4] => 3 [5] => Sandisk 1GB SD for Wii [6] => 1 [7] => 6.39 [8] => 0 [9] => 63 )
'echo $details' gives:
Array
'echo $cart' gives:
Sandisk 2GB SD Card : 1 : 9.21 : 0 : 3~Sandisk 1GB SD for Wii : 1 : 6.39 : 0 : 63
Any more thoughts?
|
|
|
|
06-12-2008, 07:06 PM
|
Re: extracting values from string/array
|
Posts: 5
Name: Brian Chesworth
|
phew.!! finally cracked it..!!
the final fully functioning code:
PHP Code:
<?php require_once 'config.php'; //create a log of every variable sent from the cart for each order //re-format the date $date=gmdate(DATE_RFC822); $dt=(str_replace("~","<br />",$cart)); mysql_query("REPLACE INTO orders (username,id,ip,date,method,cart,discount,subtotal,shipping,tax,total,shipping_zone,inv_name,inv_company,inv_addr1,inv_addr2,inv_state,inv_zip,inv_country,del_name,del_addr1,del_addr2,del_state,del_zip,del_country,tel,fax,email,message,sd) VALUES ('$username',$id,'$ip','$date','$method','$dt','$discount','$subtotal','$shipping','$tax','$total',$shipping_zone,'$inv_name','$inv_company','$inv_addr1','$inv_addr2','$inv_state','$inv_zip','$inv_country','$del_name','$del_addr1','$del_addr2','$del_state','$del_zip','$del_country','$tel','$fax','$email','$message','$sd')"); //define the unique products from within the $cart string //and list them as unique arrays //explode the new product arrays to extract the sub elements $_POST['cart']; $ctr=(explode("~",$cart)); $i = 0; while ($i < count($ctr)){ $details=(explode(":",$ctr[$i])); //utilise the sub elements to update stock levels mysql_query ("update memorydaddy_co_uk_eshop.tbl_product set stock = stock - $details[1] where pd_id = '$details[4]'")or die (mysql_error()); //set product availability on site (available or out of stock) $sql = mysql_query("SELECT stock FROM memorydaddy_co_uk_eshop.tbl_product WHERE pd_id = '$details[4]'") or die (mysql_error()); while(list($stock)=mysql_fetch_array($sql)) if ($stock <= 0) { mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'out of stock' where pd_id = '$details[4]'"); } //send an email if stock level is low elseif ($stock <= 5) { mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'orders only' where pd_id = '$details[4]'"); $MFrom = "Stock Admin"; $MTo = "brianchesworth@gmail.com"; $MailSubject = "PRODUCT LOW IN STOCK"; mail($MTo,$MailSubject," The Product: $details[0] with ID: $details[4] THERE IS/ARE ONLY - $stock - OF THESE ITEMS LEFT IN STOCK FAILURE TO MANUALLY UPDATE THE DATABASE STOCK LEVELS FOR THIS ITEM WILL RESULT IN IT BEING UNAVAILABLE FOR PURCHASE ","From: " . $MFrom); } else{ mysql_query("update memorydaddy_co_uk_eshop.tbl_product set instock = 'available' where pd_id = '$details[4]'"); } //and finally, repeat the process for the next product array $i++;} ?>
Thanks for your help, nyef
regards,
Brian
www.memorydaddy.co.uk
|
|
|
|
06-12-2008, 07:36 PM
|
Re: extracting values from string/array
|
Posts: 265
Name: Lucas
|
You're welcome
|
|
|
|
|
« Reply to extracting values from string/array
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|