Jordan Jordan - 1 year ago 101
PHP Question

Magento - Display all categories in dropdown list

I am creating a simple script for updating all prices to all products within a specific category in Magento. I have the action code working and it updates the prices. Now I am making a form that has a dropdown with all the available categories in it then a text field that a percent is inputted. The problem I am having is I can not get the dropdown to populate the categories and I am going insane. I have tried other similar code and hacked to pieces other code to try to get it to work and it is driving me insane. Below is my current hacked up version - I know it is ugly and wrong but eh what can you do. Thanks for looking.

require 'app/Mage.php';
class Mymodule_Pup_Helper_Data extends Mage_Core_Helper_Abstract {
public function getCategoriesDropdown() {
$categoriesArray = Mage::getModel(‘catalog/category’)
->addAttributeToSort(‘path’, ‘asc’)
->addFieldToFilter(‘is_active’, array(‘eq’=>’1′))
foreach ($categoriesArray as $categoryId => $category) {
if (isset($category['name'])) {
$categories[] = array(
‘label’ => $category['name'],
‘level’ =>$category['level'],
‘value’ => $categoryId
return $categories;
Update Prices by Category
<form action="pupped.php" method="post">
<select id="category-changer" name="cat" style="width:150px;">
<option value="">--Select Category--</option>
$_CategoryHelper = Mage::helper("pup")->getCategoriesDropdown();
foreach($_CategoryHelper as $value){
foreach($value as $key => $val){
$catNameIs = $val;
$catIdIs = $val;
$catLevelIs = $val;
$b ='';
$b = $b."-";
<option value="<?php echo $catIdIs; ?>"><?php echo $b.$catNameIs ?></option>
Percent: <input type="text" name="per">
<input type="Submit">

Answer Source

below are the code get subcategory in drop down

<select id="category" class="myinput-text required-entry widthinput" name="category">
  $parentid=5; // parent id which you want sub category
  foreach($categories as $cat){ 
   <option value="<?php echo $category->getId();?>"><?php echo $category->getName();?></option>
<?php } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download