【发布时间】:2019-01-19 19:03:10
【问题描述】:
我正在创建一个将 MySQL 数据库中的数据总计加载到表行中的报告。 每行包含 24 小时时间段(晚上 9 点到晚上 9 点)的总数。我在数据库中的时间戳是这样设置的:“2018-8-13 22:00:00”。
我正在使用 foreach 循环来加载表格,但我不知道如何根据 24 小时时间框架添加新行。
这是我目前使用的代码:
<?php
use App\Http\Controllers\DashController;
use App\Http\Controllers\KYCController;
use App\Http\Controllers\ICOController;
use App\Http\Controllers\JSONController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
$count = 0;
$totalMAF = 0;
$fiatValue = 0;
$percentageRef = 0;
$percentageFiat = 0;
$percentageBTC = 0;
$totalCount = 0;
$totalTotalMAF = 0;
$totalFiatValue = 0;
$totalPercentageRef = 0;
$totalPercentageFiat = 0;
$totalPercentageBTC = 0;
$genesisDate = "2018-8-13 21:00:00";
$currentBTC = JSONController::getURLRequest("https://api.coinmarketcap.com/v2/ticker/1/?convert=USD")->data->quotes->USD->price;
$Fiat2BTC = 0;
$targetAffID = "MAF0c41e768546c28c66a8e2cca89294853";
$affiliates = DB::table('users')->where('referred_by', $targetAffID)->get();
$startDate = 0; // 24 hour start date/time
$stopDate = 0; // 24 hour stop date/time
$dateRange = $startDate . " /<br /> " . $stopDate;
foreach ($affiliates as $affilate) {
$affID = $affilate->id;
$Txs = DB::table('maf_ico')->where('user_id', $affID)->get();
foreach ($Txs as $data) {
if(empty($data)) {
//do nothing
}else{
$count++;
//echo(dd($data));
$totalMAF += abs($data->owedMAF);
$percentageRef = ( 15 / 100 ) * $totalMAF;
$percentageFiat = ( 5 / 100 ) * ($totalMAF * .35);
$percentageBTC = round(( 5 / 100) * (($totalMAF * .35) / $currentBTC), 8);
$fiatValue = "$" . number_format(round(($totalMAF * .35), 2), 2);
}
}
}
?>
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Date Range</th>
<th scope="col">Confirmed Purchases</th>
<th scope="col">Total MAF Sold</th>
<th scope="col">Fiat Value of Sales</th>
<th scope="col">15% Referral Bonus</th>
<th scope="col">5% of Fiat/BTC</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $dateRange }}</td>
<td>{{ $count }}</td>
<td>{{ $totalMAF }} MAF</td>
<td>{{ $fiatValue }} USD</td>
<td>{{ $percentageRef }} MAF</td>
<td>{{ "$" . number_format(round(($percentageFiat), 2), 2) . " USD" }}<br />{{ $percentageBTC . " BTC" }}</td>
</tr>
</tbody>
</table>
<hr style="border: thin solid #2d2d2d;" />
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Todays Date</th>
<th scope="col">Total Confirmed Purchases</th>
<th scope="col">Total MAF Sold</th>
<th scope="col">Total Fiat Value of Sales</th>
<th scope="col">Total 15% Referral Bonus</th>
<th scope="col">Total 5% of Fiat/BTC</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ date('m-d-Y') }}</td>
<td>{{ $count }}</td>
<td>{{ $totalMAF }} MAF</td>
<td>{{ $fiatValue }} USD</td>
<td>{{ $percentageRef }} MAF</td>
<td>{{ "$" . number_format(round(($percentageFiat), 2), 2) . " USD" }}<br />{{ $percentageBTC . " BTC" }}</td>
</tr>
</tbody>
</table>
<hr style="border: thin solid #2d2d2d;" />
<div align="center"><a href="#" class="btn btn-primary btn-lg" role="button" aria-disabled="true">Download Report (CSV)</a></div>
</body>
</html>
我需要能够从创世时间开始每 24 小时添加一个新行。我想这样做而不必在数据库中进一步存储任何内容。任何帮助将不胜感激。
【问题讨论】:
-
您会跨越多个日期吗?您的日期/时间是否连续?
-
没有。每24小时按顺序进行。例如2018-8-13 21:00:00 - 2018-8-14 21:00:00, 2018-8-14 21:00:00 - 2018-8-15 21:00:00, 2018-8-15 21 :00:00 - 2018-8-16 21:00:00 以此类推。
-
您的时间是在给定时间段内排序的,还是混淆了.. 需要排序吗?也许在您的帖子中发布数据样本。
-
很遗憾,由于我的雇主的保密协议,我无法发布样本。日期/时间将按顺序排列,始终为 21:00:00 至 21:00:00。除了最旧的 24 小时到最新的时间段之外,不需要排序。
标签: php html laravel bootstrap-4