duanang58939 2019-05-23 09:04
浏览 49

如何组合数组值重复并自定义html表中数组结果的显示?

I have here a for loop, $limit is the number of year (this is based on the sql database result). In this case, the $limit is 3, the count of lid is 3 (1, 2, 3). Then used another query to get all the values of the three lid, so there are 8 rows. In the 8 rows, there are only 5 unique class id, I need to group the duplicate index of class ID.

I already tried array_unique to display the 5 results only. But I still need the values of the other 3 results to be able to display it in the p2 and p3 columns, but in the same row if they have the same class ID

<?php
   for($iy=1; $iy&lt;=$limit; $iy++){   
     $sql="SELECT * FROM view_items WHERE pyear='$iy'"
     $query = $this->db->prepare($sql);
     $query->execute();
     $res=$query->fetch();  
     $lids = $res->lid;
     $sql_arr.="SELECT * FROM view_items WHERE lid='$lids'"; 
     if($iy!=$limit) $sqlarr .= " UNION "; 
     $new_arr[] = $sql_arr; 
   }
   $sql = implode("",$new_arr);
   $query = $this->db->prepare($sql);
   $query->execute();
   $result = $query->fetchAll();

   foreach($result as $row):    
    $q1 = ($row->month1) + ($row->month2) + ($row->month3);
    $q2 = ($row->month4) + ($row->month5) + ($row->month6); 
    $total = $q1 + $q2;
?>
   <tr>
    <td><?php echo $row->item_class; ?></td>
    <td><?php echo $q1; ?></td>
    <td><?php echo $q2; ?></td>
    <td><?php echo $total; ?></td>
   <tr>
<?php 
  endforeach; 
?>

Instead of displaying 8 result, it should display only 5 rows. Then the values of the duplicate should be on their right column based on their pyear. It's a bit hard to explain in paragraph form, maybe the sample output will do explain better.

Actual output

Actual Output (Wrong)

Expected output

Expected Output

FINAL: Used SQL query instead of customizing the display of array. Answer for both my question (BUT THE QUESTION IS NOT A DUPLICATE), but still open for any suggestions regarding array since I'm no expert in that. How to display group_concat result seperately in html table cell?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法