[prog] php & mysql

Jacinta Richardson jarich at perltraining.com.au
Fri Feb 27 12:44:46 EST 2004


On Thu, 26 Feb 2004, wolf wrote:

> 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";

I don't think you needed to change the SQL to make the code work.  However
if this gives you more of the results you want then that's great.  Can I
assume you're still getting the same kind of data out without reading the
SQL?

> 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.

What I gave you was 90% Perl.  Of course it's not going to work in PHP.
Let's see if I can PHP-ize it a bit for you now.  I don't promise this
works, I don't promise that I won't miss anything.  If I do miss
something look carefully at the changes I make around it and see if you
can guess what it should look like.

If it doesn't work come back to us with what it is doing and what errors
it's giving you.  Not "I'm getting a parsing error on line 14" but
rather: "I get this error ".... parse error ..." at line 14 which is
this line: ....";

> > <?php
> >     while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {

That should work in php.

> >
> >           # split the schedule up on commas and put into an array
> >         @split = split( ",", $row_Recordset1['schedule']);

@split is an array in Per.  You probably need something like:

	    $split = split(", ", $row_Recordset1['schedule']);

> >           # for each item in the schedule
> >         foreach $value (@split) {

	    foreach ($split as $value) {

I think.

> >               # split it into day and hour
> >             ($day, $time) = split ("/", $value);

                list($day, $time) = split("/", $value);

> >             $room = $row_Recordset1['room_code'];

Should work in php.

> >
> >               # add data to day_time structure [1]
> >               # first check we're not overwriting anything
> >             if(exists $day_time{$room}{$day}{$time}) {

                if(array_key_exists $day_time[$room][$day][$time]) {

Maybe.  Depending on whether php allows you to nest structures like this.

> >                 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";

Warn is a Perl error function.  Maybe?

                    fprintf(STDERR, "..... message above ....");

there's probably a good php alternative, but I can't think of it right
now.

> >             }
> >             else
> >             {
> >                    # add course, faculty to this room/day/time.
> >                  $day_time{$room}{$day}{$time} = {
> >                      course => $row_Recordset1['course_title'],
> >                      faculty => $row_Recordset1['l_name']
> >                  };

This is probably:

                     $day_time[$room][$day][$time] =
                         array( course => $row_Recordset1['course_title'],
                                faculty => $row_Recordset1['l_name']
                     );

I think that should work....


> >             }
> >         }
> >     }
> >
> >       # Now we print out our data structure in a nice table
> >     foreach $room (1 .. 14) {    # assuming rooms 1 - 14 {

        foreach (array(1, 2, 3, 4, 5, 6, 7 etc) as $room) {

Does PHP have a .. operator for creating a list of sequential numbers?

> >         print "<tr>";
> >         $tr = 1;
> >         print "<td>$room</td>";

That should all work.

> >         foreach $time ( "9", "12", "3" ) {  # assuming 3 periods

            foreach (array( "9", "12", "3" ) as $time) {

> >                 # 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;

That should all work.

> >             foreach $day ("Mon", "Tues", "Wed", "Thur", "Fri") {

              foreach (array("Mon", "Tues", "Wed", "Thur", "Fri") as $day) {


> >                 if(exists $day_time{$room}{$day}{$time}) {

                    if(array_key_exists($day_time[$room][$day][$time]) {

> >                     print "<td>$day_time{$room}{$day}{$time}{course}<br>".

                      print "<td>$day_time[$room][$day][$time][course]<br>".

> >                         " $day_time{$room}{$day}{$time}{faculty}</td>";

                            " $day_time[$room][$day][$time][faculty]</td>";

> >                 } else {
> >                     print "<td>&nbsp;</td>";
> >                 }
> >             }
> >             print "</tr>";
> >         }
> >    }
> > ?php>

The rest of that should work.

> 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 : (

It's all part of learning a new language.  I mistakeningly assumed you
needed algorithmic help but knew (enough) PHP.  I didn't realise that
you needed both algorithmic help and learning PHP help.

I hope the above helps you out a bit.  It probably won't work first up.
If you get any interesting errors like "I don't know that function" kind
of things check out the php manual at www.php.net.

Good luck.

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list