Looping Arrays?

Discussion in 'PHP' started by alhen, Sep 5, 2007.

  1. #1
    Hello there,
    I don't really understand looping yet, but I think it's what I need to do what I want... and what I want is this:

    In the MySQL table I have stored ID (Auto Incr.), Name, Date, Day of Week, & Time. This is who and when they clocked-in on a certain day so we can keep track of who is tardy.

    What I'd like to do is have it show up as a weekly report that has a column of names on the left, the last five dates across the top row, then the time the person signed-in in the corresponding field.

    I'm guessing that I'll need to say something like "if name=$name and date=$date, then $time"

    As I have it now I just have a list of names, dates and times. Here is my current code:
    
    <table width="500">
    <tr><td><b>Day</b></td><td><b>Date</b></td><td><b>Name</b></td><td><b>Time-in</b></td></tr>
    <?php
    
    MYSQL_CONNECT(localhost, austinds_alex, police) OR DIE("DB connection unavailable");
    @mysql_select_db( "austinds_teachers") or die( "Unable to select database"); 
    
    $date = (date ("Y-m-d"));
    $oneWeekAgo = strtotime ( '-1 week' , strtotime ( $date ) ) ; 
    $week = date ( 'm-d-y' , $oneWeekAgo ); 
    
    $query="SELECT * FROM signin WHERE date>'$week' ORDER BY name, date, day, time ASC";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $name=mysql_result($result,$i,"name");
    $date=mysql_result($result,$i,"date");
    $day=mysql_result($result,$i,"day");
    $time=mysql_result($result,$i,"time");
    
    if($time >= "07:31am") {
    echo "<tr><td> $day </td><td> $date </td><td> $name  </td><td><font color=\"red\">$time</font></td></tr>\n";
    } else if($time <= "07:32am") {
    echo "<tr><td> $day </td><td> $date </td><td> $name  </td><td> $time </td></tr>\n";
    } else {
    echo "There was a problem.";
    }
    ++$i;
    }
    ?>
    </table>
    
    PHP:
    which spits out something like this:
    
    Day         Date       Name        Time-in
    Wednesday   09-05-07   Bob Jones   07:28am
    Wednesday   09-05-07   John Doe    [COLOR="Red"][B]07:33am[/B][/COLOR]
    
    Code (markup):
    I would like it to look more like this:
    
    Name        09-04-07        09-05-07      etc...
    ---------------------------------------------------
    Bob Jones   07:23am         07:28am       etc...
    John Doe    07:23am         07:28am       etc...
    
    Code (markup):
     
    alhen, Sep 5, 2007 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Whats the difference?
     
    MMJ, Sep 5, 2007 IP