Question

We are fetching information from database and displaying category ids, but we want to display category names instead of ids.

enter image description here

now in 2nd dropdown box, instead of "category id" , we want to display category names.

foreach ($categories as $category){
$cat_id = $category["category_id"];

full phtml

public function getOptions()
    {
       $resource = Mage::getSingleton('core/resource');

$readConnection = $resource->getConnection('core_read');

$writeConnection = $resource->getConnection('core_write');

$readConnection = $resource->getConnection('core_read');

$query = 'SELECT category_id, category_name FROM ' . $resource->getTableName('outthink_advance_brand_category');

$categories = $readConnection->fetchAll($query);

$brandsArr = array();

$brandSelect = '<select id="brand_select">';
$brandSelect .= '<option value="">My Brand</option>';
foreach ($categories as $category){
$cat_id = $category["category_id"];

$brandSelect .= "<option value='".$category["category_id"]."'>".$category["category_name"]."</option>";

$query = 'SELECT `outthink_advance_brand`.brand_id, `outthink_advance_brand`.originalcategory_id FROM `outthink_advance_brand` ';
$query .= ' inner join `outthink_advance_brand_category_item` on
(`outthink_advance_brand`.brand_id = `outthink_advance_brand_category_item`.brand_id )';
$query .= ' where `outthink_advance_brand_category_item`.category_id = '.$cat_id;

$brands = $readConnection->fetchAll($query);

$bArr = array();

foreach ($brands as $brand){
$bArr[$brand["brand_id"]] = $brand["originalcategory_id"];
}
$brandsArr[$cat_id] = $bArr;
}
$brandSelect .= '</select>';

return $brandSelect.'<select id="model_select"><option value="">My Model</option></select>';

    }
    public function getbrandsArr()
    {
        $resource = Mage::getSingleton('core/resource');

$readConnection = $resource->getConnection('core_read');

$writeConnection = $resource->getConnection('core_write');

$readConnection = $resource->getConnection('core_read');

$query = 'SELECT category_id, category_name FROM ' . $resource->getTableName('outthink_advance_brand_category');

$categories = $readConnection->fetchAll($query);

$brandsArr = array();

$brandSelect = '<select id="brand_select">';
$brandSelect .= '<option value="">My Brand</option>';
foreach ($categories as $category){
$cat_id = $category["category_id"];

$brandSelect .= "<option value='".$category["category_id"]."'>".$category["category_name"]."</option>";

$query = 'SELECT `outthink_advance_brand`.brand_id, `outthink_advance_brand`.originalcategory_id FROM `outthink_advance_brand` ';
$query .= ' inner join `outthink_advance_brand_category_item` on
(`outthink_advance_brand`.brand_id = `outthink_advance_brand_category_item`.brand_id )';
$query .= ' where `outthink_advance_brand_category_item`.category_id = '.$cat_id;

$brands = $readConnection->fetchAll($query);

$bArr = array();

foreach ($brands as $brand){
$bArr[$brand["brand_id"]] = $brand["originalcategory_id"];
}
$brandsArr[$cat_id] = $bArr;
}
     return $brandsArr;
    }
}

enter image description here

Était-ce utile?

La solution

Did you try using the second column of your MySQL request?

Try this:

foreach ($categories as $category){
    $cat_id = $category["category_id"];
    $cat_name = $category["category_name"]; //HERE IS WHAT YOU WANT
}

Let me know..

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top