2

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.

4 Answers 4

2

Why not just do with mysql query

SELECT * FROM phpfox_country 
         ORDER BY IF (countryName !='USA',countryName,'' ) ASC
Sign up to request clarification or add additional context in comments.

Comments

1

Looks like there are some errors in your code. First, by setting $c to 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 $c at 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 called name - if it is not, you can fix this yourself.

<?php
require 'dbconnect.php';
// Create empty array
$message=array();

$query="SELECT * FROM phpfox_country ORDER BY name ASC";
$result=mysql_query($query);

if($result)
{
    while($row=mysql_fetch_row($result))
    {
        if($row[1]=="United States")
        {
            // Add to start of the array
            array_unshift($message, array("country_iso"=>$row[0],"name"=>$row[1]));
        }
        else
        {
            // Add to end of the array
            $message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
        }
    }
}
else
{
    $message=array("message"=>"error in code");
}
echo json_encode($message);
?>

Comments

0

One way is to reserve a place in the beginning of the array:

<?php
require 'dbconnect.php';
$message=array(0 => 'placeholder');

$query="select * from phpfox_country";
$result=mysql_query($query);

if($result)
{
   while($row=mysql_fetch_row($result))
   {

     if($row[1]=="United States")
     {
       $message[0]=array("country_iso"=>$row[0],"name"=>$row[1])
     }
     else
     {
       $message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
     }

     $c++;
   }
}
else
{
  $message=array("message"=>"error in code");
}

echo json_encode($message);
?>

Comments

0

Modify your query and code like this:

<?php
require 'dbconnect.php';

$query = "select * from phpfox_country order by <country_name>='USA' DESC,<country_name>";
$result=mysql_query($query);

if($result)
{
    $message = array();
    while( false!==($row=mysql_fetch_row($result)) )
    {
        $message[] = array("country_iso"=>$row[0],"name"=>$row[1])
    }
}
else
{
    $message=array("message"=>"error in code");
}
echo json_encode($message);
?>

But not forget to replace <country_name> with country's name column

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.