Jump to content
Sign in to follow this  
Juto

DHTML State Selection, found a bug

Recommended Posts

Hi! First of, thanks for a great contribution. Sorry to say, the support thread for it is not active.

So, I am asking for help from the community. I have tried for days to fix this, but couldn't.

It's either a logical or a code issue or both. It fills up my error log with "Undefined index at line ..."

 

Any help is most welcome.

Many thanks in advance.

 

Sara

<?php
   $select_zones_query = tep_db_query("SELECT c.countries_id, 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 this is the first time that we've seen this country,

  if ( !is_array($zones_array[$country_with_zone_code]) ) { // <---------Undefined index: AT, CA, DE etc.

echo $country_with_zone_code; // Gives  ATCADEESSECHUS ... a list of country codes

    // initialize an array for it with a blank default value
    $zones_array[$country_with_zone_code]
	  = array(array('id'   => '',
				    'text' => PLEASE_SELECT_A_STATE));
    // save the country code to id mapping for use with the javascript later
    $country_id_for[$country_with_zone_code] = $zone_option['countries_id'];
  }

  $zones_array[$country_with_zone_code][] = array('id'   => $zone_option['zone_name'],
												  'text' => $zone_option['zone_name']);
   }
?>

Share this post


Link to post
Share on other sites

Very strange. I'm not familiar with this add-on, but do have some comments. The while loop assigning $zone_option should be returning one row out of the "countries" table. Unless this add-on has radically modified the table, it should have fields 'countries_id', 'countries_name', 'countries_iso_code_2', 'countries_iso_code_3', and 'address_format_id'. The fields 'countries_id' and 'countries_iso_code_2' should be elements of $zone_option, along with 'zone_name' from table zones. An example would be '14' and 'AT', along with 'Wien', 'Tirol', and several other Austrian provinces/states. Some sort of default JOIN must be happening here, to get '14','AT','Wien' in one record and '14','AT','Tirol' in another.

 

I wonder if this code was written for MySQL 4, and you're running MySQL 5? Just for giggles, try rewriting the query from

SELECT c.countries_id, 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

to

SELECT c.countries_id, c.countries_iso_code_2, z.zone_name FROM countries c LEFT JOIN zones z ON (c.countries_id = z.zone_country_id) ORDER BY z.zone_country_id, z.zone_name

or possibly an INNER JOIN or just a plain JOIN might be the ticket.

 

That's all I can think of at this point, because I think that $country_with_zone_code should contain just one string, e.g., 'AT'.

 

I don't know where $zones_array is coming from (possibly tep_get_country_zones()), but as this code is written, it should have an element named 'AT', and $zones_array['AT'] should itself be an array with elements 'id' and 'text'. Can you investigate these things? Anyway, even if that doesn't turn out to be the case, maybe it will give you a useful hint or two.

Share this post


Link to post
Share on other sites

Hi Phil, thanks for your help. It's most welcome, as always. I have changed the query, tested left join, inner join and just join with no luck.

I have a better view of the bug now:

 

//    $select_zones_query = tep_db_query("SELECT c.countries_id, 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");
   $select_zones_query = tep_db_query("SELECT c.countries_id, c.countries_iso_code_2, z.zone_name FROM countries c INNER JOIN zones z ON (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'];
echo ' echo 1: '.$country_with_zone_code;
  // if this is the first time that we've seen this country,
  if ( !is_array($zones_array[$country_with_zone_code]) ) { //Here's the Undefined index: AT
echo ' echo 2: '.$country_with_zone_code;// echo's AT, then echo 1 starts over and echoe's
/* The echo's gives:
echo 1: AT echo 2: AT echo 1: AT echo 1: AT echo 1: AT echo 1: AT echo 1: AT echo 1: AT echo 1: AT echo 1: AT
echo 1: CA echo 2: CA echo 1: CA echo 1: CA echo 1: CA echo 1: CA echo 1: CA and so on... 
default country
Remark: Is there a difference when the index is a string, and not an integer?
*/
    // initialize an array for it with a blank default value
    $zones_array[$country_with_zone_code]
	  = array(array('id'   => '',
				    'text' => PLEASE_SELECT_A_STATE));
    // save the country code to id mapping for use with the javascript later
    $country_id_for[$country_with_zone_code] = $zone_option['countries_id'];
  }
  $zones_array[$country_with_zone_code][] = array('id'   => $zone_option['zone_name'],
												  'text' => $zone_option['zone_name']);
   }

As you can see the $country_with_zone_code contains just one string, as you said. And yes, $zones_array does contain id and text, that is the drop downs are populated. The country drop down starts with the store country as default, with its zones. This was part of the install instructions. Also, I have checked the installation and it's ok. The addon is here: http://addons.oscommerce.com/info/6975

 

 

I am puzzled to say the least.

 

Kindest regards

 

Sara

Share this post


Link to post
Share on other sites
Is there a difference when the index is a string, and not an integer?

Yes, an array whose index is a string is an "associative array", while other arrays use numeric indexing (0, 1, 2,...). They're implemented differently internally, and you can't mix indexing: a given array is all associative or all numeric.

 

OK, your $country_with_zone_code looks OK (you should get several instances of each, including 50 or more for the USA).

 

if ( !is_array($zones_array[$country_with_zone_code]) ) {

Are you still getting an "undefined index" here? Maybe the code was written for a more lenient version of PHP, and newer versions are stricter about undefined indexes. Try

if ( !isset($zones_array[$country_with_zone_code]) || !is_array($zones_array[$country_with_zone_code]) ) {

 

You can back out the query changes, if you want, if they made no difference.

Share this post


Link to post
Share on other sites

BINGO! Errors gone... wow, what an expert you are! :)

 

I think I will keep your query with inner join... I feel more comfortable with it.

 

If you were near by, I would serve you an excellent dinner.. :)

 

Many thanks and Kind Regards

 

Sara

Share this post


Link to post
Share on other sites

BINGO! Errors gone... wow, what an expert you are! :)

Ah, shucks! Blush, blush. Good to hear that!

 

I think I will keep your query with inner join... I feel more comfortable with it.

That shouldn't cause any problems.

 

If you were near by, I would serve you an excellent dinner.. :)

How far from upstate New York? You're in the UK? That's a bit far for dinner...

 

Many thanks and Kind Regards

 

Sara

Thanks, and best wishes!

Share this post


Link to post
Share on other sites

Hi, Phil!

O' yes it would be quite a trip for a dinner... I live in Sweden, i.e "utopia", as the saying goes. :)

 

Sorry for my little late answer, I am presently cleaning up and restructure the frontends stylesheet...

Thus creating a mess out of chaos... !

 

Kind regards

Sara

Share this post


Link to post
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
Sign in to follow this  

×