bobleny Posted April 9, 2007 Share Posted April 9, 2007 I need help keeping track of some variables. I will basically have the variables "1", "2", "3", "4" for hundreds of things.... For example, a "cork" and a "mouse". The cork and mouse will be the things with the variables. Each "thing" will have its own variables. The variable's names will be the same though! This means I need to separate them from each other some how... This is what they will end up like: cork $1 = 400 $2 = 800 $3 = 405 $4 = 700 mouse $1 = 684 $2 = 304 $3 = 709 $4 = 913 I can't leave it like that because if I did, it would rewrite the variables. That, and I wouldn't be able to call cork's variables separate from mouse's variables. So I need to separate them.... How should I do this? I don't know if it should be an object or an array or what? Before you answer though, I need to say a few more things. First, there will be hundreds of these "things". There will be a cork, a mouse, a screw, a bottle, a clock, a rabbit, a box, etc... A really long list! Because of this, I will most likely set up a database so I can simply fill out a form to enter it's name and values. If this isn't clear, please say so, I've found this difficult to explain, and therefor might not be clear. Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/ Share on other sites More sharing options...
esukf Posted April 9, 2007 Share Posted April 9, 2007 For a start variables cannot begin with a number. It has to begin with a letter or an underscore. Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225165 Share on other sites More sharing options...
maxic0 Posted April 9, 2007 Share Posted April 9, 2007 To add multiple things to a variable, you do this.. <?php $name = "bob "; $name .= "marley"; echo $name; ?> This will echo "bob marley". Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225169 Share on other sites More sharing options...
sasa Posted April 9, 2007 Share Posted April 9, 2007 use array <?php $data =array( array('name' => 'corh', 1 => 400, 2 => 800, 3 => 405, 4 => 700), array('name' => 'mouse', 1 => 684, ;2 => 304, 3 => 709, 4 => 913) ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225171 Share on other sites More sharing options...
bobleny Posted April 9, 2007 Author Share Posted April 9, 2007 sasa, what is that? I don't understand.... I can't find anything about arrays like that! Everything on arrays is like this: $thingy_array[0] = cork; $thingy_array[1] = mouse; That, of course, is absolutely useless to me! But what you have written looks interesting. Could you explain it to me? I should also point out that I am not simply displaying variables, I am working them around in calculations. It will probably be like this... Some one will choose an object from a list, such as a "cork". They will then choose an option such as option "2". They will then enter a numbers such as "50,251". They will click submit. The php script will then have to find "cork" from our list below. cork $1 = 400 $2 = 800 $3 = 405 $4 = 700 mouse $1 = 684 $2 = 304 $3 = 709 $4 = 913 The script will then have to look for option 2, "800". The script will then have to take the number they entered, "50251". The script will then have to divide that number by option 2, "50251/800 = 62.81". The script will then have to round that number up to the nearest whole number, "63". The script will then have to display that number to the users, "echo $number;". I hope that helps you better understand what it is I am trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225233 Share on other sites More sharing options...
sasa Posted April 9, 2007 Share Posted April 9, 2007 try <?php $data = array('cork' => array(400,800,405,700), 'mouse' => array(684,304,709,913)); $object = 'cork'; $option = 2; $number = 50251; $out = ceil($number/$data[$object][$option-1]); echo $out; ?> Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225242 Share on other sites More sharing options...
bobleny Posted April 9, 2007 Author Share Posted April 9, 2007 Ooo, thats cool! It works, but how!? I don't understand this part "$data[$object][$option-1]". I get the $option -1 thing, because array starts at 0. What I don't get is how it knows which number to choose? Could you explain to me how this works, or point me to a tutorial. I like to know how things work before I use them... Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/46283-i-need-help-keeping-track-of-some-variables/#findComment-225249 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.