2016-03-22 16 views
0

Veritabanımda iki sütun var, yılı ve içeriği yıllara göre gruplamak istiyorum (çok fazla içeriğim var ve yıllarca sadece 2015 ve 2016 değil) böylecePhp: sütundaki tüm içeriği çıktı sadece ilk öğe değil

) örnek i html

<div class="all"> 
 
    <div class="2016"> 
 
    2016 
 
     <div class="content_2016"> 
 
      All the content of the column that is in the same ligne as 2016 in the database 
 
    </div> 
 
    </div> 
 
    
 
    <div class="2015"> 
 
    2015 
 
     <div class="content_2015"> 
 
      All the content of the column that is in the same ligne as 2015 in the database 
 
    </div> 
 
    </div> 
 
</div>

<?php 
 
\t $query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi "; 
 
\t $result = mysqli_query($connection, $query); 
 

 
\t $previous =0; 
 
\t while ($val = mysqli_fetch_array($result)) 
 
\t { 
 

 
\t \t if ($previous <> $val['pub_year']) 
 
\t \t { 
 
\t \t \t $previous = $val['pub_year']; 
 
\t \t \t $year = $previous; 
 

 
\t \t echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#'; 
 
\t \t echo $year; 
 
\t \t echo '">'; 
 
\t \t echo $year; 
 
\t \t echo '</button>'; 
 
\t \t echo '<div id="'; 
 
\t \t echo $year; 
 
\t \t echo '" class="collapse">'; 
 

 

 

 
\t \t \t $Temp = highlight("person1",$val['pub_publi'],"0000FF"); 
 
\t \t \t $Temp = highlight("person2",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person3",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person4",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person5",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person6",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person7",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person8",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person9",$Temp,"0000FF"); 
 
\t \t \t $Temp = highlight("person10",$Temp,"0000FF"); 
 

 

 
\t \t \t echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in for ' . $val['pub_publi'] . '"></a>'; 
 
    \t \t echo $Temp; 
 
\t \t \t \t echo '</div>'; 
 

 
\t \t \t 
 
\t \t \t \t \t } 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t \t } 
 

 

 

 
?>
0 bu çıkış olacak

O ... yıllar

vb sağ

çıkış olanağı olduğunu ancak yalnızca her yılın ilk ligne gösterilmiyor her yıl için tüm içerik.

+0

Farklı bir yıl için "if" öğesinin dışında _no_ kodu var, dolayısıyla, yalnızca bir önceki satırla karşılaştırıldığında yıl içinde yazdırılan içerik olacak. – klaar

+0

ve çalışmasını sağlamak için ne yapabilirim? Teşekkür ederim – usethe23

cevap

0

Bu kod benim için çalıştı.

<?php 
     $previous = false; 
     while ($val = mysqli_fetch_array($result)) { 

      // when group is changing, end previous and start new 
      if ($previous <> $val['pub_year']) { 

       // end previous group html markup 
       if ($previous !== false) { 
        echo '</div>'; 
       } 

       $previous = $val['pub_year']; 
       $year = $previous; 

       echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#' . $year . '>' . $year . '</button>'; 
       echo '<div id="' . $year . '" class="collapse">'; 
      } 

      // always output data inside group 
      $Temp = highlight("person1",$val['pub_publi'],"0000FF"); 
      $Temp = highlight("person2",$Temp,"0000FF"); 
      $Temp = highlight("person3",$Temp,"0000FF"); 
      $Temp = highlight("person4",$Temp,"0000FF"); 
      $Temp = highlight("person5",$Temp,"0000FF"); 
      $Temp = highlight("person6",$Temp,"0000FF"); 
      $Temp = highlight("person7",$Temp,"0000FF"); 
      $Temp = highlight("person8",$Temp,"0000FF"); 
      $Temp = highlight("person9",$Temp,"0000FF"); 
      $Temp = highlight("person10",$Temp,"0000FF"); 

      echo $Temp . '<br>'; 
     } 

     // end last group html markup 
     if ($previous !== false) { 
      echo '</div>'; 
     } 
?> 
0

bu güncellenmiş kodu deneyin:

<?php 
    $output=''; 

    $query1 = "SELECT pub_year FROM publi where pub_type=0 GROUP BY pub_year order by pub_year DESC"; 
    $result1 = mysqli_query($connection, $query1); 

    while ($val1 = mysqli_fetch_array($result1)) 
    { 
     $year = $val1['pub_year']; 
     $query2 = "SELECT * FROM publi where pub_year=$year AND pub_type=0 order by pub_year DESC, pub_publi "; 
     $result2 = mysqli_query($connection, $query2); 

     $output.=$year.'<br>'; // storing each year value one time 

     while ($val2 = mysqli_fetch_array($result2)) 
     { 
      $output.=$val2['pub_type'].', '.$val2['pub_publi'].'<br><br>'; 
     } 
    } 
    echo $output; 
?> 

Numune Çıktı:

2016 
[pub_type_1], [pub_publi_1] 
[pub_type_2], [pub_publi_2] 

2015 
[pub_type_1], [pub_publi_1] 
[pub_type_2], [pub_publi_2] 
+0

Ben de bunu denedim ama excatly istediğim yıl bir kez çıktı olarak ben yıl 2016.2011 vs göre sıralanır akordeon içinde kullanabilirsiniz böylece ama beklendiği gibi çalışmıyor. Açılan içerik ilk ligne 'değerine' atanır veya sadece ilk ligne 'değeri' görünür ve diğerleri değil – usethe23

+0

Bu güncellenmiş kodu denediniz mi? – Thiyagesan

İlgili konular