Click to See Complete Forum and Search --> : Looped results return garbled display?


lilqhgal
03-31-2006, 01:59 PM
I have a form where the user clicks to view results in order by US state OR by international (non-US states). Right now, the results are displayed properly if the view-by-state selection is chosen. If the international selection is chosen, it displayes the results twice and the format is messed up. Here's the code:

<?php
if(isset($_POST['view_all']))
{
switch($_POST['view_all'])
{
case "state_asc":
$query = tep_db_query("SELECT * FROM `vendors` ORDER BY `vendors_state` ASC");
break;
case "international_asc":
$query = tep_db_query("SELECT * FROM `vendors` WHERE `vendors_country_id` != '223' ORDER BY `vendors_company` ASC");
break;
default:
$query = tep_db_query("SELECT * FROM `vendors`");
}
# replace the following echo with your actual query and output code:


$vendor_query = $query;
# loop through each row returned by query
while($vendor = mysql_fetch_object($vendor_query))
{
$string_vendor = '';

if (strlen($vendor->vendors_company)>0) {
if ($string_vendor!='') $string_vendor .= "<BR>";
$string_vendor .= "<b>" . $vendor->vendors_company . "</b>";
}

if (strlen($vendor->vendors_firstname)>0) {
if ($string_vendor!='') $string_vendor .= "<BR>";
$string_vendor .= "<b>" . $vendor->vendors_firstname . "</b> ";
}

if (strlen($vendor->vendors_lastname)>0) {
if ($string_vendor!='' and !(strlen($vendor->vendors_firstname)>0)) $string_vendor .= "<BR>";
$string_vendor .= "<b>" . $vendor->vendors_lastname . "</b>";
}

if (strlen($vendor->vendors_street_address)>0) {
if ($string_vendor!='') $string_vendor .= "<BR>";
$string_vendor .= $vendor->vendors_street_address;
}

if (strlen($vendor->vendors_suburb)>0) {
if ($string_vendor!='') $string_vendor .= "<BR>sub";
$string_vendor .= $vendor->vendors_suburb;
}

if (strlen($vendor->vendors_city)>0) {
if ($string_vendor!='') $string_vendor .= "<BR>";
$string_vendor .= $vendor->vendors_city;
}

if (strlen($vendor->vendors_zone_id)>0) {
if ($string_vendor!='' and !(strlen($vendor->vendors_city)>0)) $string_vendor .= "";
if (strlen($vendor->vendors_city)>0) $string_vendor .= ', ';
$string_vendor .= tep_get_zone_code(223, $vendor->vendors_zone_id, $string_vendor) . ' ';
}

if (strlen($vendor->vendors_postcode)>0) {
$string_vendor .= $vendor->vendors_postcode;
}

if (strlen($vendor->vendors_telephone)>0) {
$string_vendor .= '<BR> ' . $vendor->vendors_telephone;
}

if (strlen($vendor->vendors_url)>0) {
$string_vendor .= "<BR><a href='" . $vendor->vendors_url ."' target='_blank'>" . $vendor->vendors_url . "</a>";
}

echo $string_vendor . "<br><br>";
}


}
?>

Now I'm not exactly sure how the zone id thing works, but that obviously has something to do with it. What I DO know is that all us based states are zone 223, and anything else would be considered international. So maybe some kind of if or not statement? But I'm not sure how to get it to work. Any suggestions are much appreciated. Thanks!

chazzy
03-31-2006, 02:32 PM
it looks like you're missing a bracket here.

if (strlen($vendor->vendors_zone_id)>0) {
if ($string_vendor!='' and !(strlen($vendor->vendors_city)>0)) $string_vendor .= "";
if (strlen($vendor->vendors_city)>0) $string_vendor .= ', ';
$string_vendor .= tep_get_zone_code(223, $vendor->vendors_zone_id, $string_vendor) . ' ';
}

lilqhgal
03-31-2006, 02:45 PM
I don't see what bracket you're referring to...

chazzy
03-31-2006, 03:05 PM
sorry read the code again.

you're not too detailed on your problem.

if you look here:


if (strlen($vendor->vendors_zone_id)>0) {
if ($string_vendor!='' and !(strlen($vendor->vendors_city)>0)) $string_vendor .= "";
if (strlen($vendor->vendors_city)>0) $string_vendor .= ', ';
$string_vendor .= tep_get_zone_code(223, $vendor->vendors_zone_id, $string_vendor) . ' ';
}

you're checking vendors_city inside of vendors_zone_id, not sure why. it's also the only one where you have logic like this.

Can you explain what tep_db_query does exactly?

Do you do any error checking or anything?

