manipulating retrieved data using arrays

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
cheguevara67
Forum Newbie
Posts: 1
Joined: Thu Mar 01, 2012 5:27 am

manipulating retrieved data using arrays

Post by cheguevara67 »

0 down vote favorite
share [g+] share [fb] share [tw]


i am querying the DB as follows:

$

Code: Select all

result=mysql_query("SELECT name,items FROM mytable WHERE price='$price'");
now,i want to create an array to insert the values that are as a result of this query e'g let's say this is the resultant data:

name quantity
john 10
joel 20
brian 10

how do I create an array to insert the above data using $name and $quantity as variables;
secondly,(this is a trading platform),so let's say a user wanted to buy 25 items from the data in the array,and so to achieve this the script has to take the 10 items from john and 15 from joel(that adds up to 25) and then set their items to the remaining value i.e john's items=0 and joel's items=5.how do I code this.please assist. i've tried to simplify this the best i can .thanks :(
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: manipulating retrieved data using arrays

Post by pbs »

Use mysql_fetch_array or mysql_fetch_assoc funtions then use foreach statement. In foreach statement create a new array with name field as array key and quantity as array value like

$outputarray['john'] = 10
$outputarray['joel'] = 20
$outputarray['brian'] = 10

may this will help u
Post Reply