Setting default country in countries drop down
#21
Posted 18 June 2007, 00:11
-Thanks
#22
Posted 27 June 2007, 15:38
-Thanks
#23
Posted 06 July 2008, 18:27
efficiondave, on Apr 8 2004, 07:40 AM, said:
<td class="main"><?php echo tep_get_country_list('country', $entry['entry_country_id']) . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
to:
<td class="main"><?php echo (($entry['entry_country_id'] == '') ? tep_get_country_list('country', 223) : tep_get_country_list('country', $entry['entry_country_id'])) . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
so, this will make the address book to display selected country.. right?
"Lets learn about osCommerce together!..."
#24
Posted 22 July 2008, 03:45
- Check countries table in db to find the id for your country (or just view the source of the dropdown). In my below example, I'm using 13 for Australia.
- Edit includes/languages/english.php, add the following anywhere you like:
define('DEFAULT_COUNTRY_ID', 13); // 13 = Australia
If you were using multiple languages, you'd set the country for each one. - includes/functions/html_output.php:
function tep_get_country_list($name, $selected = DEFAULT_COUNTRY_ID, $parameters = '') {
Done. Using $selected = DEFAULT_COUNTRY_ID in the function arguments is a PHP language feature which defaults the variable to DEFAULT_COUNTRY_ID if none is already set.
Concise and simple. Enjoy.
Edited by scottos, 22 July 2008, 03:47.
#25
Posted 14 August 2009, 01:34
Silverado05, on Jun 18 2007, 12:11 AM, said:
-Thanks
I will really appreciate if you can give me step by step instructions of how to delete the drop down box for the country and hard code text. If possible let me know how to delete the text boxes for city and state also and hard code it to display a specific country/city/state.
Thanx in advance
#26
Posted 15 August 2009, 04:43
quiznosnow, on Aug 13 2009, 09:34 PM, said:
<tr>
<td class="main"><?php echo ENTRY_CITY; ?></td>
<td class="main"><?php echo tep_draw_input_field('city') . ' ' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
</tr>
<?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
if ($process == true) {
if ($entry_state_has_zones == true) {
$zones_array = array();
$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
while ($zones_values = tep_db_fetch_array($zones_query)) {
$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
}
echo tep_draw_pull_down_menu('state', $zones_array);
} else {
echo tep_draw_input_field('state');
}
} else {
echo tep_draw_input_field('state');
}
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
</td>
</tr>
<?php
}
?>
<tr>
<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
<td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
</tr>If all that you want is to replace this with hard coded inputs, it would look like <tr>
<td class="main"><?php echo ENTRY_CITY; ?></td>
<td class="main"><?php echo tep_draw_hidden_field('city', 'Toronto') . 'Toronto' . ' ' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
</tr>
<?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
echo tep_draw_hidden_field('state', 'Ontario') . 'Ontario';
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
</td>
</tr>
<?php
}
?>
<tr>
<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
<td class="main"><?php echo tep_draw_hidden_field('country', '38') . 'Canada' . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
</tr>The '38' is the code for Canada. I got that by looking at the HTML for the select box: <option value="38">Canada</option>The United States is 223 and the UK is 222. You could also get these values by looking in the countries table in the database.
#27
Posted 18 August 2009, 23:59
btw How do you know that I want the city/state/country to be toronto/ontario/canada?? I'm curious..
Thnx again
tc
#29
Posted 20 August 2009, 00:24
#30
Posted 21 August 2009, 23:40
#31
Posted 23 August 2009, 03:30
DasMax, on Aug 21 2009, 07:40 PM, said:
//--></script>add
<?php
$countries_with_zones = array();
$countries_with_zones_query = tep_db_query("SELECT c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
}
?>
function displayStateInputs(value) {
var state_input = document.getElementById('state');
<?php
foreach ($countries_with_zones as $country_with_zone_code) {
?>
var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementById('<?php echo $country_with_zone_code; ?>_states');
<?php echo $country_with_zone_code; ?>_states_menu.style.display = "none";
<?php
}
?>
switch (value) {
<?php
foreach ($countries_with_zones as $country_with_zone_code) {
?>
case '<?php echo $country_with_zone_code; ?>':
<?php echo $country_with_zone_code; ?>_states_menu.style.display = "";
state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
state_input.style.display = "none";
break;
<?php
}
?>
default:
state_input.value = '';
state_input.style.display = "";
}
}
function updateState(value) {
document.getElementById('state').value = value;
}In create_account.php (around line 18), before $process = false;
if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
$process = true;add $state = '';Note that you could set this to a particular state if you want your drop down to default to a particular state.
In create_account.php, around line 263, change
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">to
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('US')">Replace US with another country's two character ISO code as appropriate or set to an empty string (two single quotes with nothing between them) to default to showing a text box rather than a select menu. This value should correspond with the value that you set for your countries drop down menu default (leave empty if you are not setting a default for your countries drop down). Around lines 411 to 439, change
<?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
if ($process == true) {
if ($entry_state_has_zones == true) {
$zones_array = array();
$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
while ($zones_values = tep_db_fetch_array($zones_query)) {
$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
}
echo tep_draw_pull_down_menu('state', $zones_array);
} else {
echo tep_draw_input_field('state');
}
} else {
echo tep_draw_input_field('state');
}
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
</td>
</tr>
<?php
}
?>to <?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
$zones_array = array();
$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
while ($zone_option = tep_db_fetch_array($select_zones_query)) {
$country_with_zone_code = $zone_option['countries_iso_code_2'];
if (!is_array($zones_array[$country_with_zone_code]) {
$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
}
$zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
}
foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="displayStateInputs(this.value);"');
}
echo tep_draw_input_field('state', '', 'onchange="updateState(this.value)"');
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
</td>
</tr>
<?php
}
?>and in includes/languages/english/create_account.php (or whatever language), add define('PLEASE_SELECT_A_STATE', 'Please select a state');Note: I haven't tested this. Backup before trying.
#32
Posted 23 August 2009, 04:27
Line 435 - if (!is_array($zones_array[$country_with_zone_code]) {
This is the error I get.
#33
Posted 23 August 2009, 04:39
#34
Posted 23 August 2009, 19:10
In a stock osCommerce, in includes/form_check.js.php, just before the
//--></script>add
<?php
$countries_with_zones = array();
$country_id_for = array();
$countries_with_zones_query = tep_db_query("SELECT c.countries_id, c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
$country_id_for[$country_with_zone['countries_iso_code_2']] = $country_with_zone['countries_id'];
}
?>
function displayStateInputs(value) {
var state_input = document.getElementsByName('state')[0];
<?php
foreach ($countries_with_zones as $country_with_zone_code) {
?>
var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementsByName('<?php echo $country_with_zone_code; ?>_state')[0];
<?php echo $country_with_zone_code; ?>_states_menu.style.display = 'none';
<?php
}
?>
switch (value) {
<?php
foreach ($countries_with_zones as $country_with_zone_code) {
?>
case '<?php echo $country_id_for[$country_with_zone_code]; ?>':
<?php echo $country_with_zone_code; ?>_states_menu.style.display = '';
state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
state_input.style.display = 'none';
break;
<?php
}
?>
default:
state_input.value = '';
state_input.style.display = '';
}
}
function updateState(value) {
document.getElementsByName('state')[0].value = value;
}In create_account.php (around line 18), before $process = false;
if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
$process = true;add $state = ''; $country = '223';Note that you could set $state to a particular state (e.g. $state = 'Alabama'
In create_account.php, around line 263, change
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">to
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('<?php echo $country; ?>')">Around lines 411 to 443, change <?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
if ($process == true) {
if ($entry_state_has_zones == true) {
$zones_array = array();
$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
while ($zones_values = tep_db_fetch_array($zones_query)) {
$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
}
echo tep_draw_pull_down_menu('state', $zones_array);
} else {
echo tep_draw_input_field('state');
}
} else {
echo tep_draw_input_field('state');
}
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
</td>
</tr>
<?php
}
?>
<tr>
<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
<td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
</tr>to <?php
if (ACCOUNT_STATE == 'true') {
?>
<tr>
<td class="main"><?php echo ENTRY_STATE; ?></td>
<td class="main">
<?php
$zones_array = array();
$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
while ($zone_option = tep_db_fetch_array($select_zones_query)) {
$country_with_zone_code = $zone_option['countries_iso_code_2'];
if (!is_array($zones_array[$country_with_zone_code])) {
$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
}
$zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
}
foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="updateState(this.value)" style="display:none"');
}
echo tep_draw_input_field('state');
if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
</td>
</tr>
<?php
}
?>
<tr>
<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
<td class="main"><?php echo tep_get_country_list('country', $country, 'onchange="displayStateInputs(this.value)"') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
</tr>and in includes/languages/english/create_account.php (or whatever language), add define('PLEASE_SELECT_A_STATE', 'Please select a state');You also need to have the charset set appropriately for the various states you will show. UTF-8 might work, although the osCommerce javascript may then fail in a browser using Shift-JIS (Japanese encoding). Backup before trying.
Edited by ecartz, 23 August 2009, 19:20.
#35
Posted 24 August 2009, 17:56
Are you going to put it into contribution format?
#36
Posted 23 May 2011, 19:18
dontera, on 26 March 2004, 21:36, said:
I tried various ways to output a sorted countries list, changing the country I wanted at top to be ID 0 and whatnot, didnt work. Finally I relaized I was overlooking the obvious of using <option .... selected>
Thankfully, the requisite functions have built-in "selected" handling, thus setting your default country is easy.
Edit create_account.php and find
<tr>
<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
<td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
</tr>
you want to change this line
php echo tep_get_country_list('country') .
to bephp echo tep_get_country_list('country','223') .
223 == the country ID for United States. YOu can set that to be any country by looking either in the database countries list for your countries ID, or even easier, look at the HTML of the create_account.php page and find where the country drop down list is, search for your country and grab the "value".Hope this helps.
Wow! Thank you. Exactly what I needed and provided a perfect fix. Now I can move on to the next project lol
#37
Posted 08 April 2012, 17:00
you can now find the country number in the database table named countries!














