0

I have a page that display the logs of student according to date, year, section and IN/OUT. My question, how to count the number of student who displayed by filtering.

screenshot:http://oi60.tinypic.com/2a5ejgg.jpg

This is the code.

//SET UP SQL STATEMENT
$WHERE='';
if ($year<>'0')
{
    $WHERE = " student.year =  $year ";
}

if ($section<>'0')
{
    if ($WHERE=='')
    {
        $WHERE = " student.section =  '" . $section . "'";
    }
    else
    {
        $WHERE = $WHERE . "and student.section =  '" . $section . "'";
    }
}
//SET UP SQL STATEMENT
if ($in_out<>'0')
{
    if ($WHERE=='')
    {
        $WHERE = " `log`.status_log =  '" . $in_out . "'";
    }
    else
    {
        $WHERE = $WHERE . "and `log`.status_log =  '" . $in_out . "'";
    }
}
//SET UP SQL STATEMENT
if ($datefilter<>'')
{
    if ($WHERE=='')
    {
        $WHERE = " `log`.date_log =  '" . $datefilter . "'";
    }
    else
    {
        $WHERE = $WHERE . "and `log`.date_log =  '" . $datefilter . "'";
    }
}
$start = ($page-1)*$per_page;

if ($WHERE=='')
{
    //SET UP SQL STATEMENT
    $sql = "SELECT `log`.id, (student.id) as ids, student.name, 
                   student.`year`, student.section, `log`.date_log, 
                   `log`.time_log, `log`.ampm, `log`.status_log,
                   section.sec_name 
            FROM `log` 
               Inner Join student ON student.cardcode = `log`.stud_id 
               Inner Join section ON section.id = student.section 
            order by `log`.id DESC 
            limit $start,$per_page";
}
else
{//SET UP SQL STATEMENT SELECT COUNT(*) AS totalin FROM log WHERE status_log='in'
    $sql = "SELECT `log`.id, (student.id) as ids, student.name,
                   student.`year`, student.section, `log`.date_log, 
                   `log`.time_log, `log`.ampm, `log`.status_log, 
                   section.sec_name 
            FROM `log` 
               Inner Join student ON student.cardcode = `log`.stud_id 
               Inner Join section ON section.id = student.section 
            WHERE $WHERE 
            order by `log`.id DESC
            limit $start,$per_page";

}
//EXECUTE SQL STATEMENT
$result = mysql_query($sql);


//OOP ALL QUERY DATA ON LOAD AS LIST
while($row = mysql_fetch_array($result))
{
    echo '<tr style="font-size: 12px;" class="record">';
    echo '<td ><a style="text-decoration:none; color:#4F6B72" href="studentlog.php?id='.$row['ids'].'"</a>' .$row['name']. '</td>';
    if ($row['year']=='1')
    { echo '<td>&nbsp;1st</td>';}
    else if ($row['year']=='2')
    { echo '<td>&nbsp;2nd</td>';}
    else if ($row['year']=='3')
    { echo '<td>&nbsp;3rd</td>';}
    else if ($row['year']=='4')
    { echo '<td>&nbsp;4th</td>';}
    echo '<td >'. $row['sec_name'].  '</td>';
    echo '<td >'. $row['status_log'] .'</td>';
    echo '<td>'.$row['date_log'].'</td>';
    echo '<td>'.$row['time_log'] . ' ' . $row['ampm']   .'</td>';
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

0

Once you have executed the query you can us mysql_num_rows() which tells you the actual number of row return by the query

So

//EXECUTE SQL STATEMENT 
$result = mysql_query($sql);        

$rows_returned = mysql_num_rows($result);

As it seems obvious you are just learning PHP and MYSQL can I suggest that you do not use the mysql_ extension!

Instead learn mysqli_ or PDO see this document for why

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149