0

I have som trouble finding out what i am doing wrong. I am using Bootstrap Table in a foreach loop

 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  

Foreach Loop

The problem is that there are to many Bruger:, Team: and startet

i only want 1 line of Bruger, Team: and startet and then the users listed below. I know where the problem is:

 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>";  
        }

But i dont no how to solve it. Sorry for my bad english and stupid question. Merry Christmas

2
  • put table tag and th tags out of your loop and it will be clear Commented Dec 23, 2015 at 23:05
  • Thanks M8! It worked. Commented Dec 23, 2015 at 23:13

3 Answers 3

1

The only thing you would want to leave inside the foreach is the <tr> and <td> sections. Leave the <table..> and <th> (table headers outside) the foreach that way it will generate a table.

Example

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
Sign up to request clarification or add additional context in comments.

1 Comment

@MrLister Thank you for the edit! I'm quite new to answering :)
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  

Comments

0

It's really difficult to understand what you're asking, but I think I got the gist. The following code example takes an array

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

and turns it into a table

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

code:

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

perhaps that will help you

2 Comments

he just need to make the th not appear at every row it fetch.
@AlaaM.Jaddou ah I see

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.