[prog] php & mysql

wolf wolf at wolfrising.net
Thu Feb 26 16:09:23 EST 2004


Hi,

I'm still working on this : )
I've slightly revised the select statement to the following:
$query_Recordset1 = "select room_code, UPPER(c.title) AS course_title, 
f.l_name from section s, course c, faculty f";
$query_Recordset1 .= " where s.c_code = c.c_code and s.f_code_inst = 
f.f_code and term='fall' and year=2003  AND schedule like '%Mon/6%' AND 
s.campus_code = 'SCE' order by s.room_code, s.c_code";



however, I don't quite seem to be able to get the php part working 
still, from checking my
php book it seems that @can not be used for error checking ( if that's 
what it was intended for)
and even with commenting that out, I still receiving an error of 
parsing error on line 14.

> <?php
>     while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
>
>           # split the schedule up on commas and put into an array
>         @split = split( ",", $row_Recordset1['schedule']);
>
>           # for each item in the schedule
>         foreach $value (@split) {
>               # split it into day and hour
>             ($day, $time) = split ("/", $value);
>
>             $room = $row_Recordset1['room_code'];
>
>               # add data to day_time structure [1]
>               # first check we're not overwriting anything
>             if(exists $day_time{$room}{$day}{$time}) {
>                 warn "$room is double booked on $day at $time between 
> ".
>                      "$row_Recordset1['course_title'] - ".
>                      "$row_Recordset1['l_name'] and ".
>                      "$day_time{$room}{$day}{$time}{course} - ".
>                      "$day_time{$room}{$day}{$time}{faculty}\n";
>             }
>             else
>             {
>                    # add course, faculty to this room/day/time.
>                  $day_time{$room}{$day}{$time} = {
>                      course => $row_Recordset1['course_title'],
>                      faculty => $row_Recordset1['l_name']
>                  };
>             }
>         }
>     }
>
>       # Now we print out our data structure in a nice table
>     foreach $room (1 .. 14) {    # assuming rooms 1 - 14 {
>         print "<tr>";
>         $tr = 1;
>         print "<td>$room</td>";
>
>         foreach $time ( "9", "12", "3" ) {  # assuming 3 periods
>                 # Start each new time on a new row except the first
>                 # pad out the room value so that everything lines up
>             if($tr == 0) {
>                 print "<tr>";
>                 print "<td>&nbspl</td>";
>             }
>             print "<td>$time</td>";
>             $tr = 0;
>
>             foreach $day ("Mon", "Tues", "Wed", "Thur", "Fri") {
>                 if(exists $day_time{$room}{$day}{$time}) {
>                     print 
> "<td>$day_time{$room}{$day}{$time}{course}<br>".
>                           "
> $day_time{$room}{$day}{$time}{faculty}</td>";
>                 } else {
>                     print "<td>&nbsp;</td>";
>                 }
>             }
>             print "</tr>";
>         }
>    }
> ?php>


If anyone might have any ideas, all ears are open. I seem to be stuck, 
it makes sense in
my head what I want to do, but I seem to have a devil of time trying to 
convert any of
that to code : (

altho on a positive note, I have been getting a bit better at doing C++ 
, one step at a time
I suppose : )

Thanks!

On Feb 24, 2004, at 7:28 PM, Jacinta Richardson wrote:

