Returning nothing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Returning nothing

Post 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
...
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Returning nothing

Post by Benjamin »

Try:

Code: Select all

 
getCountryName('US') 
 
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Returning nothing

Post by Cirdan »

Thanks, but didn't work.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Returning nothing

Post by Benjamin »

You'll need to print_r() your $this->_countries array to see exactly what's in it and go from there.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Returning nothing

Post 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.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Returning nothing

Post 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.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Returning nothing

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Returning nothing

Post 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.
Post Reply