我目前有一个 sql 和 php 问题,我使用此语句从服务及其关联表中获取所有内容:
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select', 'a.*'
)
);
$query->from('`#__services_service` AS a');
$query->where("a.zone");
$query->select('zone.name AS zone, zone.description AS zone_description, zone.price AS zone_price, zone.interval_recommended AS interval_recommended');
$query->join('LEFT', '#__services_zones AS zone ON zone.id = a.zone');
$query->select('category.icon AS category');
$query->join('LEFT', '#__services_categories AS category ON category.id = a.category');
但是在我加入的区域表中,有逗号分隔值“1,2”,并且在创建了一个关联数组后,我将服务分组到它们的区域,一个单一的服务只进入一个单一的区域,而不是当表中的值是 '1,2' 它应该在两个区域中......我有一种感觉是因为'zone.name AS zone'
,因为它只返回一个名字,如果我说它'zone AS zone'
返回逗号分隔的数字......
这是我对数组进行排序的方式:
foreach( $this->items as $item ){
if( !isset( $zones[ $item->zone ] ) )
$zone_items[ $item->zone] = array();
$zone_items[ $item->zone ][] = $item;
$zones[$item->zone] = array(
'zone' => $item->zone,
'zone_interval' => $item->interval_recommended,
'zone_description' => $item->zone_description,
'zone_price' => $item->zone_price,
'items' => $zone_items[$item->zone]
);
}
我通过以下方式显示它:
<?php foreach( $zones as $zone): ?>
<div class="row-fluid zones-page">
<div class="row-fluid zone-title-block">
<div class="span4 zone-name"><h1><?php echo $zone['zone']; ?></h1></div>
<div class="span7 zone-description">
<?php echo $zone['zone_description']; ?>
<?php if($zone['zone_interval'] == 1): ?>
<span class="interval pull-right">Interval Recommended</span>
<?php endif; ?>
</div>
</div>
<?php foreach( $zone['items'] as $items ) :?>
<div class="row-fluid zone-services-contain">
<div class="span1"><div class="zone-icon"><?php //echo $items->category; ?></div></div>
<div class="span11 zone-services">
<h4><?php echo $items->name; ?></h4>
<div><?php echo $items->description; ?></div>
</div>
</div>
<?php endforeach; ?>
<div class="row-fluid zone-amount">
only <div><?php echo 'R' . $zone['zone_price']; ?></div>
</div>
</div>
<div class="bicycle-divider">
<div class="bicycle-icon"></div>
</div>
<?php endforeach; ?>
任何帮助是极大的赞赏。
该Services
表如下所示:
和Zones
表: