CI 묻고 답하기

제목 왕초보입니다. 데이타베이스 쿼리 질문
글쓴이 iko 작성시각 2012/11/28 21:18:19
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 12613   RSS
 안녕하세요.

연습용으로 작은 쇼핑몰을 만들고 있습니다.

막히는?부분이 있어 질문드려요.

제품을 구매한 구매자들이 리뷰와 함께 별을 남기는데요.

첫페이지 일부분에 리뷰점수가 가장 좋은 제품 6개를 보여줍니다.

아래는 지저분한 소스 일부분이구요.ㅠㅠ 

일단 작동은 하는데 루프도 너무 많고 그래서 혹시 간단하게 데이타베이스 쿼리를 이용해서 구현할수 있나 해서요.

작은 조언이라도 감사하게 생각하겠습니다~^___^


             $top_sellers = $this->db->select('*')->from('review')->get()->result();
             
             $sold_products = array();
             
             foreach($top_sellers as $item)
             {
                 $qty = 1;
                 if(array_key_exists($item->model,$sold_products))
                 {
                     $qty++;
                     $sold_products[$item->model] = $sold_products[$item->model]+$item->star."_".$qty;
                 }
               else
                {
                    $sold_products[$item->model] = $item->star."_".$qty;    
                }
                
             }
             
             $rating_array = array();
             
             foreach($sold_products as $key=>$value)
             {
                 $score_array = explode('_',$value);
                 
                 $score = round($score_array[0]/$score_array[1]);
                 
                 $rating_array[$key] = $score;
                 
             }
             
             
             arsort($rating_array);
             
             $top_3 = array();
         
         foreach($rating_array as $key => $value)
         {
             $top_3[$key] = $value;
             if(count($top_3)==6)
             {
                 break;
             }
             
         }
 다음글 CKFinder 연동시 Session 관련하여 질문 드... (2)
 이전글 개편 작업시 codeigniter 사용에 대한 질문 (7)

댓글

니삼 / 2012/11/28 22:38:48 / 추천 0
 디비 튜닝 질문이라면 디비 구조도 필요할것 같네요..
한대승(불의회상) / 2012/11/29 09:33:57 / 추천 0
"리뷰점수가 가장 좋은 제품 6개" 가 핵심이라면...

$top_sellers = $this->db->select('*')->from('review')->order_by('리뷰점수', 'desc')->limit(6)->get()->result();
하시면 끝
iko / 2012/11/29 10:33:21 / 추천 0
sorry to write in english , can't  type in korean in company computer.
thanks for the info anyway. how stupid i am.....i could actually update sold qty , review scores in each product database field. and could use simple database query. -.-;;;