我有一个小服务台,我有一个要求,每月向我的经理出示机票报告。
我希望升级我们生成此报告的方式,因为目前,我使用Navicat运行每月手动编写的以下查询:
SELECT
updates.incidentid AS `Incident ID`,
updates.bodytext AS `Call Status`,
updates.duration AS `Total Minutes`,
software.`name` AS 'Support Type',
incidents.title AS Description,
contacts.forenames AS `First Name`,
contacts.surname AS `Last Name`,
FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
sites.`name`,
users.realname
FROM
updates
INNER JOIN incidents ON updates.incidentid = incidents.id
JOIN contacts ON incidents.contact = contacts.id
INNER JOIN sites ON sites.id = contacts.siteid
INNER JOIN users ON incidents.`owner` = users.id
INNER JOIN software ON incidents.softwareid = software.id
WHERE
updates.bodytext = 'Incident Closed'
AND FROM_UNIXTIME(incidents.opened, '%m') = '07'
AND FROM_UNIXTIME(incidents.opened, '%Y') = '2016'
ORDER BY contacts.siteid ASC我想把所有这些放在一个非常简单的网页中,允许用户选择月份和年份,然后提供一个HTML格式的下载。
我是把它放在PHP中的最佳位置,还是可以用HTML创建它?请善待我,我是个新手,我的思维过程有点混乱。
非常感谢您的时间和耐心!
发布于 2016-08-11 15:37:01
我将向您展示基本的内容:首先,一个HTML文件,其中用户以数字形式输入月份和年份(注意调用"report.php"):
<html>
<body>
<form method="post" action="report.php">
Enter month (1..12) <input type="text" name="month"/>
<br/>
Enter year (four digits) <input type="text" name="year"/>
<br/>
<input type="submit" value="Display report"/>
</form>
</body>
</html>现在,PHP文件"report.php“(注意在开头捕获月份和年份并将其存储在变量$month和$year中):
<?php
$month = $_POST["month"]; // PARAMETERS FROM
$year = $_POST["year"]; // THE HTML FILE.
$cnx = mysqli_connect( "localhost","user","password","databasename");
$data = mysqli_query( $cnx,"YOUR BIG SELECT HERE" ) or die( mysqli_error( $cnx ) );
echo "<table>" .
" <tr>" .
" <td>Incident id</td>" .
" <td>Call status</td>" .
" <td>Description</td>" .
" <td>Date logged</td>" .
" <td>Site name</td>" .
" <td>User</td>" .
" </tr>";
while ( $row = mysqli_fetch_array( $data ) ) // LOOP TO DISPLAY DATA.
echo "<tr>" .
" <td>{$row["IncidentID"]}</td>" .
" <td>{$row["CallStatus"]}</td>" .
" <td>{$row["Description"]}</td>" .
" <td>{$row["DateLogged"]}</td>" .
" <td>{$row["name"]}</td>" .
" <td>{$row["realname"]}</td>" .
"</tr>";
echo "</table>"; // TABLE END.
?>您必须将文本“您在这里的大选择”替换为您的大查询。这是如何在查询中插入变量$month和$year:
SELECT
updates.incidentid AS `Incident ID`,
updates.bodytext AS `Call Status`,
updates.duration AS `Total Minutes`,
software.`name` AS 'Support Type',
incidents.title AS Description,
contacts.forenames AS `First Name`,
contacts.surname AS `Last Name`,
FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
sites.`name`,
users.realname
FROM
updates
INNER JOIN incidents ON updates.incidentid = incidents.id
JOIN contacts ON incidents.contact = contacts.id
INNER JOIN sites ON sites.id = contacts.siteid
INNER JOIN users ON incidents.`owner` = users.id
INNER JOIN software ON incidents.softwareid = software.id
WHERE
updates.bodytext = 'Incident Closed'
AND FROM_UNIXTIME(incidents.opened, '%m') = '$month' ◄■■■■
AND FROM_UNIXTIME(incidents.opened, '%Y') = '$year' ◄■■■■
ORDER BY contacts.siteid ASC将先前的代码复制到两个名为"report.html“和"report.php”的文件中,然后在浏览器中打开"report.html“。
有很多东西需要改进,例如,使用jQuery,您可以改进用户进入月份和年份的方式,但这是另一个问题。
发布于 2016-08-11 14:59:51
您可以使用HTML (可能会利用javascript来访问选定的月份和年份值并将它们插入查询模板的字符串中)来生成SQL查询,但是如果您想通过服务器端运行查询,则最好使用PHP或另一种脚本语言。
发布于 2016-08-11 15:01:15
Mashymoo,如果你使用Navicat,你可以使用Navicat报表生成器。希望这能有所帮助。经理们喜欢报告!
https://stackoverflow.com/questions/38899864
复制相似问题