본문 바로가기
MySql

MySQL CodeIgniter, 모델에서 컨트롤러로 여러 쿼리를 반환하는 방법은 무엇입니까?

by 베이스 공부 2020. 11. 21.
반응형

내 모델이 '카테고리'테이블의 여러 레코드 인 데이터 자체에 대한 쿼리와 'Posts'테이블의 개수 필드를 반환하도록 모델을 가져 오려고합니다.

나는 가능한 해결책을 많이 시도했지만 지금까지 아무도 나를 위해 해결하지 못했습니다.

내보기는 올바른 경로 / forum / $ id에서 작동하지만 카테고리 및 카테고리 별 게시물 수를로드하는 것은 작동하지 않습니다.

카테고리 컨트롤러 :

$this->load->database();
$this->load->model("Categories_model");

$results=$this->Categories_model->getCategories();

$data=array('results'=>$results);

$this->load->view('Categories_view',$results);

카테고리 모델 :

// this works if I only want to get all the categories  
// $query=$this->db->get('Categories');
// return $query->result();

$query1 = $this->db->get('Categories');
$query2 = $this->db->query("SELECT COUNT(*) AS rowCount FROM Posts");

$return array('categories' => $query1, 'count' => $query2);

카테고리보기 :

<tbody>
    <?php foreach ($results['categories'] as $r):?>
        <tr>


<td><?=$r->rowCount?></td> </tr> <?php endforeach;?> </tbody>

범주보기를로드 할 때 다음 오류가 발생합니다.

구문 오류, 예기치 않은 '배열'(T_ARRAY), 파일 이름 : models / Categories_model.php

이 작업을 수행하는 방법에 대한 샘플 코드 또는 현재 코드 수정을 도와 줄 수 있습니까?

미리 감사드립니다!

 

해결 방법

 

코드에서 반환이 잘못됨 반환 유형에서 $ 제거

$return array('categories' => $query1, 'count' => $query2);

그것은해야한다

return array('categories' => $query1, 'count' => $query2);

 

참조 페이지 https://stackoverflow.com/questions/29944028

 

 

반응형

댓글