hi i have an list of countries on table so i am sorting with country name then i get A-Z locations like this Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands,USA, Yemen , Zambiabut actually i need USA on top (i mean first element) like this order USA,Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands, Yemen , Zambiahere is my code
<?php
require 'dbconnect.php';
$empty=array();
$c=0;
$query="select * from phpfox_country";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
$message=array("country_iso"=>$row[0],"name"=>$row[1])
}
else
{
$message[$c]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
$c++;
}
$message=($message==null)?$empty:$message;
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
thanks for advance.
Looks like there are some errors in your code. First, by setting
$cto 0 by default and using that as an array index you are assigning the alphabetically first country to that slot, not USA. Worse still, when you actually reach the row with USA on it, you don’t add it to the existing array, instead you overwrite the array by assigning a new array.I suggest that you don’t use
$cat all. Instead start with an empty array, add USA to the front of the array, and all other countries to the end of the array. As for arranging the records, that is easiest to do with the SQL query. The below code assumes the column with the country name is calledname– if it is not, you can fix this yourself.