Codeigniter: Display multiple images from database for - codeigniter
I'm running a real estate website. I'm trying to list all images in the database for a particular listing on their respective page. I have the following code but it just repeats the same default image instead of showing all images for that listing.
Model:
public function getDetails($listing_id)
{
$this->db->select(
'listings.listing_id AS id,
listings.account_id AS account_id,
listings.nbrhood_id AS nbrhood_id,
listings.price AS price,
listings.convertible AS convertible,
listings.available AS available,
images.file_name AS file_name,
listings.sqft AS sqft,
listings.db_tag AS db_tag,
listings.web_id AS web_id,
listings.description AS description,
bedrooms.beds_num AS beds_val,
bedrooms.beds_name AS beds_text,
bathrooms.baths_num AS baths_val,
bathrooms.baths_name AS baths_text,
nbrhoods.nbrhood_name AS nbrhood,
nbrhoods.image AS nbrhood_image,
types_listings.type_name AS type,
pets.pet_name AS pets'
);
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
$this->db->join('bedrooms', 'bedrooms.beds_id = listings.beds_id');
$this->db->join('bathrooms', 'bathrooms.baths_id = listings.baths_id');
$this->db->join('nbrhoods', 'nbrhoods.nbrhood_id = listings.nbrhood_id');
$this->db->join('types_listings', 'types_listings.type_id = listings.type_id');
$this->db->join('pets', 'pets.pet_id = listings.pet_id');
$this->db->where('listings.listing_id', $listing_id);
$this->db->limit(1);
return $this->db->get($this->table_name)->row();
}
PHP:
<?php foreach ($listing AS $listing_id): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $listing -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
</div>
</li>
<?php endforeach; ?>
class Listing_Model extends CW_Model {
protected $table_name = 'listings';
protected $order_by = 'listing_id';
protected $primary_key = 'listing_id';
protected $display_val = '';
public $defaults = array(
'status_id' => 1
);
public $qualifiers = array('listing_id', 'account_id', 'status_id', 'image_id', 'file_name', 'featured', 'feature_id', 'deal_id', 'nbrhood_id', 'beds_id', 'baths_id', 'pet_id', 'type_id', 'owner_id', 'web_id', 'price', 'convertible', 'available', 'sqft', 'address', 'unit', 'zip', 'description', 'notes');
public $rules = array(
'new' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
)
),
'edit' => array(
'listing_id' => array(
'field' => 'listing_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'account_id' => array(
'field' => 'account_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'status_id' => array(
'field' => 'status_id',
'label' => '',
'rules' => 'trim|required|xss_clean'
),
'image_id' => array(
'field' => 'image_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'file_name' => array(
'field' => 'file_name',
'label' => '',
'rules' => 'trim|xss_clean'
),
'featured' => array(
'field' => 'featured',
'label' => '',
'rules' => 'trim|xss_clean'
),
'feature_id' => array(
'field' => 'feature_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'deal_id' => array(
'field' => 'deal_id',
'label' => '',
'rules' => 'trim|xss_clean'
),
'nbrhood_id' => array(
'field' => 'nbrhood_id',
'label' => 'Neighborhood',
'rules' => 'trim|required|xss_clean'
),
'price' => array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|min_length[3]|integer|xss_clean'
),
'beds_id' => array(
'field' => 'beds_id',
'label' => 'Bedrooms',
'rules' => 'trim|required|xss_clean'
),
'convertible' => array(
'field' => 'convertible',
'label' => 'Convertible',
'rules' => 'trim|is_natural|xss_clean'
),
'baths_id' => array(
'field' => 'baths_id',
'label' => 'Bathrooms',
'rules' => 'trim|required|xss_clean'
),
'dateAvailable' => array(
'field' => 'dateAvailable',
'label' => '',
'rules' => 'trim|xss_clean'
),
'available' => array(
'field' => 'available',
'label' => 'Date Available',
'rules' => 'trim|required|xss_clean'
),
'pet_id' => array(
'field' => 'pet_id',
'label' => 'Pets',
'rules' => 'trim|required|xss_clean'
),
'sqft' => array(
'field' => 'sqft',
'label' => 'Square Feet',
'rules' => 'trim|min_length[3]|integer|xss_clean'
),
'type_id' => array(
'field' => 'type_id',
'label' => 'Listing Type',
'rules' => 'trim|required|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|min_length[175]|xss_clean'
),
'amenity_ids[unit][]' => array(
'field' => 'amenity_ids[unit][]',
'label' => 'Unit Amenities',
'rules' => 'trim|xss_clean'
),
'amenity_ids[property][]' => array(
'field' => 'amenity_ids[property][]',
'label' => 'Property Amenities',
'rules' => 'trim|xss_clean'
),
'owner_id' => array(
'field' => 'owner_id',
'label' => 'Owner/Landlord',
'rules' => 'trim|required|xss_clean'
),
'address' => array(
'field' => 'address',
'label' => 'Street Address',
'rules' => 'trim|required|min_length[12]|xss_clean'
),
'unit' => array(
'field' => 'unit',
'label' => 'Unit Number',
'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
),
'zip' => array(
'field' => 'zip',
'label' => 'Zip Code',
'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
),
'notes' => array(
'field' => 'notes',
'label' => 'Notes',
'rules' => 'trim|xss_clean'
),
'image_ids[]' => array(
'field' => 'image_ids[]',
'label' => '',
'rules' => 'trim|xss_clean'
),
'web_id' => array(
'field' => 'web_id',
'label' => 'Web ID',
'rules' => 'trim|alpha_dash|xss_clean'
)
)
);
public function __construct() {
parent::__construct();
}
public function getNew() {
// Remember everytime you add a field you have to add it to form validation...
$listing = new stdClass();
$listing->listing_id = $this->_get_identifier();
$listing->account_id = $this->session->userdata('account_id');
$listing->status_id = 0;
$listing->feature_id = '';
$listing->image_id = 0;
$listing->nbrhood_id = ''; // Dropdown
$listing->beds_id = ''; // Dropdown
$listing->baths_id = ''; // Dropdown
$listing->pet_id = ''; // Dropdown
$listing->type_id = ''; // Dropdown
$listing->owner_id = ''; // Dropdown
$listing->web_id = 'CWA' . random_string('numeric', 7); // varchar
$listing->featured = ''; // bool
$listing->deal_id = '0'; // bool
$listing->price = ''; // int
$listing->convertible = ''; // bool
$listing->dateAvailable = ''; // Only to populate visible jQuery datapicker
$listing->available = ''; // date
$listing->sqft = ''; // int
$listing->address = ''; // varchar
$listing->unit = ''; // varchar
$listing->description = ''; // text
$listing->zip = ''; // int
$listing->notes = ''; // text
$listing->amenity_ids['unit'] = array(); // Dropdown
$listing->amenity_ids['property'] = array(); // Dropdown
$listing->image_ids = array();
return $listing;
}
I don't know about your data model. You should post it here. If your data model has been done correctly, then this is the line that would change:
$this->db->join('images', 'images.image_id = listings.image_id', 'left');
to
$this->db->join('images', 'images.listing_id = listings.id');
You should store the listing ID in the Images table rather than the other way round.
EDIT:
From data model, I mean database structure. If there are n images for every single 'listing', then you need to store a reference to the 'listing ID' in the 'images' table.
Took over this project from someone else. Where would I add fields to the image table. Right now in CMS it shows multiple images for a listing. The code is:
<?php foreach ($images as $image): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name"> </span> -->
<img src="<?=images_url('110x68/' . $image -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
<div class="btn-group">
<button class="btn <?php
if ($image -> image_id == $listing -> image_id)
echo 'btn-warning';
?> image-default"><span class="icon-star <?php
if ($image -> image_id == $listing -> image_id)
echo 'icon-white';
?>"></span></button>
<button class="btn image-remove"><span class="icon-remove"></span></button>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
Related
How to Make a Status Field Bydefault Enable in Magento Form?
Following is my Form Field Code. $fieldset->addField('status','select', array( 'label' => Mage::helper('synclogin')->__('Eshot Status'), 'name' => 'status', 'values' => array( array( 'value' => 0, 'label' => Mage::helper('synclogin')->__('Disabled'), ), array( 'value' => 1, 'label' => Mage::helper('synclogin')->__('Enabled'), ), ), ) ); How to Make a Status Field Bydefault Enable in Magento Form?
$fieldset->addField('status','select', array( 'label' => Mage::helper('synclogin')->__('Eshot Status'), 'name' => 'status', 'value' => '1', 'values' => array( array( 'value' => 0, 'label' => Mage::helper('synclogin')->__('Disabled'), ), array( 'value' => 1, 'label' => Mage::helper('synclogin')->__('Enabled'), ), ), ) );
Yii1: How to force filter list in CGridView to parse html ?
I have a grid, in this grid I have filter. Grid code is: $this->widget('booster.widgets.TbGridView', array( 'id' => 'sam', 'type' => 'striped bordered condensed', 'dataProvider' => $dataProvider, 'responsiveTable' => true, 'enableHistory' => true, 'filter' => $asset, 'columns' => array( array( 'name' => 'id', 'header' => '#', 'filter' => false, 'type' => 'text', ), array( 'name' => 'user', 'header' => 'Registered By', 'type' => 'text', 'value' => '$data["assignedBy"]', ), array( 'name' => 'createdAt', 'header' => 'Created At', 'type' => 'datetime', ), array( 'name' => 'serial', 'header' => 'Serial', ), array( 'name' => 'brand', 'header' => 'Brand', ), array( 'name' => 'model', 'header' => 'Model', ), array( 'name' => 'assetType', 'type' => 'text', 'filter' => \wsi\it\model\AssetType::getRepository()->getTypeTree(), 'value' => '$data["assetTypeName"]', 'header' => 'Type', ), array( 'name' => 'assigned', 'value' => '(isset($data["assignedTo"]))? $data["assignedTo"]:null', 'header' => 'Assigned To', ), array( 'name' => 'location', 'filter' => \wsi\hr\Facade::getInstance()->getLocations(), 'value' => '$data["locationName"]', 'header' => 'Location', ), array( 'name' => 'status', 'header' => 'Status', 'filter' => \wsi\it\model\Asset::$statusOptionList, 'value' => '\wsi\it\model\Asset::$statusOptionList[$data["status"]]', ), array( 'class' => 'booster.widgets.TbButtonColumn', 'template' => '{view} {update} {delete}', 'header' => '', 'buttons' => array( 'update' => array( 'url' => '\Yii::app()->controller->createUrl("asset/create", array("id"=>$data["id"]))', ), 'view' => array( 'url' => '\Yii::app()->controller->createUrl("asset/view", array("id"=>$data["id"]))', ), 'delete' => array( 'url' => '\Yii::app()->controller->createUrl("asset/delete", array("id"=>$data["id"]))', ), ), ), ) ) ); When Grid is rendered in the browser you can see that the filter list box contains html which is not parsed fully! I had this problem with Yii-Booster before and I solved it with an option which I passed to that widget ('htmlOptions' => 'encode' => false) and It prevents to be treated as string, so browsers would parse it as space. That code which You can see below does not work for grid filter! $form->dropDownListGroup($formModel, 'segmentList', array( 'wrapperHtmlOptions' => array( 'class' => 'col-md-6' ), 'widgetOptions' => array( 'data' => $segmentTreeArray, 'htmlOptions' => array( >>> 'encode' => false, <<< ) ), 'hint' => "Press CTRL to add another item, otherwise others will be deselected", )); BUT I am sure that I have to pass same "encode" => false to filter list too, I just can not find under what key I should pass it (htmlOptions did not work).
Zf2 entity create a custom filters
public function getInputFilter($em){ if (!$this->inputFilter) { $inputFilter = new InputFilter(); $inputFilter->add(array( 'name' => 'fullName', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), array('name' => 'SpecialChar') ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 100, ), ), ), )); $inputFilter->add(array( 'name' => 'password', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 100, ), ), ), )); $inputFilter->add(array( 'name' => 'email', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'EmailAddress', ), array( 'name' => 'User\Validator\NoEntityExists', 'options'=>array( 'entityManager' =>$em, 'class' => 'User\Entity\User', 'property' => 'email', 'exclude' => array( array('property' => 'id', 'value' => $this->getId()) ) ) ) ), )); $this->inputFilter = $inputFilter; } return $this->inputFilter; } I want to add a new function in entity called "Special Char" in all input fields, so how does one create a custom filter in a doctrine entity. I want to add validation to avoid special char in entity because I need to use this in n no of places. How do I implement this?
From Zend documentation: namespace Application\Filter; use Zend\Filter\FilterInterface; class MyFilter implements FilterInterface { public function filter($value) { // perform some transformation upon $value to arrive on $valueFiltered return $valueFiltered; } } Then you should be able to do: $inputFilter->add(array( 'name' => 'fullName', 'required' => true, 'filters' => array( array('name' => 'Application\Filter\MyFilter') …
ZF2 How to validate fields inside fieldset?
I have a field in the form like this: $this -> add(array( 'type' => 'field-set', 'name' => 'meta_properties', 'options' => array( 'label' => "Meta Properties", ), 'elements' => array( ) )); $meta_fieldSet = $this -> get('meta_properties'); $meta_fieldSet->add(array( 'name' => 'meta_title', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => 'Title', ), )); $meta_fieldSet->add(array( 'name' => 'meta_description', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => 'Description', ), )); $meta_fieldSet->add(array( 'name' => 'meta_keywords', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => 'Keywords', ) )); And in my input filter class I have $inputFilter -> add($factory -> createInput(array( 'name' => 'meta_title', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 150, ), ), ), ))); $inputFilter -> add($factory -> createInput(array( 'name' => 'meta_description', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 150, ), ), ), ))); $inputFilter -> add($factory -> createInput(array( 'name' => 'meta_keywords', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 150, ), ), ), ))); It is not validating. How to validate InputFilter?
When creating Fieldsets you need to extend \Zend\Form\Fieldset. Try creating a fieldset like this: <?php /** * Fieldset Example */ namespace Module\Form; use Zend\Form\Fieldset; use Zend\InputFilter\InputFilterProviderInterface; use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator; class NewFieldset extends Fieldset implements InputFilterProviderInterface { public function __construct() { // we want to ignore the name passed and give it our own parent::__construct('appointment'); // Use the model you are associating with your fieldset here $this->setHydrator(new ClassMethodsHydrator(false)) ->setObject(new MyModel) ->setLabel('My Model'); $this->add(array( 'name' => 'fieldset_item_1', 'type' => 'Select', 'options' => array( 'label' => 'FieldSet Item 1', ) )); $this->add(array( 'name' => 'fieldset_item_2', 'type' => 'Select', 'options' => array( 'label' => 'FieldSet Item 2', ) )); } /** * Sets up the input filter specification and returns it */ public function getInputFilterSpecification() { return array( 'fieldset_item_1' => array( 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 100, ), ), ), ), 'fieldset_item_2' => array( 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 100, ), ), ), ), ); } } Then you can use it in your form like this: $this->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'fieldset', 'options' => array( 'label' => '', 'count' => 1, 'should_create_template' => true, 'allow_add' => true, 'use_as_base_fieldset' => false, 'create_new_objects' => true, 'target_element' => array('type' => 'Module\Form\NewFieldset') ), )); Now when you perform $form->isValid() in a controller it will validate the fieldset. In addition, you can now make some fancy front-end code to dynamically add and remove fieldsets from the form and they will all get validated.
Magento Custom module - how to add custom select box with parent categories
I wrote a module to have my own menu bar, rather than just using categories as a menu bar. So, here I want to show already added menus while adding new menu in the Tab/Form.php in my custom menu module. How can I show all of the existing menu names as a dropdown/options list on the form. Here is the code that I used to have menu form. $form = new Varien_Data_Form(); $this->setForm($form); $fieldset = $form->addFieldset('menu_form', array('legend'=>Mage::helper('menu')->__('Menu information'))); $note = "Name of this Menu"; $fieldset->addField('title', 'text', array( 'label' => Mage::helper('menu')->__('Menu Name'), 'class' => 'required-entry', 'required' => true, 'note' => $note, 'name' => 'title', )); $note = "Menu level"; $fieldset->addField('level', 'select', array( 'label' => Mage::helper('menu')->__('Menu level'), 'name' => 'level', 'note' => $note, 'values' => array( array( 'value' => 1, 'label' => Mage::helper('menu')->__('Level 1'), ), array( 'value' => 2, 'label' => Mage::helper('menu')->__('Level 2'), ), ), )); $model = Mage::registry('menu'); $fieldset->addField('parent', 'text', array( 'name' => 'conditions', 'label' => Mage::helper('menu')->__('Parent Menu'), 'title' => Mage::helper('menu')->__('Parent Menu'), 'required' => false, 'note' => $note, ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions')); $fieldset->addField('target', 'select', array( 'label' => Mage::helper('menu')->__('Open in new window'), 'name' => 'target', 'values' => array( array( 'value' => "_blank", 'label' => Mage::helper('menu')->__('Yes'), ), array( 'value' => "_self", 'label' => Mage::helper('menu')->__('No'), ), ), )); $fieldset->addField('status', 'select', array( 'label' => Mage::helper('menu')->__('Status'), 'name' => 'status', 'values' => array( array( 'value' => 1, 'label' => Mage::helper('menu')->__('Enabled'), ), array( 'value' => 2, 'label' => Mage::helper('menu')->__('Disabled'), ), ), )); $note = "Menu Links to Which page. BaseURL(<b>".str_ireplace("index.php/","",Mage::getBaseUrl())."</b>) Will be Added Dynamically, Please add Your new page Refrence alone"; $fieldset->addField('menulink', 'text', array( 'label' => Mage::helper('menu')->__('URL'), 'required' => true, 'class' => 'required-entry', 'note' => $note, 'name' => 'menulink', )); $fieldset->addField('position', 'select', array( 'label' => Mage::helper('menu')->__('Position'), 'name' => 'position', 'values' => array( array( 'value' => 1, 'label' => Mage::helper('menu')->__('Top 1'), ), array( 'value' => 2, 'label' => Mage::helper('menu')->__('Top 2'), ), ), )); if ( Mage::getSingleton('adminhtml/session')->getMenuData() ) { $form->setValues(Mage::getSingleton('adminhtml/session')->getMenuData()); Mage::getSingleton('adminhtml/session')->setMenuData(null); } elseif ( Mage::registry('menu_data') ) { $form->setValues(Mage::registry('menu_data')->getData()); } return parent::_prepareForm(); In this I want to show all the added menus under the parent menu option. What should I write in my Model class so that I can have a drop down list to show them all, and after adding it should be added to database. Please help me, am struggling here.
You can do this by adding these lines. It will show already added menus as a dropdown list so that you can choose any of them as a parent menu for current item. $_menus = Mage::getSingleton('menus/menus')->getCollection(); foreach($_menus as $item) { if($item->getParent == NULL){ $_menuItems[] = array( 'value' => $item->getId(), 'label' => $item->getTitle(), ); } } $note = "Choose the parent menus for this item"; $fieldset->addField('parent', 'select', array( 'name' => 'parent', 'label' => Mage::helper('menus')->__('Parent Menu'), 'title' => Mage::helper('menus')->__('Parent Menu'), 'required' => false, 'note' => $note, 'class' => 'HideIt', 'values' => $_menuItems, ));