We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to use variables names in other variables ?

davidburley
davidburley asked
on
Medium Priority
186 Views
Last Modified: 2008-03-04
I am trying to retrieve some fields from database within phpnuke (PHP / MySQL CMS)
the field in question is the user_regdate (The date the user registered on the site)
The format of the field is MMM DD, YYYY i.e. Jan 10, 2006

I wanted to pull the results from the database and populate a table with how many members joined that month

Code i have so far is:-

            echo "<table>";
            echo "<tr><td>Year</td><td>Month</td><td>Users</td></tr>";
            $querystring = "SELECT user_regdate FROM nuke_users";
            $query = $db->sql_query( "$querystring" );
            while (list($regdate) = $db->sql_fetchrow($query)) {
                  list($monthday,$year) = explode(",",$regdate);
                  list($month, $day) = explode(" ", $monthday);
                  $year = trim($year);
//                  echo "Month - $month<br>Year - $year<br><br>";
                  if ($month=="Jan" && $year=="2006") {
                        echo "match<br>";
                        $month.$year++;
                  }
            }
      echo "Jan 2006 - " . ${Jan2006} . ":<br>";
      }

This does pull the info out and split it into variables i.e. $day, $month and $year but i am then simply trying to get it to create matching set variables that accummulate up i.e. Jan2006, Feb2006, Mar2006 etc and then adding 1 to each variable if it matches to get total amount per month

The problem is that the adding up code "$month.$year++; doesnt seem to work, i have also tried $$month$year++;

I know i have used this before somehow but too long ago to remember and cant find the code snippet now !!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

Commented:
You can have
$year++;
$date = $month.$year;

-r-

Author

Commented:
ah y but that doesnt populate my variables properly i.e. i want a seperate variable for every month of the year for every year
i.e. $Jan2005, $Feb2005, $Mar2005, $Apr2005 etc and each month would simply hold the count of how many users joined during that month.
$arrayMonths = array('Jan2005' => 0, 'Feb2005' => 0, 'Mar2005' => 0, 'Apr2005 => 0,....)

Now when you say: $Jan2005 you could just do:
$arrayMonths['Jan2005']

To Increment them:
$arrayMonths['Jan2005']++;

That sound ok for you?
Joe P
I initailize the array with a single line syntax:
$array = array('a' => 0)

That is the same as:
$array = array();
$array['a'] = 0;

I am not familier with 2d arrays in php but I am sure experts like Roonaan know how.  They could potentially improve my code so that you have a seperate section for each month, like this:
$arrayMonths['Jan']['2005'] = 0;
Which would be different from $arrayMonths['Jan']['2006'];

But I don't know how to do that yet, It will probably not be too difficult if you are intrested in such an improvement.
Joe P

Note: You can also use this with the code I gave you:
$registerTime = 'Jan2005';
echo $arrayMonths[$registerTime]; // this will print out the current number in $arrayMonths['Jan2005'] so it is variable safe inside those []s

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
Below is a simplified example with the method you are looking for I believe.

//put the 'm' in front for PHP will not accept a number for its variable first character
$month = "m12";
$year = "2005";

$monthYear = $month.$year;
${$monthYear} = 16;

$month = "m01";
$year = "2006";

$monthYear = $month.$year;
${$monthYear} = 12;

echo $m122005;
echo "<BR>".$m012006;

${$monthYear}++;

echo "<br>".$m012006;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.