根据平均值

时间:2017-08-10 14:09:36

标签: php arrays rank

我已经尝试了一段时间,但仍然,我被困在这里。实际上我需要从平均值中获得排名。这是我的结果

Average         Rank
   39        39 rank is 1
   32        32 rank is 1
   51        51 rank is 1
   57        57 rank is 1

我真正需要的是

Average           Rank
  39               3 
  32               4 
  51               2 
  57               1

我尝试了几种方法,但似乎没有任何效果。顺便说一下这是我的代码

<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?> 

<?php 
$array = array($avg);
$i=1;
foreach($array as $key=>$value) 
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;
}
					

这是完整的代码

<?php $nama_mapel=$ux['nama_mapel'];
$an = mysql_fetch_array(mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas' and nama_mapel='$nama_mapel'"))?>
											
<text style="display:none;"><?php if($NILAI_AKHIR!=0){$NILAI_AKHIR4=substr($NILAI_AKHIR/100*4,0,4); echo $NILAI_AKHIR4;} ?></text>
                     <tr>
                    <td width="5%" class="ki1234" height="auto" valign="center" align="center"><?php echo $no++ ;?></td>                     
                    <td width="40%" class="ki1234" height="auto" valign="center" align="left">&nbsp;<?php echo $mmm['nama'];?></td>
					<td width="10%" class="ki1234" height="auto" valign="center" align="center"><?php echo $nis; ?></td>
					<?php 
			
			$mapelz=mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas'  and id_tp='$id_tp'");
			$arrg=[];
			while($uxe = mysql_fetch_array($mapelz)){
		    $id_mapel=$uxe['id_mapel'];
		
//awal nilai akhir//		
$dren = mysql_query("SELECT*FROM nilai_siswa,data_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=data_siswa.nis and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_tugas=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm; };
$queryj=mysql_query("SELECT sum(n3_100) as rn3_100 FROM nilai_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn3_100=substr($hasil['rn3_100']/$jumlah,0,5); $RATA_TUGAS=$rn3_100;}

$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uh=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UH= $rn_100;}

$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uts=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UTS= $rn_100;}

$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_us=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_US= $rn_100;}

if(!isset($RATA_TUGAS)){$RATA_TUGAS2=0;}else{$RATA_TUGAS2=$RATA_TUGAS;};
			  if(!isset($RATA_UH)){$RATA_UH2=0;}else{$RATA_UH2=$RATA_UH;};
			  if(!isset($RATA_UTS)){$RATA_UTS2=0;}else{$RATA_UTS2=$RATA_UTS;};
			  if(!isset($RATA_US)){$RATA_US2=0;}else{$RATA_US2=$RATA_US;};
$jumlahc=($persen['tugas']*$RATA_TUGAS2)/100+($persen['uh']*$RATA_UH2)/100+($persen['uts']*$RATA_UTS2)/100+($persen['us']*$RATA_US2)/100;

							if($jumlah_tugas!=0){$k=1;} else {$k=0;};
							if($jumlah_uh!=0){$l=1;} else {$l=0;};
							if($jumlah_uts!=0){$m=1;} else {$m=0;};
							if($jumlah_us!=0){$n=1;} else {$n=0;};
							$JUMLAH_PERSEN1=($persen['tugas']*$k)+($persen['uh']*$l)+($persen['uts']*$m)+($persen['us']*$n);
							if($JUMLAH_PERSEN1==0){$JUMLAH_PERSEN=1;}else{$JUMLAH_PERSEN=$JUMLAH_PERSEN1;};
							$NILAI_AKHIRg=substr(($jumlahc/$JUMLAH_PERSEN)*100,0,5);
$nile= $NILAI_AKHIRg;
$arrg[] = $NILAI_AKHIRg;} ?>
 			
        <?php $p = mysql_fetch_array(mysql_query("SELECT * FROM peringkat WHERE kode_kelas='$kode_kelas' and nis='$nis'and id_tp='$id_tp'")); ?>	 
		<text style="display:none;"> <?php $sumc = 0; $output = ""; while($element = array_shift($arrg)){$sumc += $element; $output .= $element; if(sizeof($arrg) > 0){ $output .= " + ";}} echo $output . PHP_EOL;?></text>
		 <td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php echo $sumc;?></td> 
		 <text style="display:none;"><?php $result=mysql_query("SELECT count(*) as total FROM mapel WHERE kode_kelas='$kode_kelas' and kel!=3"); $data=mysql_fetch_assoc($result); echo $data['total'];?></text>
		  <td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?></td> 
		 <td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php 
$array = array($avg);
$i=1;
foreach($array as $key=>$value) 
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;
}
?></td> 
									
                  </tr>
				  <?php }?>

需要你的帮助..任何人......?!

1 个答案:

答案 0 :(得分:0)

根据您发布的内容,问题是如何对降序的平均值进行排序。

考虑到这一点,你知道如果你在旅途中计算平均值,你必须先通过整个数组,所以这里有一些片段来帮助你:

// Random values I want to calculate the average
$values = [
    1 => [15, 25, 35],
    2 => [45, 55, 65],
    3 => [75, 85, 95],
    4 => [105, 115, 125]
];

$avgs = array();
foreach ($values as $key => $value) {
    $avgs[$key] = array_sum($value) / count($value); // calculate average for each key on the array
}

print_r($avgs);
// Will print:
// Array
// (
//    [1] => 25
//    [2] => 55
//    [3] => 85
//    [4] => 115
//)

arsort($avgs); // sort the array in descending order
print_r($avgs);

// Will print:
//Array
//(
//    [4] => 115
//    [3] => 85
//    [2] => 55
//    [1] => 25
//)

因此,如果您的avg数组已经排序,那么您就拥有了#34; rank&#34;。

PS。键1,2,3,4只是随机值,你可以在那里找到任何东西。