php - How to send SQL count data & array data together? -


i have script designed print out values of students have accrued more 3 tardies. want script print out both student name, , amount of times they've been tardy, far i've been able print out names following script:

$sql = "select distinct studentid,classid,date attendance_main status = 'tardy' , classid '%adv%'"; $result = mysql_query($sql) or die (mysql_error()); while($row=mysql_fetch_array($result)) {         $studentid = $row['studentid'];         $sql2 = "select distinct studentid,classid,date attendance_main studentid = '$studentid' , status = 'tardy' , classid '%adv%'";         $result2 = mysql_query($sql2) or die (mysql_error());         while($row2=mysql_fetch_array($result2)) {                 $tardycount = mysql_num_rows($result2);                 $studentid = $row2['studentid'];                 if($tardycount >= 3) {                                  $sql3 = "select * students rfid = '$studentid'";                                 $result3 = mysql_query($sql3) or die (mysql_error());                                 while($row3=mysql_fetch_array($result3)) {                                         $fname[] = $row3['fname'];                                 }                         }                 }         }   $newdata = array_unique($fname); foreach ($newdata $value) {         echo $value; } 

i can't think of how intuitively this. keeping in while loop didn't work (i had multiple results coming same students despite requesting unique entries) using array_unique method think of.

thanks help!

you can (and should) in sql. should this.

select studentid, classid, date count(*) attendance_main status = 'tardy' , classid '%adv%'" left join student on student.rfid = attendance_main.studentid group studentid having count(*) > 3; 

here's how works.

  1. select results want work with:

    select studentid, classid, date count(*) attendance_main status = 'tardy' , classid '%adv%'"

  2. join students result set on common id

    left join student on student.rfid = attendance_main.studentid

  3. group student. use count(*) number of items. know we're dealing tardies because of clause in select.

    group studentid

  4. limit results tardies above 3 having claues (this clause group by)

    having count(*) > 3;


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -