• Resolved esilk

    (@esilk)


    I need to sort my result by country NAME and not the Country Code. Right now the list shows Spain and Mexico BEFORE United Kingdom. and I need the country list to be alphabetical by country name.

    Here is my code:

    <div class="forminner">
    <h2>International Dealers</h2>
    <select class="dealers" name="mbn_country" id="mbn_country" onchange="myFunction()">
    <option value="" selected="selected">Select Country</option>
    <?php
    
    $countries = new Pod('dealers');
    $countries ->findRecords(country,-1);
    $total_states = $countries->getTotalRows();
    //initialize data array
    $country_data = array();
    //loop through all records and load into data array
    while ($countries->fetchRecord()) {
    $country = $countries->get_field('country');
    $location_data = array(
    'country'=> $countries->get_field('country')
    );
    
    $location_data = array_unique($location_data);
    $country_data[0][$country][] = $location_data;
    }
    ?>
    <?php foreach ( $country_data[0] as $country => $countries ) : //loop through US locations by state ?>
    <?php
    $countries = array_unique($countries);
    
    foreach ( $countries as $country ) :
    
    if(($country['country'] != "") AND ($country['country'] != "US")) { ?>
    <option value="<?php echo country_code_to_country($country['country']); ?>"><?php echo country_code_to_country($country['country']); ?></option>
    <?php } endforeach; ?>
    <?php endforeach; ?>
    </select>
    </div>

    What am I doing wrong?
    Thanks!

Viewing 15 replies - 1 through 15 (of 15 total)
  • anonymized-13749270

    (@anonymized-13749270)

    asort function should achieve that..

    Can you give this a try and let me know how it goes:

    <div class="forminner">
    <h2>International Dealers</h2>
    <select class="dealers" name="mbn_country" id="mbn_country" onchange="myFunction()">
    <option value="" selected="selected">Select Country</option>
    <?php
    
    $countries = new Pod('dealers');
    $countries ->findRecords(country,-1);
    $total_states = $countries->getTotalRows();
    //initialize data array
    $country_data = array();
    //loop through all records and load into data array
    while ($countries->fetchRecord()) {
    $country = $countries->get_field('country');
    $location_data = array(
    'country'=> $countries->get_field('country')
    );
    
    $location_data = array_unique($location_data);
    $country_data[0][$country][] = $location_data;
    }
    ?>
    <?php foreach ( $country_data[0] as $country => $countries ) : //loop through US locations by state ?>
    <?php
    $countries = array_unique($countries);
    
    $array = array();
    foreach ( $countries as $country ) :
    $array[] = country_code_to_country($country['country']);
    endforeach;
    asort($array);
    
    foreach ( $array as $country ) :
    
    if(($country['country'] != "") AND ($country['country'] != "US")) { ?>
    <option value="<?php echo country_code_to_country($country['country']); ?>"><?php echo country_code_to_country($country['country']); ?></option>
    <?php } endforeach; ?>
    <?php endforeach; ?>
    </select>
    </div>
    Thread Starter esilk

    (@esilk)

    This did not work – it listed the countries in the same wrong order, and only the first letter of each country displayed in the dropdown.

    http://electricsilk.com/dev/loewen/index.php/dealers/

    anonymized-13749270

    (@anonymized-13749270)

    May I know what’s within the array please so I could give it a try?

    insert this code:

    var_dump($countries);

    before this:

    $countries = array_unique($countries);

    And share the debugged text via a pastebin (use pastebin.com)

    Regards.

    Thread Starter esilk

    (@esilk)

    I added the line of code but it did nothing:
    http://electricsilk.com/dev/loewen/index.php/dealers/

    anonymized-13749270

    (@anonymized-13749270)

    The link is missing. Try pasting it without http:// and www. hopefully it will be recognized as plain text..

    Regards.

    Thread Starter esilk

    (@esilk)

    electricsilk dot com/dev/loewen/index.php/dealers/

    Thread Starter esilk

    (@esilk)

    I see now when I look at the source it’s using the two letter code for the country, and Spain is ES and United Kingdom is GB, so that is why they are listed after Estonia and not in the correct place.

    Still not sure how to fix it though…

    Thread Starter esilk

    (@esilk)

    Samuel – here is the pastebin link:

    pastebin dot com/wx8n6Crv

    anonymized-13749270

    (@anonymized-13749270)

    Thanks for the paste bin (the site electricsilk seems to be offline)

    I’ll look into it. In the mean time there’s a solution in JavaScript and jQuery

    anonymized-13749270

    (@anonymized-13749270)

    Back to you. I can’t test it up hope it works for you.

    <div class="forminner">
    	<h2>International Dealers</h2>
    	<select class="dealers" name="mbn_country" id="mbn_country" onchange="myFunction()">
    		<option value="" selected="selected">Select Country</option>
    		<?php
    		$countries = new Pod('dealers');
    		$countries->findRecords(country,-1);
    		$total_states = $countries->getTotalRows();
    		//initialize data array
    		$country_data = array();
    		//loop through all records and load into data array
    		while ($countries->fetchRecord()) {
    			$country = $countries->get_field('country');
    			$location_data = array(
    				'country' => $countries->get_field('country')
    			);
    			$location_data = array_unique($location_data);
    			$country_data[0][$country][] = $location_data;
    		}
    		?>
    		<?php foreach ( $country_data[0] as $country => $countries ) : //loop through US locations by state ?>
    			<?php
    			$countries = array_unique($countries);
    			$newArray = array();
    			foreach ( $countries as $country ) :
    				if(($country['country'] != "") AND ($country['country'] != "US")) {
    					$newArray[] = country_code_to_country($country['country']);
    				}
    			endforeach; ?>
    		<?php endforeach; ?>
    		<?php
    		arsort($newArray);
    		foreach ( $newArray as $name ) {
    			echo "<option value=\"$name\">$name</option>";
    		}
    		?>
    	</select>
    </div>
    Thread Starter esilk

    (@esilk)

    Electric Silk Dev Page

    Actually the FTP failed and when the file is replaced, there is nothing in the list…

    anonymized-13749270

    (@anonymized-13749270)

    That’s weird.

    Better try this alt solution http://pastebin.com/3aCU0HYz

    Thread Starter esilk

    (@esilk)

    Adding the javascript fixed the sort order for countries, but now the Select Country option is down the list instead of first:
    http://electricsilk.com/dev/loewen/index.php/dealers/

    anonymized-13749270

    (@anonymized-13749270)

    Make it -- Select Country -- instead of Select Country and it should be kept on top.

    Thread Starter esilk

    (@esilk)

    Awesome Samuel – that works perfect. Thanks so much!

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Sort array by Country Name not Country Code’ is closed to new replies.