Jump to content
Sign in to follow this  
paulonline2501

How to interpret cutom field

Recommended Posts

hi, I've got a variable being passed to me from a third party website. in the following format "45-1," where 45 would be the item id and 1 would be the quantity. the variable could hold a few key pairs. eg: "45-1,46-3,5-3,"the first number is always the item id and the 2nd number is always the quantity. each pair separated by a comma. there is always a trailing comma. I need to deconstruct these pairs and deduct the quantity from the correct item in the products table in my database. My question is, what is the best way to deconstruct these pairs?im guessing I should put these in to an array.not totally sure how to do this. any help or ideas would be great. the variable is initially set like this:

$custom = $_POST['custom'];

Share this post


Link to post
Share on other sites

Explode the string first on commas, and loop through each element in the array and explode each element on the hyphen if it is not empty (the last element will be empty because of the trailing comma).

Share this post


Link to post
Share on other sites

...thanks justsomeguy. yeah, I found something on exploding and am trying it now. I've got the first explode working, and even managed to trim the trailing comma to stop it from being put in the array.

Edited by as_bold_as_love

Share this post


Link to post
Share on other sites
<p><?php$myString = "45-2,34-2,35-1,";echo($myString);?></p><?php$myString = trim($myString, ",");$myArray = explode(",", $myString);echo("<p>Exploded string in array:</p><ul>");foreach($myArray as $thisValue){   echo("<li>".$thisValue."</li>");}echo("</ul>");?>

I got the first explode working but struggling with the 2nd explode.

Edited by as_bold_as_love

Share this post


Link to post
Share on other sites

....actually, I think ive got it working but i'm sure there is a way to write it in a more elegant way.here's what I wrote:

<?php	$myString = trim($myString, ",");	$myArray = explode(",", $myString);	echo("<p>Exploded string in array:</p><ul>");	foreach($myArray as $thisValue)	{		$i=0;		$thisPartsValueID="";		$thisPartsValueQuantity="";		$myArrayParts = explode("-", $thisValue);		foreach($myArrayParts as $thisPartsValue)		{			if($i==0){echo("<li>id=".$thisPartsValue."</li>");$thisPartsValueID=$thisPartsValue;$i++;}			else{echo("<li>quantity=".$thisPartsValue."</li>");$thisPartsValueQuantity=$thisPartsValue;}		}		echo("insert into tableName (quantity) values (currentQuantity - $thisPartsValueQuantity) where id = $thisPartsValueID");	}	echo("</ul>");?>
Edited by as_bold_as_love

Share this post


Link to post
Share on other sites

Either of these would work:

list($id, $quantity) = explode("-", $thisValue);echo 'id = ' . $id . '<br>quantity = ' . $quantity;
$myArrayParts = explode("-", $thisValue);$id = $myArrayParts[0];$quantity = $myArrayParts[1];

The only thing I'll use foreach for is to loop through an associative array really. If you know how many items are in the array might as well just assign them directly.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...