Jump to content

[SOLVED] sort order of states


Recommended Posts

this has bugged me for a while

if i decide to add states to a country, they display fine in the state dropdown, but the order becomes incorrect if you enter new states, it looks like like the display is pulled from the db and sorted by id_state is it possible to change this so it is sorted on name so they alway appear alphabetically


regards


CHris

Link to comment
Share on other sites

You must be using PrestaShop v1.2.5 or earlier, since this bug was fixed in PrestaShop v1.3. The solution was to change lines 81-84 of classes/Country.php from:

$states = Db::getInstance()->ExecuteS('
SELECT s.*
FROM `'._DB_PREFIX_.'state` s
');



to:

$states = Db::getInstance()->ExecuteS('
SELECT s.*
FROM `'._DB_PREFIX_.'state` s
ORDER BY s.`name` ASC');

Link to comment
Share on other sites

nope usinhg 1.3 final on a new site

http://firestriker.co.uk


just add three states to UK

in this order

Kent, Surrey, Berkshire

and the dropdown in authentication.php when creating an account

shows them in the order they were entered

checked in classes/country.php and the code is as stated by Rocky, but its not working in authentication.php



Kent
Surrey
Berkshire


regards


Chris

Link to comment
Share on other sites

just some findings in firefox

script in header shows

[removed]
// <![CDATA[
idSelectedCountry = false;
valueDni = false;
countries = new Array();
countriesNeedIDNumber = new Array();
                                                                                                                                                                                                                                                                                                                                   countries[4] = new Array();
                   countries[4]['56'] = 'Alberta';
                   countries[4]['55'] = 'British Columbia';

Snip ---------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    countriesNeedIDNumber.push(6);
                                                                                                                                                                                                               countries[17] = new Array();
                   countries[17]['68'] = 'BerkShire';
                   countries[17]['66'] = 'Kent';
                   countries[17]['67'] = 'Surrey';


Snip  --------
                   countries[21]['47'] = 'Washington';
                   countries[21]['48'] = 'West Virginia';
                   countries[21]['49'] = 'Wisconsin';
                   countries[21]['50'] = 'Wyoming';
                                                                                                               //]]>
[removed]




so it shows them in the correct order in the array

when it is called into this code it ends up back in id order


State

-


*





just trying to figure out how it gets transposed by firefox, in a matter of fact how does the array get input into the dropdown box is beyond me at the moment


regards


Chris

Link to comment
Share on other sites

It appears to be this part of js/tools/statesManagement.js that populates the state dropdown:

for (indexState in states)
{
   //ie bug fix
   if (indexState != 'indexOf')
       $('select#id_state').append(''+states[indexState]+'');
}



The states seem to be sorted by array index rather than name.

I suggest that you sort the array by name by changing lines 10-20 of authentication.tpl in your theme's directory from:

{foreach from=$countries item='country'}
   {if isset($country.states)}
       countries[{$country.id_country|intval}] = new Array();
       {foreach from=$country.states item='state' name='states'}
           countries[{$country.id_country|intval}]['{$state.id_state|intval}'] = '{$state.name|escape:'htmlall':'UTF-8'}';
       {/foreach}
   {/if}
   {if $country.need_identification_number}
       countriesNeedIDNumber.push({$country.id_country|intval});
   {/if}
{/foreach}



to:

{foreach from=$countries item='country'}
   {if isset($country.states)}
       countries[{$country.id_country|intval}] = new Array();
       {foreach from=$country.states item='state' name='states'}
           countries[{$country.id_country|intval}]['{$state.id_state|intval}'] = '{$state.name|escape:'htmlall':'UTF-8'}';
       {/foreach}
       countries[{$country.id_country|intval}].sort();
   {/if}
   {if $country.need_identification_number}
       countriesNeedIDNumber.push({$country.id_country|intval});
   {/if}
{/foreach}



Hopefully, that will sort the array elements by name instead of country ID.

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 months later...
  • 5 weeks later...
  • 2 years later...

La solución para el problema es tan sencillo como ir a Países, seleccionar el país en cuestión y marcar la opción "Contiene estados".

 

Así podréis seleccionar las provincias españolas, previamente entradas claro.

 

Script para insertar provincias : http://www.prestashop.com/forums/topic/189941-aporte-agregar-provincias-espanolas-en-version-15/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...