Re: Just can't get it to work.. variables
| From: | David Robley | Date: | Wed, 30 Mar 2005 19:33:12 +0000 |
| Subject: | Re: Just can't get it to work.. variables | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-39068@lists.php.net to get a copy of this message | ||
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