在下面的代码中,我发送了一个请求,我使用 PDO 提取将结果放入一个表中。但是当我们有一个包含 2 个元素的“数据元素”时,它会在表中创建 2 行,每个“元素”对应 1 行。例子:
****************************************
**Data Element Name** | data 1
****************************************
**Elements** | element 1
****************************************
****************************************
**Data Element Name** | data 1
****************************************
**Elements** | element 2
****************************************
and what I want is:
****************************************
**Data Element Name** | data 1
****************************************
**Elements** |element 1
|element 2
****************************************
谢谢!
<?php
if ($select=="Data Element Name"){
$result="SELECT
D1.Data_Element_Name dataElementName,
D2.Data_Element_Name element
FROM
data_element_name D1
LEFT JOIN packagedelement ON packagedelement.id=COALESCE(D1.base_Attribute, D1.base_Class)
LEFT JOIN ownedattribute ON ownedattribute.FK_packagedElement=packagedelement.PrimaryKey
LEFT JOIN data_element_name D2 ON D2.base_Attribute=ownedattribute.id
WHERE
D1.Data_Element_Name LIKE '%$search%' ";
try {
$req =$pdo->query($result);
} catch(PDOException $ex) {
echo "An Error occured!"; //user friendly message
some_logging_function($ex->getMessage());
}
echo 'Data Elements including the keyword: " '.$search.' " ';
echo '<table id="table1" style="margin-left:10px; width:700px;">';
//Loop to stock results on an array
$i=0;
while($data = $req->fetch())
{
$array[$i]=$data['dataElementName'];
$element[$i]=$data['element'];
echo '<tr>
<td>
<a href="" onclick="var source = ($(\'#arrow'.$i.'\').attr(\'src\') === \'img/arrowInactive.jpg\') ? \'img/arrowActive.jpg\' : \'img/arrowInactive.jpg\'; $(\'#arrow'.$i.'\').attr(\'src\', source); $(\'#hiddenDiv'.$i.'\').toggle(); return false;">
<img id="arrow'.$i.'" src="img/arrowInactive.jpg" alt="arrowInactive" />' .json_encode($array[$i]).
'</a>
</td>
</tr>
<tr id="hiddenDiv'.$i.'" style="padding-left:20px;display:none">
<td>
<table>
<tbody>
<tr>
<td class="LeftCell">Elements</td>
<td id="elements">'.json_encode($element[$i]).'</td>
</tr>
</tbody>
</table>
</td>
</tr>';
$i=$i+1;
}
echo '</table>';
}
?>