News:

Support the VirtueMart project and become a member

Main Menu

[DEV RESPOND REQUESTED] VM 1.x incorrect state list in registration form

Started by jdoyle, August 27, 2007, 21:16:03 PM

Previous topic - Next topic

jdoyle

Description:
When creatng the state list for the register form in VM1.0.12 and VM 1.5 the state names and the 2 letter code are taken from the database then put into an array.  This is where the bug is created.  The 2 letter code for the state is used as a key and so when states with the same 2 letter code are pulled from the database and inserted in the to the Array the are earlier ones are overwritten. 

For example WA is first in the list as Western Australia but by the time the array is complete it has been re-named Washington and Washington does not appear. 

VirtueMart Version:
1.0.12 and 1.1.

Joomla/Mambo Version:
1.0.12

Steps to replicate:
Demo site shows bug.

Proposed fix(es):
In ps_html.php->list_states()   (Line 180)
Instead of putting the output of the query into an array they could be written directly to the state list <select> options  or the array needs a unique key.


$list[0] = "0,".$VM_LANG->_PHPSHOP_SELECT;
$list[1] = "NONE,not listed";
$country = "";
$i = 2;

while( $db->next_record() ) {
if( $country != $db->f("country_name")) {
$list[$i] = $i . ",------- ".$db->f("country_name")." -------";
$country = $db->f("country_name");
$i++;
}
$list[$i] = $db->f("state_2_code").','.$db->f("state_name");
$i++;
}

$this->state_dropdown_display($list_name, $selected_item, $list,"","",$extra);


with a new drop down function of


function state_dropdown_display($name, $value, &$arr, $size=1, $multiple="", $extra="") {

if( !empty( $arr ) ) {
echo "<select class=\"inputbox\" name=\"$name\" size=\"$size\" $multiple $extra>\n";
while (list($key, $val) = each($arr)) {
list( $state_2_code, $state_name ) = explode(',',$val);
$selected = "";
if( is_array( $value )) {
if( in_array( $state_2_code, $value ) ) {
$selected = "selected=\"selected\"";
}
}
else {
if( strcmp( $value, $state_2_code ) == 0 ) {
$selected = "selected=\"selected\"";
}
              }
                echo "<option value=\"$state_2_code\" $selected>$state_name";
echo "</option>\n";
}
echo "</select>\n";
}
return True;
}


Personally I have changed the ps_html.php->list_states() function to


function list_states($list_name,$selected_item="", $country_id="", $extra="") {
global $VM_LANG, $vmLogger;

$db =& new ps_DB;
$q = "SELECT country_name, state_name, state_3_code , state_2_code
FROM #__{vm}_state s, #__{vm}_country c
WHERE s.country_id = c.country_id ";
if( !empty( $country_id )) {
$q .= " AND c.country_id=".(int)$country_id;
}
$q .= "\nORDER BY country_name, state_name";
$db->query( $q );

echo '<select class="inputbox" name="'.$list_name.'" size="1" "" '.$extra.'>\n';
echo '<option value="0">'.$VM_LANG->_PHPSHOP_SELECT;
echo "</option>\n";
echo '<option value="NONE">not listed';
echo "</option>\n";
$country = "";
$i=0;

while( $db->next_record() ) {
if( strcmp( $selected_item, $state_2_code ) == 0 ) {
$selected = "selected=\"selected\"";
}
if( $country != $db->f("country_name")) {
echo "<option value=\"$i\">------- ".$db->f("country_name")." -------</option>\n";
$i++;
$country = $db->f("country_name");
}
$state_2_code = $db->f("state_2_code");
$state_name = $db->f("state_name");
echo "<option value=\"$state_2_code\" $selected>$state_name";
echo "</option>\n";
}
echo "</select>\n";

return 1;
}


Overall I am not sure why the two letter code is used - the state_id would uniquely identify the state.

Bugtracker task #:
If you want me to create one then I will.

System info:
na

Regards
John