1

我想显示添加到数据库中的所有属性的列表,但每次我尝试一些它都不起作用。我想在首页的 div 中显示这个。我试图将其插入到 features.tpl:

<?php 
foreach ($attribute_groups as $attribute_group) {
echo $attribute_group['name'];
print_r($attribute_group);
echo '<select name="listaGrupe">';
foreach ($attribute_groups['attribute'] as $attribute) {
echo '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo '</select>';
}

?>
4

3 回答 3

1

你需要使用这个模型

public function getAttributes($data = array()) {
    $sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int)$this->config->get('config_language_id') . "'";

    if (!empty($data['filter_name'])) {
        $sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
    }

    if (!empty($data['filter_attribute_group_id'])) {
        $sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
    }

    $sort_data = array(
        'ad.name',
        'attribute_group',
        'a.sort_order'
    );  

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
        $sql .= " ORDER BY " . $data['sort'];   
    } else {
        $sql .= " ORDER BY attribute_group, ad.name";   
    }   

    if (isset($data['order']) && ($data['order'] == 'DESC')) {
        $sql .= " DESC";
    } else {
        $sql .= " ASC";
    }

    if (isset($data['start']) || isset($data['limit'])) {
        if ($data['start'] < 0) {
            $data['start'] = 0;
        }               

        if ($data['limit'] < 1) {
            $data['limit'] = 20;
        }   

        $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
    }   

    $query = $this->db->query($sql);

    return $query->rows;
}

在您的控制器中只需加载此模型并使用它返回的数据进行操作。默认情况下它返回数据库中的所有属性

于 2012-12-18T05:01:38.667 回答
0

检查您的控制器..如果 $attribute_groups 中有数据....

print_r($attribute_groups);

看看..确保您的 $attribute_groups 数组不为空...我认为您收到该错误,因为您的 $attribute_groups 数组为空...

如果您确定,$attribute_groups 可以为空,那么您可以检查...

if(!empty($attribute_groups)){
foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
}

这将查看您的数组是否为空。如果不是,则 foreach()... 否则什么也不做

于 2012-09-24T14:19:28.063 回答
0

您可能正在寻找更接近此的函数格式:

foreach ($attribute_groups as $attribute_group) {
$options = '';
$name = $attribute_group['name'];
$output = '<select name="$name">';
foreach ($attribute_groups['attribute'] as $attribute) {
$options .= '<option value="'.$attribute.'">'.$attribute.'</option>';
}
echo $output.$options.'</select>';
}
于 2012-09-24T13:55:05.667 回答