だから私は地元のコラージュで学生の仕事をしていて、与えられたトピックでウェブサイト全体を作成する必要があります. 駐車場のトピックを選択し、立ち往生しました。立ち往生していて、どこで問題を探すべきかわかりません。私がする必要があるのは、データベースから特定の年と月のデータをフィルタリングすることです。これは次のようになります。
そして、このテーブルからデータを並べ替えるための私のクエリは次のようになります。
SELECT COUNT(parking_time) AS "Amont", SUM(sum) AS "Sum"
FROM parking_info
GROUP BY MONTH(parking_time), YEAR(parking_time).
そして、このクエリからの出力:
ご覧のとおり、ユーザーは DROPDOWN リストから年と月を選択して、特定の月を選択し、MySQL クエリからデータをフィルタリングする必要があります。例: ユーザーがドロップダウン 10月から2013 年と月を選択した場合、特定の月の情報 ( Amount 4 / Sum 170) のみをユーザーに出力する必要があります。私のウェブページ)。
基本的に、すべてのカウント、フィルター、および SQL クエリを保持するfunctions.phpファイルがあります。この特定のページのfunctions.phpファイルは次のようになります。
<?php
function cleanFilterArray($filters) {
$new_filters = array();
if ($filters) {
foreach ($filters as $field => $data) {
if ($data != '%%') {
$new_filters[$field] = $data;
}
}
return $new_filters;
} else {
return false;
}
}
function get_months_in_LT()
{
$months = array();
$months[] = array(1, 'january');
$months[] = array(2, 'february');
$months[] = array(3, 'march');
$months[] = array(4, 'april');
$months[] = array(5, 'may');
$months[] = array(6, 'june');
$months[] = array(7, 'july');
$months[] = array(8, 'august');
$months[] = array(9, 'september');
$months[] = array(10, 'october');
$months[] = array(11, 'november');
$months[] = array(12, 'december');
return $months;
}
function get_month_in_LT($num)
{
$months = get_months_in_LT();
return $months[$num][1];
}
function get_month_report($filters) {
$where = null;
if ($filters) {
$filters = cleanFilterArray($filters);
foreach ($filters as $field => $data) {
if (!empty($data)) {
$where .= $field.' \''.$data.'\' AND ';
}
}
if ($where) {
$where = 'WHERE '.substr($where, 0, -5); //removes last 'AND' and adds a 'WHERE' to the begining
}
}
var_dump($where);
var_dump($filters);
$result = mysql_query('
SELECT COUNT(parking_time) AS "Amont", SUM(sum) AS "Sum"
FROM parking_info
GROUP BY MONTH(parking_time), YEAR(parking_time)
'.$where.'
');
$rows = array();
while ($row = mysql_fetch_row($result))
{
$rows[] = $row;
}
return $rows;
}
?>
そして、すべての HTML と他のすべてのものを保持する私の実際のページは次のようになります。
<?php
include_once 'functions.php';
if (isset($_GET['submit']))
{
$year = $_GET['year'];
$month = $_GET['month'];
}
else
{
$year = date('Y');
$month = date('m');
}
if (isset($_GET['submit']))
{
if (isset($year) && $year > (int) date('Y'))
{
$error = 'this years has not yet come!';
}
if (isset($month) && $month > (int) date('m'))
{
$error = 'this month has not yet come';
}
elseif (isset($year) && $year < 0)
{
$error = 'date cannot be negative';
}
elseif (isset($month) && ($month < 1 || $month > 12))
{
$error = 'not a month';
}
}
if (isset($_GET['submit']))
{
$year = $_GET['year'];
$month = $_GET['month'];
$filters = array(
"parking_time LIKE" => '%'.$year.'%',
"parking_time LIKE" => '%'.$month.'%'
);
}
include_once 'mysql.php';
if (isset($_GET['submit']) && !isset($error)){
$infos = get_month_report($filters);
}
$months = get_months_in_LT();
var_dump($infos);
?>
<?php include_once 'header.php' ?>
<h1>Month Report</h1>
<div id="main">
<p>How much per month was observed cars and what is the total amount of money collected</p>
<?php
if (isset($error))
{
echo "<p class=\"error\">Klaida: $error</p>";
}
?>
<form method="post">
<table border="1">
<tr>
<th>Year<span style="color:red">*</span></th>
<td><input type="text" name="year" value="<?php if (isset($year)) echo $year ?>" /></td>
</tr>
<tr>
<th>Month</th>
<td>
<select name="month" style="width:100%">
<?php
foreach ($months as $montha)
{
if (isset($month) && $month == $montha[0])
{
echo "<option value=\"$montha[0]\" selected=\"selected\">$montha[1]</option>";
}
else
{
echo "<option value=\"$montha[0]\">$montha[1]</option>";
}
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" class="search"><button type="submit" name="submit" style="width:100%">Search</button></td>
</tr>
</table>
</form>
<br />
<br />
<?php if (isset($_GET['submit']) && sizeof($error) == 0): ?>
<table border="1">
<tr>
<th>Amount</th>
<th>Sum, LT</th>
</tr>
<?php
foreach ($infos as $info)
{
echo "<tr>";
echo "<td>$info[0]</td>";
echo "<td>$info[1]</td>";
echo "</tr>";
}
if (count($infos) == 0)
{
echo "<tr><td colspan=\"2\">Rezultatų nėra.</td></tr>";
}
?>
</table>
<?php endif; ?>
<br/>
<br/>
<br/>
<br/>
</div>
<?php include_once 'footer.php' ?>
私が立ち往生したばかりで、このページが何も出力しない理由がわからないので、誰かが私が実際に必要としているものを理解してくれることを願っています。ありがとうございました。