Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/122670-naming-like-variables/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.