PHP实现按status字段值将数据归类到对应表头下
Solution to Group Tickets by Status
Got it, let's fix this so your tickets show up under the right headers based on their status value. Here's a step-by-step approach with adjusted code:
1. First, Group Your Data by Status
Before rendering the table, split your $table data into two separate arrays based on the status field. You'll need to replace [STATUS_INDEX] with the actual index of the status value in your $table array (e.g., if status is stored in $table[$i][1], use that instead).
<?php // Initialize empty arrays for each status group $newTickets = []; $currentTickets = []; // Loop through all tickets to group them for ($i = 1; $i <= $last_item_table; $i++) { $status = $table[$i][STATUS_INDEX]; // Replace with your actual status index if ($status == "0") { $newTickets[] = $table[$i]; } elseif ($status == "1") { $currentTickets[] = $table[$i]; } } ?>
2. Render Each Group Under Its Corresponding Header
Now you can render each section separately. I've also fixed a small bug in your original code (you missed a $ in table[$i][7]) and updated the id attributes to class since IDs must be unique across the page.
<!-- New Tickets Section --> <th id="typeleft" colspan="11">New tickets: <span id="numt"><?=count($newTickets)?></span></th> <?foreach ($newTickets as $ticket) {?> <tr class="typeNew"> <td class="tid"><p class="rep"><span><a href="<?=$ticket[11]?>" target="_blank">#<?=$ticket[2]?></a></span></p></td> <td class="tclient"><p class="rep"><span><?=$ticket[3]?></span></p></td> <td class="tassign"><p class="rep"><span><?=$ticket[4]?></span></p></td> <td class="tauthor"><p class="rep"><span><?=$ticket[5]?></span></p></td> <td class="tcompany"><p class="rep"><span><?=$ticket[6]?></span></p></td> <td width="5%" class="tsubj"><p class="rep"><span><?=$ticket[7]?></span></p></td> <td class="tlastup"><p class="rep"><span><?=$ticket[8]?></span></p></td> <td class="tqname"><p class="rep"><span><?=$ticket[10]?></span></p></td> <td class="tlastrespown"><p class="rep"><span><?=$ticket[9]?></span></p></td> </tr> <?}?> <!-- Current Tickets Section --> <th id="typecurrent" colspan="11">Current: <span id="numcurrent"><?=count($currentTickets)?></span></th> <?foreach ($currentTickets as $ticket) {?> <tr class="typeCurrent"> <td class="tid"><p class="rep"><span><a href="<?=$ticket[11]?>" target="_blank">#<?=$ticket[2]?></a></span></p></td> <td class="tclient"><p class="rep"><span><?=$ticket[3]?></span></p></td> <td class="tassign"><p class="rep"><span><?=$ticket[4]?></span></p></td> <td class="tauthor"><p class="rep"><span><?=$ticket[5]?></span></p></td> <td class="tcompany"><p class="rep"><span><?=$ticket[6]?></span></p></td> <td width="5%" class="tsubj"><p class="rep"><span><?=$ticket[7]?></span></p></td> <td class="tlastup"><p class="rep"><span><?=$ticket[8]?></span></p></td> <td class="tqname"><p class="rep"><span><?=$ticket[10]?></span></p></td> <td class="tlastrespown"><p class="rep"><span><?=$ticket[9]?></span></p></td> </tr> <?}?>
Key Notes:
- Replace
[STATUS_INDEX]: Make sure to swap this with the actual position of thestatusvalue in your$tablearray. For example, if status is the first element in each row, use$table[$i][0]. - Unique IDs: I changed all
<td id="...">to<td class="...">because IDs can only be used once per page. Using classes here is the correct practice. - Dynamic Counts: The count of tickets in each section is now dynamically generated with
count($newTickets)andcount($currentTickets)instead of relying on$last_item_table.
内容的提问来源于stack exchange,提问作者Cabbanis
相关产品推荐
相关产品推荐

