0

想知道是否有更简单的方法可以做到这一点......

我有一个包含一堆行的表,需要根据名为“page_group”的 1 列上的特定值分别显示它们。现在,当我将结果的指针重置为零时它确实有效,但想知道是否有一种更简单的编码方式,因为至少有 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>
4

2 回答 2

0

你有一个循环中的 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>   
于 2013-03-05T02:39:47.807 回答
0

像这样的循环将是更好的解决方案:

$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 } ?>
于 2013-03-05T02:44:16.410 回答