Re: Just can't get it to work.. variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Gavin Amm wrote:

> Hi All,
> 
> I just can't for the life of me get the variable to output a value from
> the array:
> (all preceding script in place to make this work..)
> 
> The "start" date retrieved from the database is, for example, 20050412
> (yyyymmdd).
> 
> The example output for each variable (w/o quotes) is:
> $monthInt outputs "04"
> $monthVal outputs ""
> 
> ### CODE FOR DEBUGGING PURPOSES ###
> while ($row = mysql_fetch_array($result)){
> $monthName = array ("", "Jan", "Feb", "Mar", "Apr", "May",
> "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
> $monthInt = substr($row['start'], 4, 2);
> echo "\$monthInt = $monthInt<br>";
> $monthVal = $monthName[$monthInt];
> echo "\$monthVal = $monthVal<br>";
> echo "<p><b>".$monthVal." ".substr($row['start'], 6,
> 2)."</b><br>\n".$row['details']."\n</p>\n\n";
> }

That would indicate that you are actually saying:
 $monthVal = $monthName[04];

where the 04 is actually a string; you have no element '04' in your array.
Cast $monthInt to an integer before you use it as an array element.

Alternatively, if you are using MySQL, you can use DATE_FORMAT to achieve
the same result.
> 
> ### ORIGINAL CODE ###
> while ($row = mysql_fetch_array($result)){
> $monthName = array ("", "Jan", "Feb", "Mar", "Apr", "May",
> "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
> echo "<p><b>".$monthName[substr($row['start'], 4, 2)]."
> ".substr($row['start'], 6, 2)."</b><br>\n".$row['details']."\n</p>\n\n";
> }

David

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux