2016-08-09 3 views
1

데이터베이스에서 일부 데이터가 반환되지만 현재는 모든 데이터를 하나의 큰 목록으로 반환합니다.범주 별 SQL 결과 분할

데이터에는 모든 커플 항목에 대한 카테고리 이름이 있습니다. 카테고리가 지정된 모든 항목 그룹을 분리하고 싶습니다.

Example: 

test1 (has category testing) 
test2 (has category testing) 
test3 (has category testing123) 

이러한 결과는 단지 하나 개의 큰 목록으로 보여, 나는 결과처럼되고 싶습니다

testing: 
test1 
test2 

testing123: 
test3 

내가 이걸 어떻게해야합니까?

내 코드는 지금처럼 (에만 관련 부품) :

while ($row_items = mysqli_fetch_assoc($res_items)) { 

     $checklist = ''; 
     if ($row_items['checkpoints'] != '') { 
      $check = explode(',', $row_items['checkpoints']); 
      foreach($check as $list) { 
       $checklist .= ' 
       <li class="cataloguslist"> 
        <i style="color:#e88f41;" class="fa fa-check-square" aria-hidden="true"> 
       </i> '.$list.'</li>'; 
      } 
     } 

     echo ' 
     <div class="col-lg-2 col-md-3 col-sm-6 col-xs-6 portf"> 
      <a class="fancybox" href="../../catalogus/'.$row_items['afbeelding'].'"> 
      <div class="gallery-item"> 
       <div class="image"> 
        <img src="../../catalogus_icons/'.$row_items['afbeelding'].'.jpg" alt="" style="border:1px solid #ccc;" class="img-responsive" /> 
       </div> 

       <div class="bottom-info" style="min-height:200px;"> 
        <div class="name">'.$row_items['naam'].'</div> 
        <ul style="margin-left:35px;margin-top:10px;">'.$checklist.'</ul> 
        <a href="contact.php"> 
         <button class="contact_button buttoncontact btn-primary" style="border-radius:2px;"> 
          Vraag offerte aan 
          <span class="icon-mail-alt"></span> 
         </button> 
        </a> 
        <div class="contactlink"> 
         <a href="contact.php">Neem contact op</a> 
        </div> 
       </div> 
      </div> 
      </a> 
     </div>'; 
    } 

모든 결과가 cat 같은 id 및 카테고리 이름을 가진 테이블 (producten_cats에 연결 데이터베이스에 cat 행을 가지고 그것은 tablerow 이름으로 aswell에 :. naam,

답변

0

그것은 당신의 카테고리 이름은 코드에서 거짓말을 어디에 있는지 어렵다, 그래서 당신에게 정확한 답을 줄 수

T o 데이터베이스 결과 집합에 새 category 값이있는 첫 번째 행을 가져올 때이를 감지하기 위해 표시된대로 데이터베이스 결과 집합을 분할하여 이와 같이 수행합니다. 이런 종류의 물건이 제대로 작동하려면

$previous_category = "---nothing---"; 
while ($row_items = mysqli_fetch_assoc($res_items)) { 

    $category = $row_items['category']; 
    if ($category != $previous_category) { 
     $previous_category = $category; 
     /* show category header, for example */ 
     echo '<h2>$category:</h2>'; 
    } 
    $checkpoints = $row_items['checkpoints']; 
    /* show details, for example */ 
    echo '<p>$checkpoints</p>'; 
    } 

, 당신은 당신의 쿼리에 ORDER BY category를 사용해야합니다.

이러한 종류의 기술은 일반적으로 표현 레이어 형식으로 으로 알려져 있습니다. MySQL에서 제공하는 데이터 테이블을 사용자 친화적 인 레이아웃으로 변환합니다.