简体   繁体   English

使用PHP在HTML表格单元中输出MySQL查询

[英]Output MySQL query in HTML table cell with PHP

I'm working in the IT department of my company and it is my responsibility to send daily reports of the calls registered in the Call Center department. 我在公司的IT部门工作,我有责任每天发送在呼叫中心部门注册的呼叫的报告。 I'm new with MySQL, PHP or HTML and I'm already facing my first problems. 我是MySQL,PHP或HTML的新手,我已经遇到了第一个问题。

My objective is to run some queries in PHP and then output them in a HTML table cell. 我的目标是在PHP中运行一些查询,然后将它们输出到HTML表单元格中。 Let me be more specific. 让我更具体一点。 I have access to a telephone record database and I need to count the number of total calls and lost calls of every branch daily. 我可以访问电话记录数据库,并且需要每天计算每个分支机构的总呼叫数和丢失呼叫数。 Right now I just run 2 queries in MySQL and then copy-paste them in a .xls table. 现在,我只在MySQL中运行2个查询,然后将它们复制并粘贴到.xls表中。

I have the right queries, I get the right result in mysql, even in PHP I get the result posted on the webpage (kind of), but I don't know how to create a table, or how to input the result of a query into a specific cell of the table. 我有正确的查询,在mysql中也得到了正确的结果,即使在PHP中,我也得到了发布在网页上的结果(有点),但是我不知道如何创建表或如何输入a的结果查询表的特定单元格。

Here's what I've got so far: 到目前为止,这是我得到的:

<?php

$conn=@mysql_connect('host', 'username', 'password', 'asteriskcdrdb.cdr');
echo ('total calls ROPCW: ');
$result=mysql_query('select count(*) from asteriskcdrdb.cdr where calldate like "2016-04-11%" and dst="020" and disposition="ANSWERED" and duration > "10" ');
echo mysql_result($result, 0);

mysql_close($conn);
?>

This code will post on my page the count of total calls made on 4.11.2016 like this: total calls ROPCW: 369 该代码将在我的页面上发布2016年11月4日的总呼叫次数,如下所示:总呼叫ROPCW:369

My objective is to create a table like this: 我的目标是创建一个像这样的表:

图片

So the result of that query I mentioned above would go on the ROPCW->Total cell (where is 305) on the left side, on the right side are the monthly reports so far. 因此,我上面提到的查询结果将显示在左侧的ROPCW-> Total单元格(此处为305)上,右侧是到目前为止的月度报告。

You can work out one big query or use a view to gather the data you need from a single source. 您可以制定一个大型查询,也可以使用视图从单一来源收集所需的数据。 Other suggestion would be, looping through all your branches and gather all the data you need, to build an associative array indexed similar to this: 其他建议是,遍历所有分支并收集所需的所有数据,以建立索引类似于以下的关联数组:

$records[branch] = [date => [total => x, lost => y], mtd => [total => X, lost => Y]];

Then just do a: 然后执行以下操作:

foreach ($records as $branch => $record)
{
     echo string to build table row
}

I suggest use mysql_fetch_object or mysql_fetch_assoc functions instead of mysql_result. 我建议使用mysql_fetch_object或mysql_fetch_assoc函数而不是mysql_result。 Basically because those functions fetch an entire row instead of one single cell value. 基本上是因为这些函数获取整行而不是单个单元格值。 For your example query works fine because your aggregate function returns only one cell and row. 对于您的示例,查询工作正常,因为您的聚合函数仅返回一个单元格和一行。 But for your desired output won't work. 但是对于您想要的输出将不起作用。 Keep in mind that you can inject HTML code within PHP and vice versa. 请记住,您可以在PHP中注入HTML代码,反之亦然。 Just do something like: 只需执行以下操作:

The idea would be run a select * from yourTable; 这个想法将在yourTable中运行select *; Then, 然后,

$result = mysql_query($sql);
echo "<table>\n";
echo "<tr><th>Branch</th></tr>\n";
while ($callRecord = mysql_fetch_object($result)) {
    echo "<tr><td>{$callRecord->branch}</td></tr>\n";
}
echo "</tr>\n";
echo "</table>\n";

That should output a list of all tour branches. 那应该输出所有巡回分支的列表。 this is a guideline. 这是一个准则。 Hope this helps!! 希望这可以帮助!!

You can go to www.php.net and choose between mysql_fetch functions which one will work better for you. 您可以访问www.php.net并在mysql_fetch函数之间进行选择,其中最适合您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM