Solved

PHP need help splitting a querystring so I can manipulate the bits...

Posted on 2011-02-21
9
237 Views
Last Modified: 2012-05-11
This is a follow on to a question I asked this afternoon.  I need to parse a query string, excise certain bits and manipulate others in order to do some calculations.  As written the shopping cart (JCart), creates a querystring and sends that information to PayPal for them to process.  I need to be able to parse the querystring, find the price of one of the products, subtract that from the total, calculate tax on the remainder and add that to the querystring going to PayPal.

I can create my own string to the purposes of calculation so, for no particular reason, I created the attached.  Product names are first, quantity, second and item cost is third.  From this I need to do the following:

1. Determine the sum of all amounts that are not associated with the 'Trading Post' entry - probably easiest to multiply the prices by quantity, sum that and figure out the tax on that.

If anyone has an easy way or elegant way to do all that I'd sure appreciate some help with it. I've been slogging through it but my way is very plodding - embarassingly so.  I just don't know another way at this point - explode the string, get a count, find out how many items there are in the array by getting the numbers between the '_" and "=",
getting the position of the Trading Post item, etc, etc.

A few things to mention:
1. The string could be as small as 1 item or as large as 20.
2. The Trading Post item could be anywhere in the string - it's dependent on when it was added to the shopping cart.
3. Not all carts and thus not all strings will have a Trading Post item.

As always, thank so much in advance.
&item1_1=Camp+Dog+Tags&item2_1=4&item3_1=1&item1_2=Silly+Bands&item2_2=5&item3_2=1&item1_3=Trading+Post&item2_3=1.00&item3_3=55

Open in new window

0
Comment
Question by:saabStory
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34948985
parseurl shall help you to split up the querystring ...
as from there, you need to loop the returned array, and act based on the key + value parts...
0
 

Author Comment

by:saabStory
ID: 34949128
I've been looking at parse_url but I don't have a full url - only the segment attached.  When I try it, it looks as though it's assigning everything to a single variable.  

I can get somewhat close by exploding the fragment  with explode("&item",$querystring).  That gives me something like

1_1=Camp+Dog+Tags
1_2=4
1_3=1
2_1=Silly+Bands
2_2=5
2_3=1
3_1=Trading+Post
3_2=1
3_3=55

So from here, somehow I need to multiply the 2nd elements by the 3rd elements in each array and figure tax rate on that.  I'm stuck getting to the individual parts to work on them.  Once I can get it on this test data, I'll need to be sure it will work with any number of items in the querysting...

Any ideas would be greatly appreciated.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34949301
Can you post exploding code ? We can work on that.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 34949314
you will know the array's size, you can can loop, something like this:
$items = explode("&item", $querystring);
$array_size=count($items);

// loop the array:
$i = 0;
$total = 0;
while ($i +2 < $array_size)
{
   $item_price = $items[$i+1];
   $item_qty = $items[$i+2];
   $total += $item_price * $item_qty;

   $i += 3;
}
echo $total;

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:kivan24
ID: 34949329
Try this:
$str = '&item1_1=Camp+Dog+Tags&item2_1=4&item3_1=1&item1_2=Silly+Bands&item2_2=5&item3_2=1&item1_3=Trading+Post&item2_3=1.00&item3_3=55';
$arr = explode('&item', $str);
$elems = array();
foreach ($arr as $elem) {
    if (!empty($elem)) {
        list($key, $val) = explode('=', $elem);
        print $key ." = ". $val."<br>";
    }
}

Open in new window

0
 
LVL 8

Assisted Solution

by:kivan24
kivan24 earned 250 total points
ID: 34949374
$str = '&item1_1=Camp+Dog+Tags&item2_1=4&item3_1=1&item1_2=Silly+Bands&item2_2=5&item3_2=1&item1_3=Trading+Post&item2_3=1.00&item3_3=55';
$arr = explode('&item', $str);
$elems = array();
foreach ($arr as $elem) {
    if (!empty($elem)) {
        list($key, $val) = explode('=', $elem);
        $elems[substr($key, -1)][$key] = $val;
    }
}

