PHP 循环遍历结果数组,只想回显某些项目一次
PHP Looping through an array of results, only want to echo certain items once
我正在遍历一组结果,但是结果中有些行具有重复的名称。有没有办法只回显这个名字一次。解决方案可能很简单,但此刻我很困惑。
代码如下:
$service_query = $this->db->query('SELECT service_id, server_id, port, comment FROM services');
foreach ($service_query->result() as $service)
{
$server_query = $this->db->query('SELECT server_id, name, host FROM servers WHERE server_id =' . $service->server_id);
foreach ($server_query->result() as $server)
{
echo $server->name;
echo $service->comment;
echo $server->host;
echo $service->port;
echo '<br>';
}
}
结果如下:
Mail Server IMAP (Secure) XXX.XXX.XXX.XXX:993
Mail Server POP3 (Secure) XXX.XXX.XXX.XXX:995
Mail Server POP3 XXX.XXX.XXX.XXX:110
Mail Server IMAP XXX.XXX.XXX.XXX:143
Web Server Apache XXX.XXX.XXX.XXX:80
仅供参考,我正在为这个项目使用 codeigniter 框架。
你的意思是 "Mail Server" 数据重复了?如果是这样,请考虑在查询中使用 MySQL DISTINCT
来删除重复项,请参阅示例 here.
据我了解您需要什么。就像
它在您的 SQL 语法中
改变它
'SELECT server_id, name, host FROM servers WHERE server_id =' . $service->server_id .' GROUP BY name'
试试这个:
$query = $this->db->query('SELECT name, service_id, server_id, host, port, comment
FROM services
LEFT JOIN servers USING (server_id)
ORDER BY servers.name');
$server_name = '';
foreach ($query->result() as $server)
{
if($server_name != $server->name) {
echo $server->name;
$server_name = $server->name;
}
else { echo '-/-/-' };
echo $server->comment;
echo $server->host;
echo $server->port;
echo '<br>';
}
我通过在 $service 之前查询数据库中的 $server 并在前面的循环中回显 $server->name 来解决这个问题。这是我的解决方案。感谢大家的快速回答。
$server_query = $this->db->query('SELECT server_id, name, host FROM servers');
foreach ($server_query->result() as $server)
{
echo $server->name;
$service_query = $this->db->query('SELECT service_id, server_id, port, comment FROM services WHERE server_id =' . $server->server_id);
foreach ($service_query->result() as $service)
{
echo $service->comment;
echo $server->host;
echo $service->port;
echo '<br>';
}
}
我正在遍历一组结果,但是结果中有些行具有重复的名称。有没有办法只回显这个名字一次。解决方案可能很简单,但此刻我很困惑。
代码如下:
$service_query = $this->db->query('SELECT service_id, server_id, port, comment FROM services');
foreach ($service_query->result() as $service)
{
$server_query = $this->db->query('SELECT server_id, name, host FROM servers WHERE server_id =' . $service->server_id);
foreach ($server_query->result() as $server)
{
echo $server->name;
echo $service->comment;
echo $server->host;
echo $service->port;
echo '<br>';
}
}
结果如下:
Mail Server IMAP (Secure) XXX.XXX.XXX.XXX:993
Mail Server POP3 (Secure) XXX.XXX.XXX.XXX:995
Mail Server POP3 XXX.XXX.XXX.XXX:110
Mail Server IMAP XXX.XXX.XXX.XXX:143
Web Server Apache XXX.XXX.XXX.XXX:80
仅供参考,我正在为这个项目使用 codeigniter 框架。
你的意思是 "Mail Server" 数据重复了?如果是这样,请考虑在查询中使用 MySQL DISTINCT
来删除重复项,请参阅示例 here.
据我了解您需要什么。就像
它在您的 SQL 语法中
改变它
'SELECT server_id, name, host FROM servers WHERE server_id =' . $service->server_id .' GROUP BY name'
试试这个:
$query = $this->db->query('SELECT name, service_id, server_id, host, port, comment
FROM services
LEFT JOIN servers USING (server_id)
ORDER BY servers.name');
$server_name = '';
foreach ($query->result() as $server)
{
if($server_name != $server->name) {
echo $server->name;
$server_name = $server->name;
}
else { echo '-/-/-' };
echo $server->comment;
echo $server->host;
echo $server->port;
echo '<br>';
}
我通过在 $service 之前查询数据库中的 $server 并在前面的循环中回显 $server->name 来解决这个问题。这是我的解决方案。感谢大家的快速回答。
$server_query = $this->db->query('SELECT server_id, name, host FROM servers');
foreach ($server_query->result() as $server)
{
echo $server->name;
$service_query = $this->db->query('SELECT service_id, server_id, port, comment FROM services WHERE server_id =' . $server->server_id);
foreach ($service_query->result() as $service)
{
echo $service->comment;
echo $server->host;
echo $service->port;
echo '<br>';
}
}