2016-03-23 8 views
1

In meiner Datenbank Anzeige Ich habe eine Tabelle publi innerhalb dieser Tabelle pub_year und pub_publi ich habe zwei Spaltenphp Schleife innerhalb Akkordeon nicht korrekt

Beispiel der Tabelle publi Inhalt genannt:

<table border="1"> 
 
    <tr> 
 
     <td>2016</td> 
 
     <td>content_2016_1</td> 
 
    </tr> 
 
    <tr> 
 
     <td>2016</td> 
 
     <td>content_2016_2</td> 
 
    </tr> 
 
    <tr> 
 
     <td>2016</td> 
 
     <td>content_2016_3</td> 
 
    </tr> 
 
    <tr> 
 
     <td>2015</td> 
 
     <td>content_2015_1</td> 
 
    </tr> 
 
    <tr> 
 
     <td>2015</td> 
 
     <td>content_2015_2</td> 
 
    </tr> 
 
</table>

, und ich will diesen Ausgang

<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button> 
 
    <div id="2016" class="collapse"> 
 
    content_2016_1 <br> 
 
    content_2016_2 <br> 
 
    content_2016_3 <br> 
 
    </div> 
 

 
    <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button> 
 
    <div id="2015" class="collapse"> 
 
    content_2015_1 <br> 
 
    content_2015_2 <br> 
 
    </div>

und dies ist mein Code:

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

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

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

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

     $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 '<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>'; 
     echo $Temp; 
     echo '</div>'; 
    } 
} 

?> 

aber das Ergebnis, das i erhalten:

<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button> 
<div id="2016" class="collapse"> 
    content_2016_1 <br> 
</div> 

content_2016_2 


content_2016_3 

<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button> 
<div id="2015" class="collapse"> 
content_2015_1 <br> 

</div> 
content_2015_2 <br> 

Antwort

0

Das Wie Sie die Daten nach pub_year gruppieren, ist korrekt, aber in Ihrem Beispiel geben Sie nur Informationen aus, wenn sich die Gruppe ändert.

ich etwas Ähnliches vorschlagen würde:

<?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

Funktioniert perfekt vielen Dank Siffer. – usethe23

Verwandte Themen