Jump to content

I need help keeping track of some variables....


bobleny

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.