1

I Have To Create One Custom Grid. in That Grid Image Have Field . But Image Not Displayed There.

C:\xampp2\htdocs\testmagento\app\code\local\Rushikesh\Module\Block\Adminhtml\Module\Grid.php Code.

$this->addColumn('image', array(
            'header' => Mage::helper('module')->__('Image'),
            'index' => 'image',
            'renderer' => 'Rushikesh_Module_Block_Adminhtml_Renderer_Image',
        ));

C:\xampp2\htdocs\testmagento\app\code\local\Rushikesh\Module\Block\Adminhtml\Renderer\Image.php Code

Class Rushikesh_Module_Block_Adminhtml_Renderer_Image extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
        {
            $value = $row->getData($this->getColumn()->getIndex());
            return '<img src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$value.'"  width="80" />';
        }
}

ModuleController.php

public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            if(isset($_FILES['fileinputname']['name']) and (file_exists($_FILES['fileinputname']['tmp_name']))) {
              try {
                $uploader = new Varien_File_Uploader('fileinputname');
                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything


                $uploader->setAllowRenameFiles(false);

                // setAllowRenameFiles(true) -> move your file in a folder the magento way
                // setAllowRenameFiles(true) -> move your file directly in the $path folder
                $uploader->setFilesDispersion(false);

                $path = Mage::getBaseDir('media') . DS ;

                $uploader->save($path, $_FILES['fileinputname']['name']);

                $data['fileinputname'] = $_FILES['fileinputname']['name'];
              }catch(Exception $e) {

              }
            }else {      

                if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1)
                    $data['image_main'] = '';
                else
                    unset($data['fileinputname']);
            }  
            try {
                $postData = $this->getRequest()->getPost();
                if(isset($postData['links'])){
                    $products = Mage::helper('adminhtml/js')->decodeGridSerializedInput($data['links']['products']); //Save the array to your database
                }
                //var_dump($postData['links']);
                //exit;
                $moduleModel = Mage::getModel('module/module');

                $moduleModel->setId($this->getRequest()->getParam('id'))
                    ->setTitle($postData['title'])              
                    ->setCId($postData['c_id'])
                    ->setTitle2($postData['title2'])
                    ->setContent($postData['content'])                  
                    ->setStatus($postData['status'])                    
                    ->setCreatedTime(time()) //Created Date-Time
                    ->save();

                /* Short Method For Saving Data In To Database */

                        /* $makemodelModel = Mage::getModel("module/module")    
                                ->addData($postData)
                                ->setId($this->getRequest()->getParam("id"))
                                ->save(); */

                /* Short Method For Saving Data In To Database */

                var_dump($products);
                //var_dump($postData);exit;
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setModuleData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setModuleData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }

        }
        $this->_redirect('*/*/');
    }

When Show Inspect Element in browser.

enter image description here

1

You are not saving the image file name and for this you have to change some code like :

$moduleModel->setId($this->getRequest()->getParam('id'))
       ->setTitle($postData['title'])              
       ->setCId($postData['c_id'])
       ->setTitle2($postData['title2'])
       ->setContent($postData['content'])                  
       ->setStatus($postData['status'])                    
       ->setCreatedTime(time()) //Created Date-Time
       ->setImage($data['fileinputname']) // Add this line according to your image field name
       ->save();
  • in database image value insert is Zero – Rushikesh Solanki Mar 23 '17 at 12:07
  • You have to check the image is getting uploaded or not. Also check the image field type in database, it bust be varchar. – Ashish Jagnani Mar 23 '17 at 12:20
  • Yes u r right, its workes now – Rushikesh Solanki Mar 23 '17 at 12:34
0

I think there is some problem in your save action. Try below code.

<?php

public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {

            if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
                try {   
                    /* Starting upload */   
                    $uploader = new Varien_File_Uploader('filename');

                    // Any extention would work
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);

                    // Set the file upload mode 
                    // false -> get the file directly in the specified folder
                    // true -> get the file in the product like folders 
                    //  (file.jpg will go in something like /media/f/i/file.jpg)
                    $uploader->setFilesDispersion(false);

                    // We set media as the upload dir
                    $path = Mage::getBaseDir('media') . DS ;
                    $uploader->save($path, $_FILES['filename']['name'] );

                } catch (Exception $e) {

                }

                //this way the name is saved in DB
                $data['filename'] = $_FILES['filename']['name'];
            }


            $model = Mage::getModel('module/module');       
            $model->setData($data)
                ->setId($this->getRequest()->getParam('id'));

            try {
                if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                    $model->setCreatedTime(now())
                        ->setUpdateTime(now());
                } else {
                    $model->setUpdateTime(now());
                }   

                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('module')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('module')->__('Unable to find item to save'));
        $this->_redirect('*/*/');
    }

?>

Then write below code in your image.php file

<?php

Class Rushikesh_Module_Block_Adminhtml_Renderer_Image extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $img = $row->getData($this->getColumn()->getIndex());
        if($img && strpos($img, 'no_selection') !== 0) {
            return sprintf('<img src="%s" width="60px" />', Mage::getBaseUrl('media').$img);
        }

        return '';
    }
}

?>
  • in my save action all data should stored in database but only image name should not stored in database – Rushikesh Solanki Mar 23 '17 at 11:40
  • $moduleModel = Mage::getModel('module/module'); $moduleModel->setId($this->getRequest()->getParam('id')) ->setTitle($postData['title']) ->setFileinputname($_FILES['fileinputname']['name']) ->setCId($postData['c_id']) ->setTitle2($postData['title2']) ->setContent($postData['content']) ->setStatus($postData['status']) ->setCreatedTime(time()) //Created Date-Time ->save(); – Rushikesh Solanki Mar 23 '17 at 11:40
  • here is my code that strore all data into database – Rushikesh Solanki Mar 23 '17 at 11:40
  • ->setFileinputname($_FILES['fileinputname']['name']) I use that for store image – Rushikesh Solanki Mar 23 '17 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.