我曾经有一个类似的表,最后我使用一些 SQL 将数据作为单独的行返回。我已经修改了我的对象以使用您的 sql 并编写了一个简单的函数,它将按照您在问题中使用的格式在表中输出您的数据。
我使用的主要查询聚合函数是 MySQL GROUP_CONCAT。这类似于一个implode()
函数。默认分隔符是逗号,因此如果您的元数据中有逗号,您可以通过SEPARATOR 'yourSeparator'
在列名称后面的括号内添加来将其更改为不同的标记。您还可以使用distinct
列名的前面来仅选择 group_concat 中的不同行。
我已将表头部分放在try
语句中,这样如果您没有任何结果,您将不会显示半生成的表。
<?php
class myUser
{
public $ID;
public $name;
public $metas;
}
class myTables
{
public $SQL="
select
ID,
name,
GROUP_CONCAT(metavalue) as metas
FROM
table1
GROUP BY
ID,
name;";
public function outputTable()
{
$hostname="mysql:host=localhost;dbname=test";
$username="testdb";
$password="testdbpassword";
try{
echo "
<table>
<thead>
<th>Name</th>
<th>MetaValue</th>
</thead>
<tbody>
";
$dbh = new PDO($hostname, $username, $password);
$stmt = $dbh->query($this->SQL);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new myUser);
foreach($stmt as $myUser)
{
echo "
<tr><td>".$myUser->name."</td><td><ul>";
$metas=explode(",", $myUser->metas);
for($i=0;$i<count($metas);$i++)
{
echo "<li>".$metas[$i]."</li>";
}
echo "</ul></td></tr>";
}
unset($obj);
unset($stmt);
unset($dbh);
echo "
</tbody>
</table>
";
}
catch(PDOException $e){
echo 'Error : '.$e->getMessage();
exit();
}
}
}
$myPage= new myTables();
$myPage->outputTable();
?>
示例输出:
<table>
<thead>
<th>Name</th>
<th>MetaValue1</th>
</thead>
<tbody>
<tr><td>Rose</td><td><ul><li>Drinker</li><li>Nice Person</li><li>Runner</li></ul></td></tr>
<tr><td>Gary</td><td><ul><li>Player</li><li>Funny</li></ul></td></tr>
</tbody>
</table>
我从您的查询中删除了Autonum
,否则它会破坏聚合函数。但是,如果您确实需要它,则必须以类似的方式对其进行聚合。该group_concat
函数将跳过空字段,因此您需要巧妙地将它们带入,否则您的自动编号 ID 将与您的结果不匹配。我在这里用一个简单coalesce()
的内部group_concat
函数做到了这一点。
为了容纳这些额外的部分,我稍微重写了对象。这应该可以满足您的所有需求,我已经留下了由我设置为 false 的私有变量设置的调试说明isDebug
,但是将其设置为 true 会在对象运行函数时为您提供额外的信息。这将帮助您进行调试,因为我假设您的源数据实际上要复杂得多。
<?php
class myUser
{
public $ID;
public $name;
public $metaDesc;
public $metaNum;
public $metaDescs;
public $metaNums;
}
// Basic User stored here. One Object per Row is created.
class myTables
{
private $isDebug=false; // Change this to true to get all validation messages;
private $hostname="mysql:host=localhost;dbname=test";
private $username="testdb";
private $password="testdbpassword";
private $myUsers=array();
private $curUser = myUser;
private $userCount=0;
private $SQL="
select
ID,
name,
GROUP_CONCAT(coalesce(metavalue,'null')) as metaDesc,
group_concat(autonum) as metaNum
FROM
table1
GROUP BY
ID,
name;";
public function getuserData()
{
$dbh = new PDO($this->hostname, $this->username, $this->password);
$stmt = $dbh->query($this->SQL);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new myUser);
$userCount=0;
foreach($stmt as $myUser)
{
$this->myUsers[$userCount]=new myUser;
$this->myUsers[$userCount]->ID=$myUser->ID;
$this->myUsers[$userCount]->name=$myUser->name;
$this->myUsers[$userCount]->metaDesc=$myUser->metaDesc;
$this->myUsers[$userCount]->metaNum=$myUser->metaNum;
$userCount++;
}
$this->userCount=$userCount;
if($this->isDebug){echo "There are ".$this->userCount." users found.<br>";}
unset($obj);
unset($stmt);
unset($dbh);
}
// Pulls the data from the database and populates the this->object.
public function outputTable()
{
echo "
<table>
<thead>
<th>Name</th>
<th>MetaValue</th>
</thead>
<tbody>
";
for($i=0; $i<$this->userCount; $i++)
{
if($this->isDebug){echo "Running main cycle. There are ".$this->userCount." elements.";}
$this->myUsers[$i]->metaDescs=explode(',', $this->myUsers[$i]->metaDesc);
$this->myUsers[$i]->metaNums=explode(',', $this->myUsers[$i]->metaNum);
if($this->isDebug){echo "This user has ".(count($this->myUsers[$i]->metaDescs))." segments<br>";}
if($this->isDebug){echo "My first segment is ".($this->myUsers[$i]->metaDesc)."<br>";}
echo "<tr><td>".$this->myUsers[$i]->name."</td><td><ul>";
for($j=0;$j<count($this->myUsers[$i]->metaDescs);$j++)
{
echo "<li>ID: ".$this->myUsers[$i]->metaNums[$j]." - ".$this->myUsers[$i]->metaDescs[$j]."</li>";
}
echo "</ul></td></tr>";
}
echo "
</tbody>
</table>
";
}
// Outputs the data held in the object into the table as required.
}
$myPage= new myTables();
$myPage->getUserData();
$myPage->outputTable();
?>
输出现在如下所示:
<table>
<thead>
<th>Name</th>
<th>MetaValue</th>
</thead>
<tbody>
<tr><td>Rose</td><td><ul><li>ID: 1 - Drinker</li><li>ID: 2 - Nice Person</li><li>ID: 3 - Runner</li></ul></td></tr>
<tr><td>Gary</td><td><ul><li>ID: 4 - Player</li><li>ID: 5 - Funny</li><li>ID: 6 - null</li><li>ID: 7 - Smelly</li></ul></td></tr>
</tbody>
</table>
Name MetaValue
Rose
ID: 1 - Drinker
ID: 2 - Nice Person
ID: 3 - Runner
Gary
ID: 4 - Player
ID: 5 - Funny
ID: 6 - null
ID: 7 - Smelly