lilqhgal
03-31-2006, 03:19 PM
chazzy, thanks for that better explination. :) I see where it's checking the city inside the zone_id, and as I didn't write this code I'm not sure either. (I'm just trying to troubleshoot it and get it to display properly.) What I figure though is usually when you display an address, you display the city and state at the same time? (my closest guess as to why it's doing that). However, when I try removing that and just having the following, no state code is shown at all:
if (strlen($vendor->vendors_zone_id)>0) {
$string_vendor .= $vendor->vendors_zone_id;
}

chazzy
03-31-2006, 03:21 PM
see my last comment, i editted it right as you were posting.

lilqhgal
03-31-2006, 03:33 PM
ahHA! Well I'm a complete moron. Apparently the vendors_zone_id refers to the STATE. The number 223 is the venders_country_id. So I tried wrapping it in an if statement but that didn't work:

if ($vender_country_id = 223) {
if (strlen($vendor->vendors_zone_id)>0) {
if ($string_vendor!='' and !(strlen($vendor->vendors_city)>0)) $string_vendor .= "";
if (strlen($vendor->vendors_city)>0) $string_vendor .= ', ';
$string_vendor .= tep_get_zone_code(223, $vendor->vendors_zone_id, $string_vendor) . ' ';
}
}

Chazzy, here's the function for tep_db_query:
function tep_db_query($query, $link = 'db_link') {
global $$link, $logger;

if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
if (!is_object($logger)) $logger = new logger;
$logger->write($query, 'QUERY');
}

$result = mysql_query($query, $$link) or tep_db_error($query, mysql_errno(), mysql_error());

if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
if (mysql_error()) $logger->write(mysql_error(), 'ERROR');
}

return $result;
}

and also, out of curiosity, the function for tep_get_zone_code:
function tep_get_zone_code($country, $zone, $def_state) {

$state_prov_query = tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and zone_id = '" . (int)$zone . "'");

if (!tep_db_num_rows($state_prov_query)) {
$state_prov_code = $def_state;
}
else {
$state_prov_values = tep_db_fetch_array($state_prov_query);
$state_prov_code = $state_prov_values['zone_code'];
}

return $state_prov_code;
}

As far as error checking goes, I'm ashamed to say I don't. This wasn't my script to begin with so I wouldn't even know where to start!

lilqhgal
04-04-2006, 01:12 PM
I'm still having trouble with this. I tried wrapping the whole code in an if statement (if country code == 223) and that didn't work. I dont know what to do and my client is all over me to fix this! I've tried contacting the script's original writer and he's MIA. Can anybody help?

lilqhgal
04-04-2006, 01:33 PM
I also found this in the functions file:
function tep_address_format($address_format_id, $address, $html, $boln, $eoln) {
$address_format_query = tep_db_query("select address_format as format from " . TABLE_ADDRESS_FORMAT . " where address_format_id = '" . (int)$address_format_id . "'");
$address_format = tep_db_fetch_array($address_format_query);

$company = tep_output_string_protected($address['company']);
if (isset($address['firstname']) && tep_not_null($address['firstname'])) {
$firstname = tep_output_string_protected($address['firstname']);
$lastname = tep_output_string_protected($address['lastname']);
} elseif (isset($address['name']) && tep_not_null($address['name'])) {
$firstname = tep_output_string_protected($address['name']);
$lastname = '';
} else {
$firstname = '';
$lastname = '';
}
$street = tep_output_string_protected($address['street_address']);
$suburb = tep_output_string_protected($address['suburb']);
$city = tep_output_string_protected($address['city']);
$state = tep_output_string_protected($address['state']);
if (isset($address['country_id']) && tep_not_null($address['country_id'])) {
$country = tep_get_country_name($address['country_id']);

if (isset($address['zone_id']) && tep_not_null($address['zone_id'])) {
$state = tep_get_zone_code($address['country_id'], $address['zone_id'], $state);
}
} elseif (isset($address['country']) && tep_not_null($address['country'])) {
$country = tep_output_string_protected($address['country']);
} else {
$country = '';
}
$postcode = tep_output_string_protected($address['postcode']);
$zip = $postcode;

if ($html) {
// HTML Mode
$HR = '<hr>';
$hr = '<hr>';
if ( ($boln == '') && ($eoln == "\n") ) { // Values not specified, use rational defaults
$CR = '<br>';
$cr = '<br>';
$eoln = $cr;
} else { // Use values supplied
$CR = $eoln . $boln;
$cr = $CR;
}
} else {
// Text Mode
$CR = $eoln;
$cr = $CR;
$HR = '----------------------------------------';
$hr = '----------------------------------------';
}

$statecomma = '';
$streets = $street;
if ($suburb != '') $streets = $street . $cr . $suburb;
if ($country == '') $country = tep_output_string_protected($address['country']);
if ($state != '') $statecomma = $state . ', ';

$fmt = $address_format['format'];
eval("\$address = \"$fmt\";");

if ( (ACCOUNT_COMPANY == 'true') && (tep_not_null($company)) ) {
$address = $company . $cr . $address;
}

return $address;
} (Not that I know what any of that means, but I'm sure it has something to do with the formatted address! When viewing US addresses, they look like this:
Company Name
Owner First & Last Name
123 Anywhere St.
City, ST 55555
888-555-1234
http://www.website.com/
But when you view the international addresses, they look like this:
Company
Street 123
City, Company
Street 123
City, 555 55 (postal code)
+55 555 5555 (phone)
http://www.website.com/Note how it repeats the company name and stuff. I just need it to display properly!