An array list assigned for use in a FOR loop. mysql query php

This is just an example, I have a mysql query and I have an array of conditions to query.

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

      

so the query condition will follow the array. Jan-> idel, Feb-> idel, March-> idel then continue Jan-> active, Feb-> active, March-> active

Anyone have an idea and guidance for requesting such a flow? The table layout should look like THIS

+3


source to share


2 answers


divided into two cycles

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

      



If you uncomment echo

you get

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


source


<?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);
}
?>

      



+1


source







All Articles