Page 1 of 1

Returning nothing

Posted: Wed Feb 04, 2009 10:29 am
by Cirdan
When I call the view helper's getCountryName('us') it doesn't return anything. No errors or anything. I'm loading the countries from a file that looks like this:

Code: Select all

<?php
 
class Redeemed_View_Helper_GetCountryName extends Zend_View_Helper_Abstract
{
    public function getCountryName($code)
    {
        return Redeemed_Countries::getInstance()->getCountryName($code);
    }
}

Code: Select all

<?php
 
class Redeemed_Countries
{
    protected $_countries;
    protected $_path;
    private static $_instance;
    
    public function getCountryName($code)
    {
        $countries = $this->getCountries();
 
        return $countries[$code];
    }
    
    public function getCountries()
    {
        if(empty($this->_countries))
        {
            $file = fopen('countries.txt', 'r');
            
            if(!$file)
            
            while(!feof($file))
            {
                $line = fread($file);
                
                list($name, $code) = split(';', $line);
                
                $this->_countries[$code] = $name;
            }
            
            fclose($file);
        }
        return $this->_countries;
 
    }
    
    public static function getInstance()
    {
        if(!is_object(self::$_instance))
            self::$_instance = new Redeemed_Countries();
            
        return self::$_instance;
    }
    
    private function __construct()
    { }
    
    private function __clone()
    { }
    
}
File looks like this:

Code: Select all

Afghanistan;AF
Aland Islands;AX
Albania;AL
Algeria;DZ
...
 

Re: Returning nothing

Posted: Wed Feb 04, 2009 11:59 am
by Benjamin
Try:

Code: Select all

 
getCountryName('US') 
 

Re: Returning nothing

Posted: Wed Feb 04, 2009 12:01 pm
by Cirdan
Thanks, but didn't work.

Re: Returning nothing

Posted: Wed Feb 04, 2009 12:05 pm
by Benjamin
You'll need to print_r() your $this->_countries array to see exactly what's in it and go from there.

Re: Returning nothing

Posted: Wed Feb 04, 2009 12:15 pm
by Cirdan
I had an error in the function that loads the list. if(!$file) didn't have anything after it. So I put die('cant load countries.txt'); Now instead of displaying the page, PHP stops working and displays a white page. No errors or anything.

Re: Returning nothing

Posted: Sat Feb 07, 2009 12:19 pm
by Cirdan
Okay I did some poking around, the problem is that I was using fread instead of fgets. Unfortuantly now, it returns the error 'undefined index: US'. Here is what print_r on the array returns.

Code: Select all

Array
(
    [AF
] => Afghanistan
    [AX
] => Aland Islands
...
    [US
] => United States
...
)
 
I have no idea why it is saying the index can't be found when it is there.

Re: Returning nothing

Posted: Sun Feb 08, 2009 12:07 am
by Cirdan
:banghead: Ugh. Okay after enough poking around and pulling my hair out, the problem was the country codes had a newline character due to being at the end of each line in the file. So I solved the problem by trim()ing the codes.

I guess the question now is: is there any benefit to putting this in a text file instead of the DB? I did it because I figured it would save some resources by not having to query the db.

Re: Returning nothing

Posted: Sun Feb 08, 2009 1:47 am
by requinix
Cirdan wrote:I guess the question now is: is there any benefit to putting this in a text file instead of the DB?
Not really. Instead of querying a database you're reading from a file. Advantage here, disadvantage there. It balances out.