Jump to content

Parse String and Array Help


mclamais

Recommended Posts

So I have a query string I need to work with

 

?p=2222_1,8621_2,5524_1

or

p=[sku]_[quantity],[sku]_[quantity],[sku]_[quantity]

 

I need to extract the values so I can create a little mini-cart for display only

 

What's the best way do I split querty string by the comma, and underscore, and store it so work with it.

 

I'll use the sku's to do a query, (I don't need help here)  just he parsing and array stuff.

 

$query1  = "SELECT * FROM products WHERE sku IN (" .$skus. ")";

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/110121-parse-string-and-array-help/
Share on other sites

Use the expode() function:

<?php
$skus = array();
$quant = array();
$p = '2222_1,8621_2,5524_1';
foreach (explode(',',$p) as $tmp)
   list($skus[],$quant[]) = explode('_',$tmp);
$query1  = "SELECT * FROM products WHERE sku IN (" . implode(',',$skus) . ")";
?>

 

Ken

i would probably do this (was written quickly and on the fly, expect bugs,typos)

 

<?php
$item =  explode(",", $_GET['p']);
foreach($item as $i)
{
list($iSKU[], $iQty[]) =  explode("_", $i);
}

$SKUs = implode(",",$iSKU[$n]);

$query1  = "SELECT * FROM products WHERE sku IN (" $SKUs. ")";
?>

 

 

EDIT: LOL well thats sums it up

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.