[英]Matching columns from one table with another
Sorry for the confusing title, let me try to explain. 抱歉,标题令人困惑,让我尝试解释一下。 The question relates to custom logging software where a company can design their own 'log'.
问题与定制日志软件有关,公司可以在其中设计自己的“日志”。 Each log has a corresponding metadata table that has the columns from their log in it.
每个日志都有一个对应的元数据表,其中包含其日志中的列。
I am using php to render out a display of the data in the log. 我正在使用php渲染日志中的数据显示。 I first access the column names in the meta table and use that to build the table headers with a foreach as shown
我首先访问元表中的列名,并使用它通过一个foreach构建表头,如下所示
foreach ($logStructure as $header)
{
echo '<th>' . $header['label'] . '</th>';
}
endforeach;
Now I have done another select from the actual log for the rows to display and have that in an array call $logrow. 现在,我从实际日志中进行了另一个选择,以显示要显示的行,并将其放在数组中,调用$ logrow。 How do I go about iterating through those rows and putting the right data in the right column?
如何遍历这些行并将正确的数据放在正确的列中? In SQl I would have used a WHERE Clause to say something to the equivalent of the following:
在SQl中,我将使用WHERE子句来表达以下内容:
'WHERE $logrow['fieldname'] = $header['label']'
As an example the metatable fields look like this 举例来说,元表字段如下所示
[metaid] [columnLabel] [columnNameInData]
001 Log Number logNum
002 Author CreateUser
003 Subject Entry
The data gets put into the $header[] via a statement like such 数据通过这样的语句放入$ header []中
SELECT columnLabel AS label FROM metatable;
The Log Structure Looks Like This 日志结构看起来像这样
[logNum] [CreateUser] [Entry]
001 Admin Some Text about Stuff
002 Editor 1 Another Amazing Entry
003 Editor 2 It gets old by now
So the select looks like this 所以选择看起来像这样
SELECT logNum, CreateUser, Entry FROM log
The data gets put into $logrow['lognum'], $logrow['CreateUser'], $logrow['Entry']
数据放入
$logrow['lognum'], $logrow['CreateUser'], $logrow['Entry']
Hopefully this clears the question up a little better, I'm looking to match the column names in metadata with the values from those fields in the actual data. 希望这可以更好地解决问题,我希望将元数据中的列名与实际数据中这些字段的值相匹配。
If the sequence is gonna be always the same, and you are only trying to render it in the same table, I don't see what's stopping you to read both out into 2 separate arrays, then plot it onto the table. 如果序列总是相同的,而您只想在同一张表中呈现它,我看不出是什么使您无法将它们读入2个单独的数组,然后将其绘制到表上。
First this: 首先这个:
SELECT columnNameInData FROM metatable ORDER BY metaid;
put the result into an array $sort
将结果放入数组
$sort
Then implode it and build your second select clause: 然后将其内爆并构建第二个select子句:
$columns=implode(', ', $sort);
$SQL="SELECT $columns FROM log";
And read the result into array $result2
并将结果读入数组
$result2
Finally, assemble the header you have and the output of the $result2 as the rows and tds of logs, this table should have the records aligned with the headers. 最后,将您拥有的标题和$ result2的输出组装为日志的行和tds,此表应使记录与标题对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.