PHP循环

I would like to have 18 interviews and a button below each interview. But I messed up with the cycles and now I have 18 interviews with 18 buttons below each interview. It should be just one button.

I am almost sure it is a cycle problem. Can you please tell me where is my mistake?

Here is my code:

    <?php
  $sql = "SELECT * FROM interviews WHERE featured = 1";
  $featured = $db->query($sql);
?>

<div class="container">
    <table class="rwd-table">
        <tbody>
            <br><br>
            <tr>
                <?php 
        include_once("../forum/connect.php");
        $sql = "SELECT * FROM categories2 ORDER BY category ASC";
        $res = mysql_query($sql) or die(mysql_error());
        $interviews = "";
if(mysql_num_rows($res) > 0){
    while($row = mysql_fetch_assoc($res)){
      $id = $row['id'];
      $category = $row['category'];

        $sql2 = "SELECT * FROM interviews WHERE categories='".$id."'ORDER BY title DESC";
        $res2 = mysql_query($sql2) or die(mysql_error());
        if(mysql_num_rows($res2) > 0){

          while($row2 = mysql_fetch_assoc($res2)){
             $tid = $row2['id'];
             $title = $row2['title'];
              $interviews .= "<a href='view_interview.php?cid=".$id."&tid=".$tid."' class='cat_links'>".$category." - ".$title."</a>";      

    }
  }
}

    $counter = 0;
    while($product = mysqli_fetch_assoc($featured)){
      $image = $product['image'];
      $title = $product['title'];
      $decs = $product['description'];
        if($counter % 3 == 0){
            echo '</tr><tr>';
        }
        ++$counter;


echo "<td>
<div id='element1'></div>
<div id='content1'>
            <img src=".$image." alt=".$title.">
            <h3>".$title."</h3>
            <hr>
            <h4>".$decs."</h4>

       <div id='hovers'>
      <a href='view_interview.php?cid=".$id."&tid=".$tid."' class='button' target='_blank'>
        <span class='contentbut'> Read More</span>
      </a>
    </div>";    echo $interviews; 
}
    ?>
                <br><br>

                </td>
                <?php
   }   

        ?>
            </tr>
        </tbody>
    </table>
</div>

I don't see any point query same table twice in same code.

  $sql = "SELECT * FROM interviews WHERE featured = 1";
  $featured = $db->query($sql);
?>

<div class="container">
    <table class="rwd-table">
        <tbody>
            <br><br>
            <tr>
                <?php 
        include_once("../forum/connect.php");
        $sql = "SELECT * FROM categories2 ORDER BY category ASC";
        $res = mysql_query($sql) or die(mysql_error());
        $interviews = "";
if(mysql_num_rows($res) > 0){
    while($row = mysql_fetch_assoc($res)){
      $id = $row['id'];
      $category = $row['category'];

        $sql2 = "SELECT * FROM interviews WHERE categories='".$id."'ORDER BY title DESC";

Can't you join the categories2 and interviews table and use only one loop ??

Also, below line of code is part of two loops, which is actually the reason for 18x18.

<a href='view_interview.php?cid=".$id."&tid=".$tid."' class='button' target='_blank'>

You should move your second interview "loop" into the loop where you generate the output for the interview if the button(s) should go after each interview.

Be aware that the features query is not necessary bound to one result. If you have several feature=1 records, you will have a button for each one.

The most likely is that you have 18 featured interviews.

Your loop structre is like this:

loop through categories[
     loop through inteviews[
          Generate output for interview
     ]
]

loop through featured interviews [
          Generate output for button
]