分配给在FOR循环mysql查询php中使用的数组列表

时间:2017-07-19 16:48:42

标签: php html mysql arrays

这只是一个例子我有一个mysql查询,我有一个查询条件数组..

$b = array(Jan, Feb, March);
$c = array(idle, active);

$hr_ne3 = "SELECT any statement WHERE b = 'Jan' AND c = 'idle'";
$result_hr_ne3 = mysql_query($hr_ne3);
$ne_hr3=mysql_fetch_array($result_hr_ne3,MYSQL_ASSOC);

所以查询的条件将遵循数组.. Jan-> idel,Feb-> idel,March-> idel 然后继续 Jan-> active,Feb-> active,March-> active

任何人都有想法和指导来查询这种流程? 表格布局应该看起来像THIS

2 个答案:

答案 0 :(得分:1)

拆分为两个循环

$b = array("Jan", "Feb", "March");
$c = array("idle", "active");

foreach($b as $itemB) {
  $hr_ne3 = "SELECT any statement WHERE AND b = '".$itemB."' AND c = '".$c[0]."'";
  $result_hr_ne3 = mysql_query($hr_ne3);
  $ne_hr3=mysql_fetch_array($result_hr_ne3,MYSQL_ASSOC);
  //echo $hr_ne3."\n";
}

foreach($b as $itemC) {
  $hr_ne3 = "SELECT any statement WHERE AND b = '".$itemC."' AND c = '".$c[1]."'";
  $result_hr_ne3 = mysql_query($hr_ne3);
  $ne_hr3=mysql_fetch_array($result_hr_ne3,MYSQL_ASSOC);
  //echo $hr_ne3."\n";
}

如果您取消注释echo,您将获得

SELECT any statement WHERE AND b = 'Jan' AND c = 'idle'
SELECT any statement WHERE AND b = 'Feb' AND c = 'idle'
SELECT any statement WHERE AND b = 'March' AND c = 'idle'
SELECT any statement WHERE AND b = 'Jan' AND c = 'active'
SELECT any statement WHERE AND b = 'Feb' AND c = 'active'
SELECT any statement WHERE AND b = 'March' AND c = 'active'

答案 1 :(得分:1)

<?php
$b = array('Jan', 'Feb', 'March');
$c = array('idle', 'active');
$result_array = array();
foreach($c as $key){
    foreach ($b as $value) {
        $query          = "SELECT any statement WHERE b = '".$value."' AND c = '".$key."'";
        $result         = mysql_query($query);
        $result_array[] = mysql_fetch_array($result,MYSQL_ASSOC);
    }
    echo "<pre/>";print_r($result_array);
}
?>