NYCPHP Meetup

NYPHP.org

[nycphp-talk] Trouble with Array of Objects

Analysis & Solutions danielc at analysisandsolutions.com
Wed May 28 00:35:34 EDT 2003


On Tue, May 27, 2003 at 04:45:54PM -0400, Christopher R. Merlo wrote:
>
> I want to create five objects within a loop, and add them to an array.
 
Putting multiple objects into an array?  Even without looking at your
code, you're on the wrong track.  Very inefficient.     

Now, let's look at your code...


On Tue, May 27, 2003 at 05:00:34PM -0400, Christopher R. Merlo wrote:
> 
>   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++;
>     }
>   }
> }

First, are you calling multiple mysql_fetch_assoc's?  Sure looks like it, 
since you're calling new meeting() with $day_row[], $course_row[] etc.

I'm guessing you're getting things via multiple queries to multiple tables 
rather than writing one far more efficient query using LEFT JOIN's.

Second, why are you creating some class and method just to store some data 
that's already available in what should be one array?

I'm taking a very cursory and slightly drunken look at your code and all
it seems you're doing is a WHOLE LOT of work to save some course data.  
What is your ultimate goal here?  To print out a list of courses in a
preformatted way?  If this is the case, you could save a heck of a lot of
effort by just doing

while ($Row = mysql_fetch_array($RecordSet, MYSQL_ASSOC)) {
    echo "{$Row['start']} to {$Row['end']}: {$Row['dept']} etc...\
"
}

after executing a well formed query which does the date formatting in the 
query and joins all the relevant data up front.  Read the MySQL manual on 
how to write queries with the DATE_FORMAT() function:
    http://www.mysql.com/doc/en/Date_and_time_functions.html

For JOIN syntax, see http://www.mysql.com/doc/en/JOIN.html

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list