简体   繁体   English

如何通过1 php / mysql结果多次循环

[英]how to loop multiple times through 1 php/mysql result

wondering if there's an simpler way to do this... 想知道是否有更简单的方法来做到这一点...

i have a table with a bunch of rows and need to display them separately based on specific values on 1 column named "page_group". 我有一个包含一堆行的表,需要根据名为“ page_group”的1列上的特定值分别显示它们。 right now it does work when i reset the pointer of the result back to zero but wondering if a simpler way to code because got at least 15 different sections to group results ...? 现在,当我将结果的指针重置为零时,它确实起作用了,但是想知道是否有一种更简单的编码方式,因为至少有15个不同的部分可以对结果进行分组...?

//this is the query
$q_home = mysql_query("SELECT * FROM pages WHERE page_nav = 'No' ORDER BY page_group ASC");

//this would show results 1, showing all rows that have the value "sample1"
<h3>Result 1</h3>
<ul>
<? 
    while($r_nav = mysql_fetch_assoc($q_home)){
      $group_folder = $r_nav['page_group'];
      if ($r_access["$group_folder"] == "Yes" && $group_folder == 'sample1') { 
            $access = 'block;'; 
        } else { 
            $access = 'none;'; 
        } {;
            echo '<li style="display:'.$access.'"><a href="'.$group_folder.'/'.$r_nav['page_url'].'.php">'.$r_nav['page_name'].'</a></li>';
      }
    }
?>
</ul>   

//this would be result 2 showing all rows that have value "sample2"
<h3>Result 2</h3>
<ul>
<? 
    mysql_data_seek($q_home, 0);
   while($r_nav2 = mysql_fetch_assoc($q_home)){
      $group_folder = $r_nav2['page_group'];
      if ($r_access["$group_folder"] == "Yes" && $group_folder == 'sample2') { 
            $access = 'block;'; 
        } else { 
            $access = 'none;'; 
        } {;
            echo '<li style="display:'.$access.'"><a href="'.$group_folder.'/'.$r_nav2['page_url'].'.php">'.$r_nav2['page_name'].'</a></li>';
      }
    }
?>
</ul>

You have the mysql_fetch in a loop, so just don't stop the loop on a specific $group_folder (remove that part of the if clause): 您将mysql_fetch放在一个循环中,所以不要在特定的$group_folder上停止循环(删除if子句的这一部分):

<ul>
<?php
  while($r_nav = mysql_fetch_assoc($q_home)){
    $group_folder = $r_nav['page_group'];
    if ($r_access["$group_folder"] == "Yes")
      $access = 'block;';
    else
      $access = 'none;';
    echo '<li style="display:'.$access.'">
    <a href="'.$group_folder.'/'.$r_nav['page_url'].'.php">
    '.$r_nav['page_name'].'</a></li>';
  }
?>
</ul>   

Something like this loop will be better solution: 像这样的循环将是更好的解决方案:

$q_home = mysql_query("SELECT * FROM pages WHERE page_nav = 'No' ORDER BY page_group ASC");
$x = 1;
<? while( $r_nav = mysql_fetch_assoc($q_home) ) { ?>
<h3>Result <?= $x; ?></h3>
<ul>
<?php
  $group_folder = $r_nav['page_group'];
  if( $r_access["$group_folder"] == "Yes" && $group_folder == 'sample1') {
    $access = 'block;';
  }
  else { 
    $access = 'none;';
  }
  echo '<li style="display:'.$access.'"><a href="'.$group_folder.'/'.$r_nav['page_url'].'.php">'.$r_nav['page_name'].'</a></li>';
  }
  $x++;
?>
</ul>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM