Dynamically Creating PHP Variable
Posted: Sat Jul 09, 2011 2:51 pm
I'm creating a lottery statistic program and need some help. Consider the following six number stored in the database: 1, 2, 3, 4, 5, 6. Each of the six numbers are stored in the database as num_1, num_2, num_3, etc. I want to call the six numbers and count/store their occurrences.
If the number "1" is drawn, I want to add 1 to the $occ_num_1 variable. Here's my code and the issue. Should be simple but I'm lost...
Let me know if this makes sense. I create variable dynamically, but can't increment it as a variable. Do I have to use a cast or a variable variable? Not sure and can't get it to work
If the number "1" is drawn, I want to add 1 to the $occ_num_1 variable. Here's my code and the issue. Should be simple but I'm lost...
Code: Select all
//Setting the variables to zero. These are variable to record the occurrences of the numbers drawn in the lottery.
$occ_num_1 = 0;
$occ_num_2 = 0;
$occ_num_3 = 0;
$occ_num_4 = 0;
$occ_num_5 = 0;
$occ_num_6 = 0;
$occ_num_7 = 0;
$occ_num_8 = 0;
$occ_num_9 = 0;
$occ_num_10 = 0;
//Loop through all rows of the database.
for ($i=1; $i<=$num_rows; $i++)
{
// This inner loop goes through the six numbers and should add "1" to the occurrences of the numbers - This is where I'm getting hung up
for($z=1; $z < 7; $z++)
{
// This call the first num_x from the database. Example the first pass would be num_1 variable from the database. This works fine! In my lotto drawing example (above), this would be the number "1" when calling num_1 from the database
$w = $rs1['num_'.$z];
//Here the bug. Below I create $occ_num_1 dynamically, but now I want to increment this variable by one. It appears it's a string, not the variable $occ_num_1. I want to do $occ_num_1 = $occ_num_1 +1;
'$occ_num_'.$w;
}
}