dongqiuge5435 2014-09-10 00:30
浏览 64
已采纳

使用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!

  • 写回答

1条回答 默认 最新

  • douxin8610 2014-09-10 03:49
    关注

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题