I have a some code that is drawing out regions when a country a selected.
I want the regions to order alphabetically, but its not doing it for some reason.
$q=mysql_query("select * from tbl_resorts where Id_show=1 ORDER BY Nom_Rsrt") or die (mysql_error());
while($r=mysql_fetch_row($q))
{
echo "<script type='text/javascript'>";
echo "region[$r[0]]='$r[1]';";
echo "countryregion[$r[0]]='$r[3]';";
echo "</script>";
}
Not sure what the problem is as its working fine, and doing whats asked of it, but its just not being ordered alphabetically
If you order by resort name, the result will be ordered by resort name.
You said you wanted the result ordered by region. If you order by resort name, the result won’t be ordered by region.
Cancun, Majorca, London, Paris, Barcelona, Rome etc.
They are what populate the drop down after the country is selected, and it’s those that I want ordering alphabetically, and thats why its ORDER BY Nom_Rsrt
CREATE TABLE tbl_resorts ( Id_Rsrt bigint(20) NOT NULL auto_increment, Nom_Rsrt text NOT NULL, Desc_Rsrt text NOT NULL, IdCntry_Rsrt bigint(20) NOT NULL default ‘0’, Id_show bigint(20) NOT NULL,
PRIMARY KEY (Id_Rsrt)
) ENGINE=MyISAM AUTO_INCREMENT=347 DEFAULT CHARSET=latin1
Ok, I understand now. The regions are stored in the resort table.
I took a look at the HTML code of your site, and the regions are present in alphabetical order.
The problem I think is, as you can see, that you put them in an array using the Id_Rsrt as array key. So in the end, in the array, they are sorted on Id_Rsrt again.
Try something like
$q=mysql_query("select * from tbl_resorts where Id_show=1 ORDER BY Nom_Rsrt") or die (mysql_error());
$counter = 0;
while($r=mysql_fetch_row($q))
{
echo "<script type='text/javascript'>";
echo "regionid[$counter]='$r[0]';";
echo "region[$counter]='$r[1]';";
echo "countryregion[$counter]='$r[3]';";
echo "</script>";
$counter++;
}
And then adapt your javascript fillregion() function to work with this change as well.
It means that when the value of selectCountry changes (because the user selects a country) the js function fillregion() will be called, passing it the chosen country value.
The fillregion() function is found in this javascript file: indexScript.js
Let me have a try at modifying it:
function fillregion(countryid)
{
var i=0;
document.search.selectRegion.options.length=0;
if(countryid==0 || i==0 ){
document.search.selectRegion.options[0] = new Option()
document.search.selectRegion.options[0].value=0;
//document.search.selectRegion.options[0].innerText="anywhere";
document.search.selectRegion.options[0].text="anywhere";
i++;
}
if(countryid!=0 && i!=0 ){
for(j=0;j<=countryregion.length;j++)
{
if(countryregion[j]==countryid)
{
document.search.selectRegion.options[i] = new Option()
[B][COLOR="#FF0000"]document.search.selectRegion.options[i].value=regionid[j];[/COLOR][/B]
//document.search.selectRegion.options[i].innerText=region[j];
document.search.selectRegion.options[i].text=region[j];
i++;
}
}
}
swap();
}
Try adding this line to the top of indexScript2.js
// JavaScript Document
var country = new Array();
var region = new Array();
var countryregion = new Array();
[COLOR="#FF0000"][B]var regionid = new Array();[/B][/COLOR]