来自数据库的php循环

时间:2018-11-19 09:51:19

标签: php html loops serialization

我有一个代码,使用serialize()将数据存储在数据库中作为对象, 并在数据库中的循环表中显示每个类别的“付款”列,该列具有包含付款数据的数组(例如:[num,string,date]),每个类别可能会有多个付款, 但问题在于,现在它在表中的另一项下显示所有付款类别 我尝试做得更好,以便在显示付款的列中将 模态按钮,显示模态中每个类别的付款,但它显示数组中所有类别的第一个值 我尝试提供ID,但是那是不可能的,因为它会循环打印信息

那是我的代码:

function showTable($table, $data, &$totalSum = 0)
{
$totalEestimatedCost = 0;
$totalMyBudget = 0;
if (is_array($data)) {
    $tableToShow = '';
    $outputData = '<table class="table table-striped table-hover table-responsive-xs table-responsive-sm table-responsive-md table-responsive-lg" >';
    $outputData .= "
    <thead>
    <tr>
      <th  scope='col'>קטגוריה</th>
      <th  scope='col'>התקציב שלי</th>
      <th  scope='col'>עלות בפועל</th>
      <th  scope='col'>תשלומים</th>
      <th  scope='col'>יתרה לתשלום</th>
      <th  scope='col'>הקובץ</th>
    </tr>
    </thead>
    <tbody>
     ";
    $totalHefresh = 0;
    for ($i = 1; $i < count($data) / 2; $i++) {
        $arrData = unserialize($data[$i]);
        $heb_name = $arrData['data']['field_name_heb'];
        $files = $arrData['data']['files'];
            $myBudget = isset($arrData['data']['my_budget'])?$arrData['data']['my_budget']:0;
              $myBudgetNumber = number_format($myBudget);
        $estimatedCost = isset($arrData['data']['estimated_cost'])?$arrData['data']['estimated_cost']:0;
        $estimatedCostNumber = number_format($estimatedCost);
        $filesData = returnFiles($files, $arrData['data']['field_name'], $arrData['data']['stage']);
        $total_field_cost =  isset($arrData['data']['total_cost'])?$arrData['data']['total_cost']:0;
        $payments = returnPayments($arrData['payments'], $arrData['data']['field_name'], $arrData['data']['stage']);

        $hefresh = $estimatedCost - $total_field_cost;
        $hefreshNumber = number_format($hefresh);
        $totalEestimatedCost += $arrData['data']['estimated_cost'];
        $totalEestimatedCostNumber = number_format($totalEestimatedCost);
        $totalMyBudget += $arrData['data']['my_budget'];
        $totalMyBudgetNumber = number_format($totalMyBudget);
        $totalHefresh += $hefresh;
        $totalHefreshNumber = number_format($totalHefresh);
        $outputData .=
            "<tr>

<td>$heb_name</td>
 <td class='text-center'> <span> ₪ </span> <span>$myBudgetNumber</span>  </td>
<td  class='text-center'> <span> ₪ </span> <span>$estimatedCostNumber</span>  </td>
<td >$payments</td>
<td class='text-center'><span>  ₪ </span> <span>$hefreshNumber</span></td>
<td style='height: 118px'>$filesData</td>
</tr>
</tbody> ";

 //echo "<pre>";
   //print_r($arrData);
    }
    printDinamicDataTable($table , $outputData , $totalEestimatedCost , $totalHefresh);
    $outputData .= "</table>";
    $outputData .= "<div class='total-hfresh'>
    <div>סה\"כ התקציב שלי: $totalMyBudgetNumber</div>
    <div>סה\"כ עלות בפועל: $totalEestimatedCostNumber</div>
    <div>יתרה לתשלום: $totalHefreshNumber</div>

    </div>";
  //echo "<br> $outputData";
    $totalSum += $totalHefresh;
    return $outputData;
    }
   };

  function returnPayments($payments, $field, $stage)
  {
  $dataToReturn = '';
  foreach ($payments as $key => $value) {
    $payment = $value['payment'];
    $date = $value['date'];
    $note = $value['note'];
   $dataToReturn .= "<table style='width: 100%;margin-bottom: 5px'><tr><td 
    class='pay-td1'>₪  $payment </td>";
    $dataToReturn .= " <td class='pay-td2'>$note </td>";
    $dataToReturn .= "<td class='pay-td3'> $date </td>";
    $dataToReturn .= "<td><button data-stage='$stage' data-id='$key' data- 
     name='$field' class='deletePayment btn btn-danger'><b>X</b>                                 
     </button></td></tr></table>";

   }
    return $dataToReturn;
}

0 个答案:

没有答案