Hi all...

I am trying to get data from a table field called "custom" from a mysql database

the data is stored as 23-1, where 23 is the id and 1 is the quantity

How do I build an array or extract the data and place the values into two variables to use to update a different table.

I am ok with connecting to and updating databases, im just not sure how to format the data from the above field.

i'v done some digging around and found the following
$namearray = Array ( [0] => Markus [1] => Nigel [2] => David )

Any help would be appreciated,


Use a preg_split to split the string from '-' character. preg_split returns an array and at the first index would be 'id' and the second index would hold the 'quantity'

Hi Kekkaishi many thanks for your reply :)

How do i go about coding a preg_split

If i connect to my database and get the field "custom" returned

$sql = mysql_query("SELECT custom FROM tbl_name WHERE id='$id'");
while($row = mysql_fetch_array($sql)){
$id = $row['id'];

How would I code the preg_split to include the data into variables

Any help would be appreciated

If your string is always going to be in the format of ID-QUANTITY explode() would be faster and removes the need to use a regular expression.

$value = '1234-57';
$result = explode('-', $value);

echo $result[0].PHP_EOL; //1234
echo $result[1].PHP_EOL; //57

Hi mschrodeder - thanks for your reply

How would i use your code ??

How could i set variables to the result ??



Hi mschrodeder - thanks for your reply

How would i use your code ??

How could i set variables to the result ??



Wrong way.


I'm not sure if I follow, but i'll take a shot at it.

Assuming $id from your query result is the ID-QUANTITY value, then you would pass $id as the second parameter of explode. The explode function would "explode" it apart at the dash (the first parameter) and you'd be left with an array, with two values.

$id = $result[0];
$quantity = $result[1];

@ MagicMedia & mschroeder

Many thanks for all your help, this is now working 99.9%
the only thing I would like to do is remove the ( , ) after quantity

the results are correct as below shows

I have tried using

$result[1] = eregi_replace(",", "", $result[1]);
// I have also tried using 
$rquanutity = eregi_replace(",", "", $rquanutity);

is it possible to remove the ( , ) from the quantity result

If the quantity ALWAYS has a , after it you can just take the chunk that is before the comma.

$item = '1,';
$item = substr($item, 0, -1);
echo $item; //1

You could also use str_replace to replace all occurrences of commas.

$item = '1,';
$item = str_replace(',', '', $item);
echo $item; //1

Many thanks to all who have helped me solve this problem ;)
Thread has been marked as solved