使用php中的数组为特定td位置指定值

问题描述:

This is my query, where I want to display in one row for every Distinct proc.item_id.

$query = mysql_query("SELECT proc.item_id AS proc_item_id, GROUP_CONCAT(proc.month_id SEPARATOR ', ') AS month_id,GROUP_CONCAT(item_quantity SEPARATOR ',') AS item_quantity,SUM(item_quantity) AS total_quantity,item.item_cost,item.item_desc FROM proc INNER JOIN item ON proc.item_id = item.item_id GROUP BY proc.item_id")or die(mysql_error());

This is for getting all months.

$queryMonth = mysql_query("SELECT * FROM month ORDER BY month.month_id")or die(mysql_error()); `while($resMonth = mysql_fetch_array($queryMonth)) { $month[] = $resMonth['month_name'];

}  

echo '<table border="1" align="center">';
echo '<thead>';
echo '<tr><th rowspan="2">Item</th><th rowspan="2">Quantity</th><th rowspan="2">Cost</th><th colspan="12">SCHEDULE/MILESTONE OF ACTIVITIES</th></tr><tr>';
    foreach($month as $mName)
    {
        $m = substr($mName,0,3);
        echo '<th>'.$m.'</th>';
        $arrCtr++;

    }
echo '</tr>';
echo '<tbody>';

` Then, I fetch the result for $query,

while($res = mysql_fetch_array($query))

{

`$item_desc = $res['item_desc'];
    $total_quan = $res['total_quantity'];
    $cost = $res['item_cost'];
    $total_cost = $cost * $total_quan;
    $month_id = explode(',',$res['month_id']);
    $item_quan = explode(',',$res['item_quantity']);


 echo '<tr><td>'.$item_desc.'</td><td>'.$total_quan.'</td><td>'.$total_cost.'</td>';    

    echo '</tr>';       
} `

My question is: How can I add td for $item_quan? I want to use an array. $arr = array(); But using the values of array $month_id as the position of every $item_quan in $arr example:

$month_id = Array ( [0] => 3 [1] => 1 [2] => 4 )

$item_id = Array ( [0] => 3 [1] => 2 [2] => 5 )

So when: $arr[$month_id[0]] (where $month_id[0] = 3) it should look like this:

$arr[3] = $item_quan[0];

then in it will align on the 3rd table header of the months.

Please help me with this one! I was trying to figure it out for almost 3 days. Thanks!

I think want you want is to relate month index to quantity index with month value. try following snippet

$month_id = explode(',',$res['month_id']);
$item_quan = explode(',',$res['item_quantity']);
$array=array();
foreach($month_id as $i=>$m){
    $array[$m]=$item_quan[$i];
}
echo "<tr>";
  foreach($array as $k=>$v){
      echo "<td>$v</td>";
  }
echo "</tr>";