this PHP
script is supposed to return the top five losers, the top five gainers and the five most active stocks in the form of arrays called result. when i run the query in the database it returns the right values but when i run the PHP
script in the browser it doesn't return the first array, it starts from the second array ,the second array and so on. this is the script:
switch ($which){
case 'top':
$sql = "SELECT * FROM market_table ORDER BY price_change DESC LIMIT 5";
$result = $this -> connection->query($sql);
if($result->num_rows > 0){
$r = mysqli_query($this->connection,$sql);
$res = mysqli_fetch_array($r);
$result = array();
while($res = mysqli_fetch_array($r)){
array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],
'l_price'=>$res['l_price'],'deals_n'=>$res['deals_n'],
'value'=>$res['value']));
}
echo json_encode(array("result"=>$result));
}
else{
echo "top five: 0 results";
}
break;
case 'bottom':
$sql = "SELECT * FROM market_table ORDER BY price_change ASC LIMIT 5";
$result = $this -> connection->query($sql);
if($result->num_rows > 0){
$r = mysqli_query($this->connection,$sql);
$res = mysqli_fetch_array($r);
$result = array();
while($res = mysqli_fetch_array($r)){
array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],'l_price'=>$res['l_price'],
'deals_n'=>$res['deals_n'],
'value'=>$res['value']));
}
echo json_encode(array("result"=>$result));
}
else{
echo "bottom five: 0 results";
}
break;
case'most':
$sql = "SELECT * FROM market_table ORDER BY deals_n DESC LIMIT 5";
$result = $this -> connection->query($sql);
if($result->num_rows > 0){
$r = mysqli_query($this->connection,$sql);
$res = mysqli_fetch_array($r);
$result = array();
while($res = mysqli_fetch_array($r)){
array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],'l_price'=>$res['l_price'],
'deals_n'=>$res['deals_n'],
'value'=>$res['value']));
}
echo json_encode(array("result"=>$result));
}
else{
echo "most active five: 0 results";
}
break;
}
$res = mysqli_fetch_array($r);
$result = array();
Why do you have those? You're not saving the result($res) since you're initializing the result($result) to an empty array on the 2nd line. So that's where your first line disappears.