PHP Bootstrap Accordion - 如何显示动态数据?

时间:2013-06-18 19:40:18

标签: php twitter-bootstrap accordion

所以我试图让bootstrap手风琴动态显示最多获胜的前5名球队,我也将其用于其他排名。

问题是每当我点击标签2,3或4时,它只会打开手风琴中的标签1。我知道问题来自“href ='#collapse41'”,每个循环都是一样的。我假设每个手风琴div选项卡都需要一个唯一的ID,但我不知道如何正确实现它。我可能100%错了,如果是这样,请纠正我!

我已经完成了大约15次搜索,只找到了一个答案,这个答案不是很详细或有帮助。任何帮助或指导学习如何做到这一点将非常感激。

(我将其重命名为#collapse41的原因是因为我在同一页面上有多个手风琴。)


<div class="accordion" id="accordion2">
<?php
include 'db.php'; //connect to database
$result = mysql_query("SELECT * FROM teams ORDER BY wins DESC LIMIT 5");
    $rank = 1;

    if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {

echo "<div class='accordion-group'>";
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion4' href='#collapse41'>";

    echo "<td>{$row['name']} <br /> </td>";

echo "</a>";
echo "</div>";
echo "<div id='collapse41' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";

    echo "<td>Rank: {$rank} <br /> </td>";
    echo "<td>Wins: {$row['wins']} <br /> </td>";
    echo "<td>Losses: {$row['losses']} <br /> </td>";

echo "</div>";
echo "</div>";
echo "</div>";

            $rank++;        
        }
    }
    ?>
</div>

1 个答案:

答案 0 :(得分:0)

你必须打破循环....并循环一次以获得标题,并且两次获得身体......

所以你需要首先回应组div ....

 echo "<div class='accordion-group'>";

然后开始循环输出标题...... 然后运行另一个循环来输出div ....

if (mysql_num_rows($result)) {
  //LOOP 1  
  while ($row = mysql_fetch_assoc($result)) {

  //Build heading pieces
  echo "<div class='accordion-heading'>";
  echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion-".$row['id']." href='#accordian-".$row['id']."'>";

 echo "<td>{$row['name']} <br /> </td>";

 echo "</a>";
 echo "</div>";}

 //LOOP 2
 //Build inner pieces
 while ($row = mysql_fetch_assoc($result)) {

 echo "<div id='accordian-".$row['id']."' class='accordion-body collapse'>";
 echo "<div class='accordion-inner'>";

 echo "<td>Rank: {$rank} <br /> </td>";
 echo "<td>Wins: {$row['wins']} <br /> </td>";
 echo "<td>Losses: {$row['losses']} <br /> </td>";

 echo "</div>";
 echo "</div>";  }


echo "</div>";

        $rank++;        
    }
}

我实际上并不确定你的手风琴是如何构造的,所以这可能是也可能不是正确的信息,但每当我做手风琴时,我就是这样做的。如果你可以发布预期的HTML输出(手风琴模板),这对我有很大的帮助,我可以根据你的实际需要改进我的答案