2017-06-17 64 views
-4

我想每个采访下面有18个采访和一个按钮。但是我搞砸了周期,现在我每次采访下面有18个采访18个按钮。它应该只是一个按钮。PHP虽然周期

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

这里是我的代码:

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

*有18个采访,下面的每个采访的按钮。但我搞砸了周期,现在我有18次采访,每次采访下有18个按钮*。你的陈述是矛盾的。 – Ravi

+0

我的意思是每次面试时应该只有一个按钮。换句话说,我需要18个采访和18个按钮。 – Kaloyan

+0

但是现在,我在每次采访下面有18次采访和18次按钮。这意味着18 * 18 = 324个按钮。 – Kaloyan

回答

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'> 
+0

你如何建议加入他们。你的意思是加入他们在我的phpmyAdmin或修改该代码并加入他们的查询。如果你想加入他们的查询,你能提供一个解决方案。先谢谢你。 – Kaloyan

+0

你已经写了'SELECT * FROM interviews WHERE categories ='“。$ id。”'ORDER BY title DESC'。您正在使用'category id'来查询'interview'表。那么,为什么你不加入单一查询? – Ravi

+0

类别是访谈表中的列,它是类别2表中列ID的外键 – Kaloyan

0

你应该将你的第二次面试“循环”到您生成的采访时的输出按钮(一个或多个)应该在每次面试后进入循环。

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

最有可能的是,你有18个特色采访。

你的循环结构法是这样的:

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

loop through featured interviews [ 
      Generate output for button 
] 
+0

是的,我明白。我应用了更改,但现在所有18个按钮都进行了最后一次采访? – Kaloyan

+0

特色采访全是18个采访。但是,我现在只能在最后一次采访之下获得所有按钮。 – Kaloyan

+0

Yoy必须将按钮代码混合到您希望按钮出现的位置的$ interview html字符串中。 – Juan