solved - P5AMWD, Ch 2, p83, Ex. 1 - array help
<i>I'm trying to solve the date.php problem on p 83 of Beginning PHP5, Apache, MySQL Web Development with a little cleverness from the Albert Einstein array example earlier in chapter 2, and I don't seem to understand the array function. It's August now, and I get the output - "8A" from this code -
==============================
<html>
<head>
<title> How many Days in this month?</title>
</head>
<body>
<?php
$reftable = array(1=>array("monthName"=>"January",
"monthDays"=>"31"),
2=>array("monthName"=>"February",
"monthDays"=>"28"),
3=>array("monthName"=>"March",
"monthDays"=>"31"),
4=>array("monthName"=>"April",
"monthDays"=>"30"),
5=>array("monthName"=>"May",
"monthDays"=>"31"),
6=>array("monthName"=>"June",
"monthDays"=>"30"),
7=>array("monthName"=>"July",
"monthDays"=>"31"),
8=>array("monthName"=>"August",
"monthDays"=>"31"),
9=>array("monthName"=>"September",
"monthDays"=>"30"),
10=>array("monthName"=>"October",
"monthDays"=>"31"),
11=>array("monthName"=>"November",
"monthDays"=>"30"),
12=>array("monthName"=>"December",
"monthDays"=>"31"));
$month = date("n");
echo $month;
echo $reftable[$month]["monthName"]["monthDays"];
echo "<br />";
?>
</body>
</html>
==================================
I tried a Print_r and got a bunch of array values, but I don't seem to be able to figure out how to get it just to spit out the one set of values I want. Help me out, if you can, please.</i>
Figured it out: Corrected code here -
===================================
<html>
<head>
<title> How many Days in this month?</title>
</head>
<body>
<?php
$reftable = array(1 => array("monthName" => "January",
"monthDays" => "31"),
2 => array("monthName" => "February",
"monthDays" => "28"),
3 => array("monthName" => "March",
"monthDays" => "31"),
4 => array("monthName" => "April",
"monthDays" => "30"),
5 => array("monthName" => "May",
"monthDays" => "31"),
6 => array("monthName" => "June",
"monthDays" => "30"),
7 => array("monthName" => "July",
"monthDays" => "31"),
8 => array("monthName" => "August",
"monthDays" => "31"),
9 => array("monthName" => "September",
"monthDays" => "30"),
10 => array("monthName" => "October",
"monthDays" => "31"),
11 => array("monthName" => "November",
"monthDays" => "30"),
12 => array("monthName" => "December",
"monthDays" => "31"));
$month = date("n");
echo "The current month number is ";
echo $month;
echo ", ";
echo $reftable[$month]["monthName"];
echo " which has ";
echo $reftable[$month]["monthDays"];
echo "days in it. <br />";
if ($month == 2) echo "Except during leap years, when it has 29 days.";
?>
</body>
</html>
=======================================
I was not referring to each field using the right sytax, but was assuming I could use a compound syntax to reference multiple fields in a single statement. Lesson learned.
|