Home > Software engineering >  Soring data ascending in PHP
Soring data ascending in PHP

Time:07-04

morning everyone,

I want to sort data from 3 tables but based on the total value that has been calculated automatically. this is my code

$query = mysqli_query($konek,"select * from klasifikasi join siswa on siswa.id_siswa=klasifikasi.id_siswa join prodi on prodi.id_prodi=siswa.id_prodi where klasifikasi.id_klasifikasi ORDER BY klasifikasi.raport_mtk DESC, klasifikasi.raport_bindo DESC");
    while ($dataku = mysqli_fetch_array($query)) {
    $total = ((($dataku['raport_mtk'])/7)  
    (($dataku['raport_bindo'])/7) 
    (($dataku['raport_bing'])/7) 
    (($dataku['raport_ipa'])/7) 
    (($dataku['un_mtk'])/7) 
    (($dataku['un_ipa'])/7) 
    (($dataku['psikotest'])/7)*0.75)-
    ((($dataku['raport_ips'])/4) 
    (($dataku['un_bindo'])/4) 
    (($dataku['un_bing'])/4) 
    (($dataku['npa'])/4)*0.25);

Later I want to sort the value from the largest based on the $total. I've tried but it doesn't work. please help bro

CodePudding user response:

Yo, bro, Do the calculation and the sorting in the query. Simpler and clearer, bro.

SELECT *,
    (raport_mtk raport_bindo raport_bing raport_ipa un_mtk un_ipa psikotest)/7*0.75-
    (raport_ips un_bindo un_bing npa)/4*0.25 AS total 
FROM klasifikasi 
    JOIN siswa ON siswa.id_siswa=klasifikasi.id_siswa 
    JOIN prodi ON prodi.id_prodi=siswa.id_prodi 
WHERE klasifikasi.id_klasifikasi 
ORDER BY total

CodePudding user response:

I think you need to fetch the array as an associative array, so it'd be: "mysqli_fetch_assoc($query)" else you can't access the array using the field-names

  •  Tags:  
  • php
  • Related