gazalec Posted April 4, 2007 Share Posted April 4, 2007 Hi i was wondering i am using a while loop to extract selected data from a MySQL database with PHP but i was wondering if there was anyway to assign these values to their own separate variable names as they came off? i want it so that each value that is extracted is assigned to its own variable because i want to use them separately later Thanks for Any Help Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 4, 2007 Share Posted April 4, 2007 use an array... http://www.php.net/array Quote Link to comment Share on other sites More sharing options...
gazalec Posted April 4, 2007 Author Share Posted April 4, 2007 Sorry i'm gonna sound really dumb but it just seems to have done the exact same thing, could anyone help me out with the code??? Thanks for the help Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 4, 2007 Share Posted April 4, 2007 In an array you can address the elements individually..i.e. $array_name['element_name']. In the mysql object you can only address them in a linear fashion...i.e. through a while loop. while ($row = mysql_fetch_assoc($result)) { $data[$row['id'] = $row; } You can then address the items by their id... echo $data[1]['username']; Or if you use the username as the key... while ($row = mysql_fetch_assoc($result)) { $data[$row['username'] = $row; } echo $data['gazalec']['post_count']; Quote Link to comment Share on other sites More sharing options...
gazalec Posted April 4, 2007 Author Share Posted April 4, 2007 I dont think this is working for what i need, ok i'll explain the whole situation to see if it clears it up, on my database i have a table with cust_no, no_of_products, order_date, what i want is that you type in a cust_no it picks up all the orders they had i.e. order_date, products ordered on that date, then i want them to be separated into individual variables i.e., order_date1 = '2007-04-04' no_of_products1 = '700', order_date2 = '2007-03-29' no_of_products2 = '500' etc. for all their orders, is this even possible ??? Thanks so much for the help so far Quote Link to comment Share on other sites More sharing options...
Barand Posted April 4, 2007 Share Posted April 4, 2007 The only situation I can think where it would be preferable to to do it like that, rather than use an array, is if you are charging per line of code <?php foreach ($order_dates as $date) echo "$date<br>"; ?> Fee : £1.00 Contrast with <?php echo "$order_date_1<br>"; echo "$order_date_2<br>"; echo "$order_date_3<br>"; echo "$order_date_4<br>"; echo "$order_date_5<br>"; echo "$order_date_6<br>"; echo "$order_date_7<br>"; echo "$order_date_8<br>"; echo "$order_date_9<br>"; echo "$order_date_10<br>"; echo "$order_date_11<br>"; echo "$order_date_12<br>"; echo "$order_date_13<br>"; echo "$order_date_14<br>"; echo "$order_date_15<br>"; echo "$order_date_16<br>"; echo "$order_date_17<br>"; echo "$order_date_18<br>"; echo "$order_date_19<br>"; echo "$order_date_20<br>"; ?> Fee : £20.00 Quote Link to comment Share on other sites More sharing options...
gazalec Posted April 4, 2007 Author Share Posted April 4, 2007 well the thing is i need each order date and each no_of_products separately because i'm making a bar graph in php, so that a cell is giving id's and if it is in the 'order_date' column and is the value of the 'no_of_products' it makes its background black and if it isn't its white to do a kind of line graph thing, i'm experimenting. so i hope that clears things up Sorry bout the double post Quote Link to comment Share on other sites More sharing options...
dough boy Posted April 4, 2007 Share Posted April 4, 2007 So: while ($row = mysql_fetch_assoc($result)) { $dates=[] = $row['date']; $num=[] = $row['num']; } Quote Link to comment Share on other sites More sharing options...
gazalec Posted April 5, 2007 Author Share Posted April 5, 2007 i just get a parse error here and how would i call on these later ??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.