[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use variables names in other variables ?

Posted on 2006-04-04
7
Medium Priority
?
165 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 !!
0
Comment
Question by:davidburley
5 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16371890
You can have
$year++;
$date = $month.$year;

-r-
0
 

Author Comment

by:davidburley
ID: 16372534
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.
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16372945
$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
0
 
LVL 17

Accepted Solution

by:
BogoJoker earned 500 total points
ID: 16372984
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
0
 
LVL 2

Assisted Solution

by:x95guzik
x95guzik earned 500 total points
ID: 16373314
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;
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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