0

I'm trying to display zipcode which only logged in user's area.

To get user's area, get_zipcode() calls get_area_name().

But, there is 500 error if I get value from get_area_name();

If I commented out get_area_name() and put harded coded value $area ="ny", it works.

I think there is some issue with get_area_name().

table for "area" is following

CREATE TABLE IF NOT EXISTS `wn_area` (
  `area_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `area` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`area_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=165 ;

Model:

public function get_zipcode($area_id)
{
    $this->db->select("*");
    $this->db->from("zipcode");
    if ($area_id != "" ) {
        $ar_area_ids = array(1, 2, 3, 4, 5, 6, 7, 8);
        //$area = get_area_name($area_id);
        $area = "ny";
        if (in_array($area_id, $ar_area_ids)) {
            $this->db->like('sido', $area); 
        } else {
            $this->db->like('gugun', $area);
        }
    } else {
        $this->db->like("sido", 'ny'); 
        $this->db->limit("10000");
    }
    $this->db->order_by("zipcode_id");
    $query = $this->db->get(); 
    return ($query->num_rows() > 0) ? $query->result() : false;
}

public function get_area_name($area_id)
{
    $this->db->select("*");
    $this->db->from("area");
    $this->db->where("area_id", $area_id);
    $query = $this->db->get();
    return $query->row()->area;
}

2 Answers 2

1

since its class to call method from another method inside class , you need to use $this, like, change:

$area = get_area_name($area_id);

to

$area = $this->get_area_name($area_id);
Sign up to request clarification or add additional context in comments.

1 Comment

Great. But another issue with utf-8. It returns broken characters. I checked db config and html header. They area "utf=8". Is there any way return value to set utf-8 for get_area_name
0

get_area_name is not a "function", it is a "method". This means you must acknowledge its relationship to the instanciated class object with $this. Also, it is probably better practice to create a white list of city ids as a private property of the class or maybe even a constant (instead of harding coding magic numbers directly into a method body.

It might be possible to refactor your zipcode-fetching method so that there is only 1 trip to the database, but the fact that you are using LIKE to hunt for qualifying rows makes me fundamentally suspicious of your database design. Short of a deeper investigation, you can simplify the conditional aspects of your script which will simplify the query building process.

In getZipcode(), I advise against returning false from a method that is ordinarily expected to return an array. In this case, I recommend returning an array of zero or more objects with result().

In getAreaName(), I recommend returning the qualifying string or null. This is automatically handled properly if you feed the column name to row(). Note, I am not a fan of using area as a column name in a table by the same name. Maybe the columns of area should have been id and name.

private array $cityIds = [1, 2, 3, 4, 5, 6, 7, 8];
//------------^^^^^^^^

public function getAreaName(int $areaId): ?string
//--------------^^^^^^^^^^^
{
    return $this->db->get_where('area', ['area_id' => $areaId])->row('area');
}


public function getZipcode(?int $areaId): array
{
    if ($areaId) {
        $areaName = $this->getAreaName($areaId);
        //----------^^^^^^^^^^^^^^^^^^
        if (!$areaName) {
            throw new Exception('Invalid/Unrecognized area id');
        }
        $searchColumn = in_array($areaId, $this->cityIds) ? 'city' : 'county';
        //--------------------------------^^^^^^^^^^^^^^
        $limit = null;
    } else {
        $areaName = 'ny';
        $searchColumn = 'city';
        $limit = 10_000;
    }
    return $this->db
        ->like($searchColumn, $areaName)
        ->order_by('zipcode_id')
        ->get('zipcode', $limit)
        ->result();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.