1

I have a simple array which has been used before with no problems. Now I am trying to use it again and it is displaying nothing for text (Even in view source).

Array:

$month = Array(
    1 => "January",
    2 => "February",
    3 => "March",
    4 => "April",
    5 => "May",
    6 => "June",
    7 => "July",
    8 => "August",
    9 => "September",
    10 => "October",
    11 => "November",
    12 => "December"
);

Code:

function getMonthlyStats($comic_id) {
    require "config.php";
    $query = 'SELECT *, SUM(views) AS `views` FROM '.$db_tbl_stats.' WHERE '.$db_fld_stats_comic_id.'="'.$comic_id.'" GROUP BY '.$db_fld_stats_month.' ASC';
    $r_query = mysql_query($query);
    while ($result = mysql_fetch_array($r_query)) {
    $percent = ($result[$db_fld_stats_views]/getTotalStats($comic_id))*100;
    $m = number_format($result['month']);
        echo '<tr>';
        echo '<td width="100">&nbsp;&nbsp;';
        echo $month[$m];
        echo '</td>';
        echo '<td width="400" class="bar"><div style="width: '.$percent.'%"></div>'.$result[$db_fld_stats_views].' Views</td>';
        echo '<td>'.number_format($percent).'%</td>';
        echo '</tr>';
    }

}

$m returns a number from 1-12 which obviously represents a month.

If you need more info I will be happy to explain more. Any help is greatly appreciated! Going on 4 hours writing and my eyes are tired.

5
  • 1
    can you please provide more code? Commented Aug 5, 2012 at 18:29
  • var_dump($m) returns string(1) "7" Commented Aug 5, 2012 at 18:30
  • do a var_dump of $month also, it may be overridden somewhere. Commented Aug 5, 2012 at 18:32
  • var_dump($month) returns null Commented Aug 5, 2012 at 18:35
  • There's your problem. Make $month global or pass it in an argument Commented Aug 5, 2012 at 18:42

1 Answer 1

1

You need to add global $month; to your function, or use $GLOBALS['month'] instead of $month.

This is called "variable scope". See http://php.net/manual/en/language.variables.scope.php for more details.

Sign up to request clarification or add additional context in comments.

8 Comments

yeah $m comes out as either 7 or 8 atm as those are the only 2 i have in my database.
What happens if you add the line print $month[7]?
print $m; returns a 7 or 8.
Sure - what about print $month[7]; or print $month[8];?
print $month[7] = nothing displayed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.