donglian7879 2013-09-25 13:11
浏览 36
已采纳

有关PHP代码的任何想法吗?

I need to make some math, and I cannot seem to find a way to do it.

I have the following table, resulting from this query:

SELECT materii.id,
       materii.materie,
        GROUP_CONCAT(note.note) AS note,
        GROUP_CONCAT(DISTINCT teza.`teza`) AS teza


FROM materii
LEFT JOIN teza ON materii.id = teza.id_materie
LEFT JOIN note ON materii.id = note.id_materie
LEFT JOIN elevi ON note.id_elev = elevi.cod_elev 
LEFT JOIN luni ON note.`luna_nota`=luni.`id`
WHERE elevi.`cod_elev` = 1 AND luna_nota = 9
GROUP BY materii.id, materii.materie
ORDER BY materii.materie

table

I need to do something like :

$notele = mysql_query($pentrumedie)
                or die("Nu am gasit note in baza de date");
$numar_note = mysql_num_rows($notele);
if($numar_note==0)
{

}
else 
{
    while($rand2=mysql_fetch_array($notele))
    {     
         $note1 = ($rand2['notele'] / $numar_note);
       $medie_septembrie = ($note1 / $cate_note_sunt);
    }
}

I need to do something like that for all the "note", but $rand2['notele'] needs to be the sum of them, if there are more "note", and if there is a value for "teza", then the math formula needs to be:

(($rand2['notele'] / $numar_note) * 3 + teza) / 4

I tried some if functions, but none of them work....Any Ideas? Thanks!!!

  • 写回答

1条回答 默认 最新

  • douhang5460 2013-09-25 13:32
    关注

    you can try to change a little bit your sql statement

    SELECT materii.id,
       materii.materie,
        SUM(note.note)/COUNT(note.note) AS medie,
        GROUP_CONCAT(DISTINCT teza.`teza`) AS teza
    FROM materii
    LEFT JOIN teza ON materii.id = teza.id_materie
    LEFT JOIN note ON materii.id = note.id_materie
    LEFT JOIN elevi ON note.id_elev = elevi.cod_elev 
    LEFT JOIN luni ON note.`luna_nota`=luni.`id`
    WHERE elevi.`cod_elev` = 1 AND luna_nota = 9
    GROUP BY materii.id, materii.materie
    ORDER BY materii.materie
    

    And in php use something like

    $medii = array();
    while($rand = mysql_fetch_assoc($notele))
    {
      if($rand["teza"] == "" || $rand["teza"] == NULL)
      {
       $medii[] = array("materie" => $rand["materie"],
                      "medie" => $rand["medie"]);
      }
      else
      {
       $medii[] = array("materie" => $rand["materie"],
                      "medie" => ((float)$rand["medie"]*3 + (float)$rand["teza"])/4);
       }
    }
    var_dump($medii);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题