How to use variables names in other variables ?

Posted on 2006-04-04
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>";
      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 !!
Question by:davidburley
    LVL 49

    Expert Comment

    You can have
    $date = $month.$year;


    Author Comment

    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.
    LVL 17

    Expert Comment

    $arrayMonths = array('Jan2005' => 0, 'Feb2005' => 0, 'Mar2005' => 0, 'Apr2005 => 0,....)

    Now when you say: $Jan2005 you could just do:

    To Increment them:

    That sound ok for you?
    Joe P
    LVL 17

    Accepted Solution

    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
    LVL 2

    Assisted Solution

    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;


    echo "<br>".$m012006;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now