PHP Model not returning result

Vince Kronlein

I have a recursive model function as such:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}

Passing in a value of 40 for instance should return the following:

3_40

When I echo out the variable $path within the model it does display the correct value, but when I call the function via my controller ie:

$result = $this->model_catalog_category->buildPaths(40);

$result returns empty.

Any ideas on why this would be happening?

Vince Kronlein

Thanks to andrewsi for this answer:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        return $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHP strtotime returning the wrong result on calculation

분류에서Dev

Timediff in mysql not returning result

분류에서Dev

Registration result keeps returning false

분류에서Dev

PHP function not returning value

분류에서Dev

If date > curDate returning wrong result, WHY?

분류에서Dev

IN query not returning any result, worklight SQL Adapter

분류에서Dev

Why is findAll() not returning all objects in model?

분류에서Dev

Returning an error response in the case of a null model

분류에서Dev

Returning single item in model - mvc razor

분류에서Dev

PHP subtract a percentage result

분류에서Dev

Returning a PHP response in a AJAX query

분류에서Dev

free_result() with MY_Model

분류에서Dev

Unexpected result from PHP request

분류에서Dev

Unexpected result from PHP request

분류에서Dev

Django views returning only one particular field instead of entire model

분류에서Dev

PHP OOP checking if a class is returning true or false?

분류에서Dev

PHP OOP checking if a class is returning true or false?

분류에서Dev

PHP DateInterval not returning last day of the month

분류에서Dev

php - MySQli row count always returning 0?

분류에서Dev

Ajax JSON PHP Stringify not posting but returning successfully

분류에서Dev

PHP Session is lost on smartphones when returning to browser

분류에서Dev

PHP simple XML parser not returning data

분류에서Dev

Why is PHP if(!$_SESSION['username']) is returning false

분류에서Dev

PHP Contact Form returning error upon submission

분류에서Dev

correcting and fix explode and sort to show the result in PHP

분류에서Dev

ajax GET - result from php not passing through

분류에서Dev

PHP time() produces wrong result on server

분류에서Dev

php xml pagination issues missing one result

분류에서Dev

PHP round function returned a weird result

Related 관련 기사

뜨겁다태그

보관