Array & object sort
<?php
$sel = mysqli_query($conn , "SELECT DISTINCT `lat_log` FROM `visitors`");
$num = mysqli_num_rows($sel);
$numArr = [];
if($num){
while($r = mysqli_fetch_assoc($sel)){
$city = $r['lat_log'];
$selcount = mysqli_query($conn , "SELECT `id` FROM `visitors` WHERE `lat_log`='$city'");
$ncount = mysqli_num_rows($selcount);
// $arr = Array($city , $ncount);
$arc = ['city'=>$city,'count'=>$ncount];
array_push($numArr,$arc);
}
usort($numArr,function($object1, $object2){
return $object1['count'] < $object2['count'];
});
foreach(array_slice($numArr , 0,15) as $k=>$v){
?>
<a class="btn btn-info"> <?php echo $numArr[$k]['city']==''?'Unknown city':$numArr[$k]['city'];?> : <?php echo $numArr[$k]['count'];?></a>
<?php
}
}
?>
Comments
Post a Comment