algy Posted September 4, 2008 Share Posted September 4, 2008 PHP Freaks, I'm a total php newbie and want to ask a question that might be too stupid or obvious. If it is, I apologize. I'm trying to write a very simple bit of code but I'm stuck on something that I expect has an easy answer. What I want to do is be able to call up two rows of data from a table and perform a simple math operation comparing each variable. My problem is that I have no clue how to differentiate between the two rows of variables I'm calling up. Example ------------------------------------------------- fruit_id | bud | fruit | color | ripe | ------------------------------------------------- apple | 4 | 2 | 4 | 6 | ------------------------------------------------- orange | 2 | 0 | 5 | 4 | ------------------------------------------------- peach | 3 | 6 | 5 | 4 | ------------------------------------------------- pear | 6 | 9 | 3 | 3 | ------------------------------------------------- I want to be able to pull up rows apple and orange and then, say, add the two seperate variables in each column and post them to an html table so that the output would be ------------------------------------------------------------- | bud | fruit | color | ripe | ------------------------------------------------------------- apple+orange | 6 | 2 | 9 | 10 | ------------------------------------------------------------- I understand how to call and display each row seperately, but have no clue how to get to apple/bud = 4 orange/bud = 2 apple/bud + orange/bud = 6 I guess I'm asking how to get to the point where apple/bud is a distinct named variable from orange/bud? I know I'm missing something fundamental, but that's why I'm asking you algy Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/ Share on other sites More sharing options...
ifubad Posted September 4, 2008 Share Posted September 4, 2008 when you read in the lines from the db, each line will be stored in an array, creating a multi dimensional array. Say if the array is just named $array, to retrieve the index that you are asking about in you post apple/bud = 4 orange/bud = 2 while you are looping thru the array, you access each line and the specified index like so foreach ($array as $key=>$value) { $returnVal = $array[$key][1]; } There are slightly different ways to doing it, this is just one simple way Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/#findComment-633446 Share on other sites More sharing options...
ifubad Posted September 4, 2008 Share Posted September 4, 2008 or if you want to check for those two specific indexes (apple, orange, etc.) before retrieving the data foreach ($array as $key=>$value) { if ($array[$key][0] == 'apple') { $returnVal = $array[$key][1]; } } Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/#findComment-633448 Share on other sites More sharing options...
burn1337 Posted September 4, 2008 Share Posted September 4, 2008 I would do this : $text = 'select * from Fruits where fruit_id=apple'; //same with orange or use a variable to make it user defined.. $get = mysql_query($text); //do for both of course $Apple = mysql_fetch_array($get); $Orange = mysql_fetch_array($get); //with orange as the id $Both = new array( 0 => '$Apple[0]. + .$Orange[0]', 1 => '$Apple[1] + $Orange[1]'); //repeat for each colum you want to add, or what ever math opperator you choose, you ccould even set each key of the arrays to signle varaiables (i.e. $A0 = $Apple=[0]) then use variables for your operations or use variables as the result of the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/#findComment-633462 Share on other sites More sharing options...
sasa Posted September 4, 2008 Share Posted September 4, 2008 try SELECT SUM( bud ) AS bud, SUM( fruit ) AS fruit, SUM( color ) AS color, SUM( ripe ) AS ripe FROM `fruits` WHERE fruit_id = 'apple' OR fruit_id = 'orange' Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/#findComment-633618 Share on other sites More sharing options...
algy Posted September 4, 2008 Author Share Posted September 4, 2008 Thank you everybody, so much! Not only did I get an answer fast, I got three different approaches to try. I am impressed and grateful. Quote Link to comment https://forums.phpfreaks.com/topic/122670-naming-like-variables/#findComment-633736 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.