NYCPHP Meetup

NYPHP.org

[nycphp-talk] Trouble with Array of Objects

Christopher R. Merlo cmerlo at turing.matcmp.ncc.edu
Tue May 27 17:00:33 EDT 2003


On 2003-05-27 16:50 -0400, Bhulipongsanon, Pinyo <Pinyo.Bhulipongsanon at usa.xerox.com> wrote:

> for ( $i = 0 ; $i <= 4 ; $i++ ) {
>   $myArray[$i] = codeToCreateObject();
> }
> 
> I am not sure if this will help?  Can I see your code?

I'll give you the highlights, and spare you most of the MySQL stuff.

Here's the class def:

  class Meeting {

  var $day;
  var $start;
  var $end;
  var $dept;
  var $course;
  var $building;
  var $room;

  function Meeting( $day, $start, $end,
		    $dept, $course, $section,
		    $building, $room ) {
    $this->day = $day;
    $this->start = $start;
    $this->end = $end;
    $this->dept = $dept;
    $this->course = $course;
    $this->building = $building;
    $this->room = $room;
    print "<p>" . $this->toString( ) . "</p>\
";
  }

  function toString( ) {
    return date( "g:i a", strtotime( $this->start ) ) . " to "
      . date( "g:i a", strtotime( $this->end ) ) . ": $this->dept "
      . "$this->course $this->section in $this->building $this->room";
  }
} // class Meeting

And here's the relevant code:

$meetings = array( );

// some more stuff

  foreach( $day_names as $day=>$name ) {

    $day_query = "select * from meetings where id = \\"{$sec_row[ "$day" ]}\\"";
    $day_result = @mysql_query( $day_query );

    $day_row = @mysql_fetch_assoc( $day_result );
    if( !is_null( $day_row[ 'start' ] ) ) {

      $meetings[ $meeting_count ] =
        new Meeting( $name, $day_row[ 'start' ], $day_row[ 'end' ],
                       $course_row[ 'dept' ], $course_row[ 'course' ],
                       $sec_row[ 'section' ], $day_row[ 'building' ],
                       $day_row[ 'room' ] );
      $meeting_count++;
    }
  }
}

Here's what I get on the page.  First, the results of the constructor
being called five times:

8:00 am to 10:50 am: CMP 103 in B 113

8:00 am to 10:50 am: CMP 103 in B 119

8:00 am to 10:50 am: CMP 103 in B 113

8:00 am to 10:50 am: CMP 103 in B 119

8:00 am to 10:50 am: CMP 218 in B 113

And then the result of print_r( $meetings ):

Array ( [4] => meeting Object ( [day] => Friday [start] => 08:00:00
[end] => 10:50:00 [dept] => CMP [course] => 218 [building] => B [room]
=> 113 ) ) 

And I'm at a loss.

-- 
cmerlo at turing.matcmp.ncc.edu        http://turing.matcmp.ncc.edu/~cmerlo

We're sorry, but the number you have dialed is imaginary.  Please
rotate your phone ninety degrees and try again.  Thank you.



More information about the talk mailing list