PHP循环

时间:2017-06-17 10:47:50

标签: php html sql loops while-loop

我想在每次采访下面接受18次采访和一个按钮。但是我搞砸了周期,现在我在每次采访中面对18个按钮进行18次采访。它应该只是一个按钮。

我几乎可以肯定这是一个循环问题。你能告诉我我的错误在哪里吗?

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

我在相同的代码中没有看到任何点查询同一个表两次。

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

joincategories2interviews并且只能使用一个循环吗?

此外,下面的代码行是两个循环的一部分,这实际上是18x18的原因。

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

答案 1 :(得分:0)

如果按钮应在每次采访后进行,你应该将你的第二次采访“循环”移动到你为面试产生输出的循环中。

请注意,features查询不必绑定到一个结果。如果您有多个feature=1条记录,则每个记录都会有一个按钮。

最有可能的是你有18次采访。

你的循环结构是这样的:

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

loop through featured interviews [
          Generate output for button
]