如何使用子ID返回父全名

时间:2015-07-06 11:07:04

标签: php mysql

我有三个函数返回具有递归查询user_id的特定子项的所有父项。 所有这些函数都运行良好..但是当我开始使用foreach循环来返回函数中的多个用户父名称时问题就开始了

merge_comments_data ..

  

注意:t_relation表示= parent_id

class News extends front_end {

public $parents_names;

function merge_comments_data($related_comments) {
    foreach ($related_comments as $comment) {
        $full_name = $this->get_parents_names($comment['cn_visitor_id']);
    }
    echo "<pre>";
    print_r($names);
    echo "</pre>";
    exit;
}

//// all these function to get full parent names by user id

function get_parents_names($user_id = 0) {
    $this->user_parent_name($user_id);
    echo $this->parents_names;
}

function user_parent_name($user_id = 0) {
// clear the variable at first
    $result = $this->get_parents($user_id);

    if (is_object($result)) {
        $this->parents_names .= ' ' . $result->t_name . ' ';
        if ($result->t_relation != 0) {
            $this->user_parent_name($result->t_relation);
        }
    }
}

public function get_parents($user_id = 0) {
    $result = $this->db->query("SELECT * FROM d_tree where t_id = '$user_id'");
    if (is_object($result->row())) {
        $result = $result->row();
    } else {
        $result = '';
    }
    return $result;
}

0 个答案:

没有答案