> On Tue, 24 Feb 2004, wolf wrote:
>
>> Hi, I'm trying to make a nice looking room chart that shows where
>> classes are held, which room number, what class, and so on.
>> I have this query that I was given:
>
>> what I'd like the code to do is produce a table that looks more like
>> this:
>>
>> ______________________Period 3______________________________
>>
>> Room     Mon                 Tues                   Wed
>> Thur                  Fri
>>
>>     14        ACCT I             ACCT I               ACCT I
>> ACCT I         ACCT I
>>                     Wilt                   Wilt
>> Wilt                    Wilt               Wilt
>>
>>
>> does anyone happen to see a way I could either re-write the query or a
>> way to re-write the php code so that the table would
>> appear like the second example?
>
> What you're going to need to do is parse the schedule and then,
> once you've gotten everything out of the database, print out your
> table.  This will result in code something like the following.
> I'm a Perl coder and I don't have the time to work out what all
> the equivalent functions are in php, so I'm going to use perl
> jargon.  If you have any trouble translating it back into php let
> me know.
>
> <?php
>     while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
>
>           # split the schedule up on commas and put into an array
>         @split = split( ",", $row_Recordset1['schedule']);
>
>           # for each item in the schedule
>         foreach $value (@split) {
>               # split it into day and hour
>             ($day, $time) = split ("/", $value);
>
>             $room = $row_Recordset1['room_code'];
>
>               # add data to day_time structure [1]
>               # first check we're not overwriting anything
>             if(exists $day_time{$room}{$day}{$time}) {
>                 warn "$room is double booked on $day at $time between 
> ".
>                      "$row_Recordset1['course_title'] - ".
>                      "$row_Recordset1['l_name'] and ".
>                      "$day_time{$room}{$day}{$time}{course} - ".
>                      "$day_time{$room}{$day}{$time}{faculty}\n";
>             }
>             else
>             {
>                    # add course, faculty to this room/day/time.
>                  $day_time{$room}{$day}{$time} = {
>                      course => $row_Recordset1['course_title'],
>                      faculty => $row_Recordset1['l_name']
>                  };
>             }
>         }
>     }
>
>       # Now we print out our data structure in a nice table
>     foreach $room (1 .. 14) {    # assuming rooms 1 - 14 {
>         print "<tr>";
>         $tr = 1;
>         print "<td>$room</td>";
>
>         foreach $time ( "9", "12", "3" ) {  # assuming 3 periods
>                 # Start each new time on a new row except the first
>                 # pad out the room value so that everything lines up
>             if($tr == 0) {
>                 print "<tr>";
>                 print "<td>&nbspl</td>";
>             }
>             print "<td>$time</td>";
>             $tr = 0;
>
>             foreach $day ("Mon", "Tues", "Wed", "Thur", "Fri") {
>                 if(exists $day_time{$room}{$day}{$time}) {
>                     print 
> "<td>$day_time{$room}{$day}{$time}{course}<br>".
>                           "
> $day_time{$room}{$day}{$time}{faculty}</td>";
>                 } else {
>                     print "<td>&nbsp;</td>";
>                 }
>             }
>             print "</tr>";
>         }
>    }
> ?php>
>
> [1]  our day_time structure would be called a hash of hashes (of
> hashes of hashes) in Perl.  In php there is no distinction
> between hashes and arrays so you'll need to change all of my
> curly braces around array elements to square ones.
>
> This data structure maps rooms to days and that to times and that
> to course and faculty.  The data structure might look like this
> if you were to print it out.
>
> Room       Day       Time
>    1  ->   Mon   ->  9  ->  course   -> "Mathematics"
>                             faculty  -> "Science"
>                     12  ->  course   -> "English"
>                             faculty  -> "Arts"
>                      3  ->  course   -> "Accounting"
>                             faculty  -> "Commerce"
>            Tues  ->  12 ->  course   -> "Mathematics"
>                             faculty  -> "Science"
>
> etc.  That is:  $day_time is a hash of rooms.
>                 $day_time{$room} is a hash of days
>                 $day_time{$room}{$day} is a hash of times
>                 $day_time{$room}{$day}{$time} is a hash of course
>                    information  containting course name and faculty.
>
> I'm sure that you've got to be able to build up structures like
> this in php.
> array day_time = array( 1 => array( Mon => array( 9 =>
>        array(course=>"Mathematics", faculty => "Science" ))));
>
> or something like that.  It's probably easy enough to build them
> up on the fly like I did too.
>
> -----------------------
>
> You'll probably need to adjust the rooms and the times above, but
> I'm sure that'll be easy.
>
> I hope this helps.
>
>      Jacinta
>
> --
>    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
>     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
>     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |
>   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
> (il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>



More information about the Programming mailing list