...

View Full Version : Naming variables with a variable? - Solved



PaulC2K
07-01-2005, 08:02 PM
Is it possible to create a variable with something already in another variable?

say $a = 10
how would i created $variable10 , using $a to specify that value?

ive tried things like

$a = '4';
$b(a$) = 'working'
echo $b4;

ideally that middle row would create the variable $b4, with $a being '4', however im stuck as to how i need to write this.

Any help would be appreciated,
PaulC2K

Kid Charming
07-01-2005, 09:00 PM
You want what's called a variable variable. Try:



$a = 10;
${'foo_' . $a} = 'bar';
echo $foo_10;

PaulC2K
07-02-2005, 12:05 AM
hmm, then what im eventually trying to build is a variable variable variable? ;)

I tried alsorts of combos with the ' ', " ", [], (), and .'s but nothing seemed to work, must admit i dont know the difference of the brackets, but i'll try anything to get something to work.

Thanks for that KidCharming.

SeeIT Solutions
07-02-2005, 02:55 AM
you could also use an array and change the index name.

like this


$variables['input1'] = "bla";
$variables['input2'] = "foo";
$variables['input3'] = "bar";

PaulC2K
07-02-2005, 03:07 AM
not sure how that would help me use the value of a variable to create the name of a new variable.


Im trying to get my head around a league standings table, i think the route ive taken isnt the easiest, because now ive got my newly named variables being created, im now stuck as to how i can decide which persons name comes top.
At the moment, a variable holds the value of a particular persons points for that round
$joe1 would be user 'joe' at round 1
however ive managed to get it to total all the rounds points per driver, but then exactly how do i rank them in order for them to be at the top of the table, rather than in the order they appeared.

I now need to figure a way of finding which variable has the highest value out of say 10 variables.
I thought i could use arrays and the sort function, but i dont know if i can get that to work how i'd like it to.
Prolly worth a new post i suppose

Kid Charming
07-02-2005, 05:02 AM
For what it sounds like you want, I think arrays, like SeeIT mentioned, would suit you much better than variable variables -- you generally sacrifice too much clarity of code using var vars to make it worth it, except in pretty specific situations. Arrays are also more efficient that having loads of variables floating around.

For example, I would give $joe his own array instead a series of variables. It would be pretty simple to put those totals into another array that you can then sort as you please.

Better still, if you have it available, I'd store all this in a database and let your queries do the work.

PaulC2K
07-02-2005, 07:12 AM
thats actually what im doing at the moment, but even this is too random to be ideal for what im doing.

See, i have too many variables i want access too, and the table would have multiple rows as each person would have there own row.


Basically im creating a league table for a racing series, each series is made up of a pre-determined number of rounds and races per round.
So one series could have 2 rounds, and 2 races each round, so 4 races in total over the one series.

The 4 races have a unique ID to them
id 68 = round 1 - race 1
id 71 = round 2 - race 2

I figured all i had to do is add the points contained in each drivers value of id 68+69+70+71 and i have my total, however a total isnt much use unless i can then sort all the totals so the highest points total goes top etc.

Thats where ive got really messed up, i could array it and sort it, but then i dont think i'd know how to do that for each user, as i figure the sort function can only be between values in the one array, once jumbled around, how would you know what each related to??
ie
joe = 1, dave = 3, ste = 2
sort that to 1/2/3 and how do you link the sorted numbers back to there respective 'owners'?

So out comes the SQL database, but then you cant create (or at least i couldnt) a table with extra fields in dependant on how many you need, we could have a 1 race series, or a 40 race series, i have 40 versions of a create table, taking into account every possibility just incase we decided to have a 33 race series one day!


Its just too messy for my basic knowledge of it all atm, ive managed shopping carts and admin areas for sites, but i cant get my head around this one at the moment, theres too many stumbling blocks and im spending more time guessing code than writing it.

marek_mar
07-02-2005, 07:19 AM
<?php
$my_var_anme = 'hello';
$$my_var_name = 'World';
print $hello; // prints World.

$my_var_anme = 'foo';
$GLOBALS[$my_var_anme] = 'bar';
print $foo; // Prints bar.
?>

SeeIT Solutions
07-02-2005, 07:39 AM
I think you need to sort out a better way to organise it, possibly by having a table each for series, rounds, races and they all link together, then have results linking to the races.

also, array_multisort() (http://au3.php.net/manual/en/function.array-multisort.php) sorts arrays and keeps the indexes the same.

basically you need to sit down and write out everything you need in the database and then organise it into separate categories and put it into tables, as few as possible but not all together.

The better the database design, the easier the coding will be.

PaulC2K
07-02-2005, 02:53 PM
I have the databases setup holding everything else, the reason i dont have this in a database is because the random number of rounds/races for each series, and creating a table to fit all possibilities wasnt possible, however thinking about it, maybe i need to make the table less specific.

I already have ones holding series info, round/race details, drivers competing, points scoring, and race results.
The race results table holds id : driver : position : points (and a couple other things which arent important)
I wanted to be able to show a standings table not only for the whole series worth of points, but also per-round. So if theres 2 rounds, 3 races at each, i'd like to be able to display a standings table for the 3 races in round 1, and also in round 2, but not combined, purely the individual rounds.

Maybe something like the following would work:
id : event :driver : round : points
1 : 1 : aaaaa : 1 : 16
2 : 1 : bbbbb : 1 : 10
3 : 1 : ccccc : 1 : 8
4 : 1 : aaaaa : 2 : 10
5 : 1 : bbbbb : 2 : 16
6 : 1 : ccccc : 2 : 8
7 : 1 : aaaaa : T : 26
8 : 1 : bbbbb : T : 26
9 : 1 : ccccc : T : 16
(T = total)

Then i could work out the points for the event_id for each driver, either combining each round's points for the total, or pick out each round's points,

I'll give that a try tonight.


btw, the SeeIT suggestion makes sense with the explained output, it looked as though it was nothing more than an array, never seen that before.

Kid Charming
07-02-2005, 04:47 PM
You should avoid trying to set up any info in the database that you can get with an aggregate function. You don't need to store totals -- as long as you have the individual rounds, you can get any totals you want in your query. Otherwise, it looks like you're on the right track. One change I'd make though, is to keep all your driver information in one table, giving each driver an INT id, and use that as a foreign key in your results table, so you'll have something like:

RESULT
race_id INT
round INT
driver_id INT
score

That way, if you ever have to change the driver's name for any reason, you only have to change it in the drivers table instead of having to track it down in other tables, too. You'll also want to put an index on that field, and INT indexes are more efficient.

PaulC2K
07-02-2005, 05:30 PM
Yeah, thats a good point really, at the moment the pages ive already created only ask for the drivers name for each position finishing a race, however at a later date i'll set it so drivers need to be 'registered' into the event, and there name will be selectable from a drop-down box, or something like that, then i can stick to IDs.
Most of this i started about 16-18 months ago, but never completed it, and only recently decided to pick it up again, so theres a mix of what i knew i needed to do then (probably been coding for 3yrs from stuff ive picked up) and stuff that i'd started without seeing the full picture so theres bits that arent ideal which i'll come back to and tidy up.
Just want to get it completed at the moment, so it can be used in 3 weeks time and then tidy it up when i have the chance.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum