Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 13th, 2007, 02:52 PM
Registered User
 
Join Date: Aug 2007
Location: Columbia, MD, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Getting Array Error message with Begin PHP4 Ch 13 vaderj PHP Databases 4 April 17th, 2005 04:37 PM
p83 the use of periods... ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 19th, 2005 09:48 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Ch. 4 & Ch. 12 athena BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 23rd, 2004 10:54 AM



All times are GMT -4. The time now is 04:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.