foreach ($elems as $key => $elem) {
    print $elem["1_".$key]." = ".($elem["2_".$key]*$elem["3_".$key]);
    print "<br>";
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34950944
I'd like to gently suggest that you step back from this problem and immediately hire a professional developer to help you.  From this and the other question about collecting tax in online commerce, I get the sense that you do not have a depth of knowledge in computer science and application design.  This is a really dangerous place to try to sharpen your skills.  There are criminal penalties for failing to collect and report taxes accurately - you do not want to get yourself or your client involved in the legal system.

Your questions seem to be about the trees.  We can show you how to disassemble strings and things like that, but what you really need is an e-commerce professional who can help you understand the dangers of the forest, as well as the nuances of the trees.

A string like this should probably be handled in an array of objects.
$str = '&item1_1=Camp+Dog+Tags&item2_1=4&item3_1=1&item1_2=Silly+Bands&item2_2=5&item3_2=1&item1_3=Trading+Post&item2_3=1.00&item3_3=55';

Each object would have a complete and atomic e-commerce definition, including a name, a price, an indicator of whether it is taxable, an indicator of whether shipping and handling must be charged, etc.  These objects can be serialized for storage.  They can be transmitted via JSON or XML.  There are professional design patterns that are used for this sort of thing.  You would benefit greatly from using those patterns.
0
 

Author Comment

by:saabStory
ID: 34985932
Ray, I just though you might be surprised that I wound up resolving my tax issue exactly as you suggested.  When I was first presented with the problem the only output readily accessible was the querystring and, in tracking that back I was able to access the arrays that were used to create the querystring and take what I needed from the source - so I wouldn't have to reverse engineer the final product. While my non-taxable object was not explicitly marked as such, it was discretely identified and therefore easy to pick out and do the necessary calculations and send the whole thing to a database.  So problem solved and everything works fine. Not bad for someone without a computer science or application design background.

My specific concern pertained to accessing the elements of the querystring, not in the legal ramifications of running afoul of the tax codes.  Believe me if this was a big deal, we would be addressing this another way but since this is an excruciatingly small application that will probably be a one-off, see no more than 50-60 uses over the next 3 months, handle a total revenue of less that $3000,and were advised by (amongst others) one of those 'professional developers with e-commerce experience' that what we were doing was perfectly adequate for the our needs, I failed to see the reason to make a case about it.  Oh and we're also moving to a different e-commerce solution other than PayPal within 6 months.

That's too much information to be added to one of these questions and, since I felt we had already covered all those bases ourselves, didn't feel it necessary to share all the gory details or a mountain of caveats for the purposes of asking a question how to detangle a querystring.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 34988383
@saabStory

>>...I failed to see the reason to make a case about it.
If you were a boat officer at the helm of a passenger liner and one of your passengers approached you and said "I'm a commercial captain with 30 years at the helm in these waters.  I think you ought to know about danger XYZ that might not be on your maps."  Would your reaction be
* "Big deal"
* "Thank you for your concern.  Can you share you knowledge with my navigator?"
* "I'm in charge of this ship.  Who do you think you are, anyway?!?"

In this scenario, it is one of your passengers that would be affected by any ship-board problems you encountered.

In the EE environment, the experts only see parts of the entire problem.  Our minds fill-in the details and apply content in a most-likely manner, based on our experiences.  One advantage of asking questions on EE is that you gain access to a host of experts with LOTS of experience that might see things from a different perspective and may spot dangers that you can't see.

I'm glad this problem is solved and that you will be moving to a different payment system, expecting that Ray's tax concerns will be handled automatically.  Had I been commenting while the question was open, I would have asked you how you were protecting yourself against SQL injection attacks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now