Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:52:41+00:00 2026-05-29T10:52:41+00:00

hi i have an list of countries on table so i am sorting with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:52:41+00:00Added an answer on May 29, 2026 at 10:52 am

    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);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this drop-down list of countries: <select name=country> <option value=>Country...</option> <option value=Afganistan>Afghanistan</option> <option
I have a list of countries in this format in an external file: <?
I have a list called Countries, and each country has a list of Towns,
I have an app where I need to get the list of countries,languages etc
Say I have a users table and a countries table, something like: tblUsers int
I have a table that is a list of countries, with two fields, ID
I have a database with a list of locations e.g.: C_ID | Name 1
I have a table with a list of countries in it that I'm using
I have a table ( country_table ) with a list of all countries and
Let's say we have a code list of all the countries including their country

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.