我在php页面中使用了两个单独的数组。
第一个包含我将用于在UI上创建html表头的所有字段名称。
此数据的数组如下所示:
Array
(
[0] => Array
(
[fieldID] => 2
[fieldName] => Project Title
[fieldAlias] => initiativeTitle
)
[1] => Array
(
[fieldID] => 4
[fieldName] => Project Description (preview)
[fieldAlias] => initiativeDescriptionPreview
)
)
接下来,我有一个数据集,包含我需要打印到表中的所有记录。此数组中的key
与标题数组中的fieldAlais
匹配。
我的目标是遍历标头数组并获取fieldAlias
,然后遍历数据,当标题行中的fieldAlias
与数据行中的键匹配时,它会打印它出来了。
以下是我填充标题数组的方法:
$primaryArray = Array();
if(isset($dashboardDetails->results->primary->fields)){
foreach($dashboardDetails->results->primary->fields as $p){
$primaryArray[] = array(
'fieldID' => (int)$p->fieldID,
'fieldName' => (string)$p->fieldName,
'fieldAlias' => (string)$p->alias
);
}
}
这是数据对象的一个示例:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[initiativeDescriptionPreview] => This is a test description
[initiativeTitle] => Test
)
以下是我在HTML表格中使用的混乱:
<table class="table table-hover table-striped">
<thead>
<tr>
<?php
// Loop over the primary fields
for ($i = 0; $i < count($primaryArray); ++$i) {
echo '<th class="small">'.$primaryArray[$i]['fieldName'].'</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
// For each field in our primary array
for ($i = 0; $i < count($primaryArray); ++$i) {
// Set our alais
$a = $primaryArray[$i]['fieldAlias'];
echo '<tr>';
// Loop over all of the records
foreach($dashboard->data as $value){
foreach($value as $key => $val){
if($key == $a){
echo '<td class="small">'.$val.'</td>';
}
}
}
echo '</tr>';
}
?>
</tbody>
</table>
这样做的结果是当它应该是同一行时打印两行数据:
这样做的最后一点是:我有两个独立的对象,标题和数据。我需要打印表头,然后将数据从另一个数组打印到相应的标题。
答案 0 :(得分:1)
首先,不清楚数据数组是否是一个数组数组,应该是它。
然后,您可以循环数据数组,其值是您正在使用的行。然后你可以循环头数组,并打印出行数组的元素,该元素的键与你当前所在的headers数组元素的'fieldAlias'元素的值相匹配。
一个例子:
$headers = Array(
Array(
'fieldID' => 2,
'fieldName' => 'Project Title',
'fieldAlias' => 'initiativeTitle'
),
Array(
'fieldID' => 4,
'fieldName' => 'Project Description (preview)',
'fieldAlias' => 'initiativeDescriptionPreview'
)
);
$results = new StdClass();
$results->data = Array(Array('initiativeDescriptionPreview' => 'This is a test description', 'initiativeTitle' => 'Test'));
?>
<table cellspacing="10" cellpadding="10">
<tr>
<?php foreach($headers as $headerField): ?>
<th style="border:1px solid red">
<?php echo $headerField['fieldName']; ?>
</th>
<?php endforeach; ?>
</tr>
<?php foreach($results->data as $row): ?>
<tr>
<?php foreach($headers as $headerField): ?>
<td>
<?php echo $row[$headerField['fieldAlias']]; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
答案 1 :(得分:0)
在我看来,您为每个列创建了一个新的<tr>
元素。
我认为您的HTML看起来像这样:
<table class="table table-hover table-striped">
<thead>
<tr>
<th class="small">some name</th>
<th class="small">some other name</th>
</tr>
</thead>
<tbody>
<tr>
<td>value of column 1</td>
</tr>
<tr>
<td>value of column 2</td>
</tr>
</tbody>
</table>
请注意,您可能只希望<tr>
<tbody>
元素
答案 2 :(得分:0)
我尝试使用直接PHP代码创建数组和对象,以便我可以测试我的方法。我认为你根本不需要$ primaryArray。这是我的完整解决方案:
<?php
$headers = array(
array("fieldID" => 2, "fieldName" => "Project Title", "fieldAlias" => "initiativeTitle"),
array("fieldID" => 4, "fieldName" => "Project Description (preview)", "fieldAlias" => "initiativeDescriptionPreview"),
);
class SimpleXMLElementObject {
public
$data;
}
$mySimpleXMLElementObject1 = new SimpleXMLElementObject;
$mySimpleXMLElementObject2 = new SimpleXMLElementObject;
$mySimpleXMLElementObject1->data = array(
"initiativeDescriptionPreview" => "This is a test description",
"initiativeTitle" => "Test"
);
$mySimpleXMLElementObject2->data = array(
"initiativeDescriptionPreview" => "This is a test description2",
"initiativeTitle" => "Test2"
);
$mySimpleXMLElementObjects = array();
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject1;
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject2;
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<table class="table table-hover table-striped">
<thead>
<?php foreach($headers as $header) { ?>
<th class="small"><?= $header["fieldName"] ?></th>
<?php } ?>
</thead>
<tbody>
<?php foreach($mySimpleXMLElementObjects as $anObject) { ?>
<tr>
<?php foreach($headers as $header) { ?>
<td class="small"><?= $anObject->data[$header["fieldAlias"]] ?></th>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
结果表如下:
<html>
<head>
<title>Test</title>
</head>
<body>
<table class="table table-hover table-striped">
<thead>
<th class="small">Project Title</th>
<th class="small">Project Description (preview)</th>
</thead>
<tbody>
<tr>
<td class="small">Test</th>
<td class="small">This is a test description</th>
</tr>
<tr>
<td class="small">Test2</th>
<td class="small">This is a test description2</th>
</tr>
</tbody>
</table>
</body>
</html>