PHP Foreach循环中的BOOTSTRAP表

时间:2015-12-23 22:58:11

标签: php twitter-bootstrap twitter-bootstrap-3 foreach

我很难找到我做错了什么。 我在foreach循环中使用Bootstrap Table

 Foreach($fetchSite as $rows)
  {
      $sname = $rows['name'];
      echo "<div class='well'>"
      . "<h4>$sname</h4>";


      foreach ($fetchDep as $rowd)
      {  
          $depn = $rowd['dep'];
          echo "<h5>$depn:</h5>";

  foreach($fetchUserdata as $row)
  {
      $uname = $row['username'];
      $team = $row['team'];
      $stime = $row['sttime'];
      $depna = $row['dep'];
      $siten = $row['name'];

      if($sname == $siten)
      {




        if($depn == $depna)
        {
             echo "<div class='row'>"

        . "<div class='table-responsive'>"
        . "<table class = 'table table-striped' style='width: 500px;'>"
        . "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>"; 

          echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
            echo "<tbody>"
        . "</tbody>"
        . "</table>"
        . "</div>"


        . "</div>";  
        }


      }


  }//end Foreach FetchUSerdata

  }// end Foreach Depfetch  
  echo "</div>";

  } // End Foreach FetchSite  

Google

问题是有很多 布鲁格:,团队:和初学者

我只需要1行Bruger,Team:和startet,然后是下面列出的用户。 我知道问题在哪里:

 if($depn == $depna)
        {
             echo "<div class='row'>"

        . "<div class='table-responsive'>"
        . "<table class = 'table table-striped' style='width: 500px;'>"
        . "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>"; 

          echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
            echo "<tbody>"
        . "</tbody>"
        . "</table>"
        . "</div>"


        . "</div>";  
        }

但我不知道如何解决它。 对不起我的英语不好和愚蠢的问题。 圣诞快乐

3 个答案:

答案 0 :(得分:1)

您希望留在foreach中的唯一内容是<tr><td>部分。将<table..><th>(表格标题在外面)保留为生成表格的方式。

实施例

echo "<div class='row'>"
     . "<div class='table-responsive'>"
     . "<table class = 'table table-striped' style='width: 500px;'>"
     . "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>";

foreach($fetchUserdata as $row)
  {
      $uname = $row['username'];
      $team = $row['team'];
      $stime = $row['sttime'];
      $depna = $row['dep'];
      $siten = $row['name'];

      if($sname == $siten)
      { 
        if($depn == $depna)
        {   
          echo "<tbody>"
               ."<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
               . "</tbody>"
        }
      }
  }
          echo "</table>"
               . "</div>"
               . "</div>";  
//end Foreach FetchUSerdata

答案 1 :(得分:0)

要理解你的要求真的很难,但我想我得到了主旨。以下代码示例采用数组

$fetchSite = [
    [
        'something' => 'a',
        'something_else' => 'b'
    ],
    ...
]

并将其变成表格

something | something_else
----------+---------------
    a     |        b

代码:

<table>
    <thead>
        <tr>
            <?php foreach(array_keys($fetchSite[0]) as $column) { ?>
                <th><?=$column?></th>
            <?php } ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach($fetchSite as $row) { 
            foreach($row as $column) { ?>
                <td><?=$column?></td>
        <?php } 
        } ?>
    </tbody>
</table>

也许这会对你有所帮助

答案 2 :(得分:0)

Foreach($fetchSite as $rows)
{
  $sname = $rows['name'];
  echo "<div class='well'>"
  . "<h4>$sname</h4>";


  foreach ($fetchDep as $rowd)
  {  
      $depn = $rowd['dep'];
      echo "<h5>$depn:</h5>";
     echo "<div class='row'>"
    . "<div class='table-responsive'>"
    . "<table class = 'table table-striped' style='width: 500px;'>"
    . "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>"; 

 echo "<tbody>";
foreach($fetchUserdata as $row)
{
  $uname = $row['username'];
  $team = $row['team'];
  $stime = $row['sttime'];
  $depna = $row['dep'];
  $siten = $row['name'];

  if($sname == $siten)
  {
    if($depn == $depna)
    {
      echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>"; 
    }
  }
}//end Foreach FetchUSerdata
    echo "</tbody>"
    . "</table>"
    . "</div>"
    . "</div>";
}// end Foreach Depfetch  
  echo "</div>";

} // End Foreach FetchSite