0

I am trying to add Image column in the existing module. but images are not showing up. here is the screenshot. Here is the screenshot. .

8 and 9 are having the image because I added it in MySQL.

here is my code. Grid.php

   $this->addColumn('file', array(
    'header'    => Mage::helper('tv_faq')->__('Image'),
    'align'     =>'left',
    'index'     => 'file',
    'renderer' => 'tv_faq/adminhtml_renderer_image',
    'width'     => '150px'
    ));

form.php

    $fieldset->addField('file', 'image', array(
        'label'     => Mage::helper('tv_faq')->__('Image'),
        'class'     => '',
        'required'  => false,
        'name'      => 'file',
        ));

TV/Faq/Block/Adminhtml/Renderer/Image.php

    public function render(Varien_Object $row){
    if($row->getFile()!=''){
        $image = Mage::getBaseUrl('media').$row->getFile();
        return sprintf('<img src="%s" width="150px" alt="%s"/>',$image, $this->escapeHtml($row->getTitle()));
    }
}

FaqController.php

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

        // init model and set data
        $model = Mage::getModel('tv_faq/faq')
        ->setCreationTime(Mage::getSingleton('core/date')->gmtDate())
        ->setImage($data['file'])
        ->setData($data)
        ->save();


        // try to save it
        try {
            // save the data
            $model->save();

            // display success message
            Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('cms')->__('FAQ Item was successfully saved')
            );
            // clear previously saved data from session
            Mage::getSingleton('adminhtml/session')->setFormData(false);
            // check if 'Save and Continue'
            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array (
                        'faq_id' => $model->getId() ));
                return;
            }
            // go to grid
            $this->_redirect('*/*/');
            return;

        }
        catch (Exception $e) {
            // display error message
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            // save data in session
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            // redirect to edit form
            $this->_redirect('*/*/edit', array (
                    'faq_id' => $this->getRequest()->getParam('faq_id') ));
            return;
        }
    }
    $this->_redirect('*/*/');
}

src of image enter image description here

  • What is the src for your image currently showing? – Sukumar Gorai Aug 21 '18 at 13:42
  • @SukumarGorai <td class="a-left "> &nbsp; </td> – Bhakti Thakkar Aug 22 '18 at 4:19
  • can you check image src code get or not? – Rv Singh Aug 22 '18 at 4:42
  • @sania image src of 8th question is <img src="127.0.0.1/mag/media/blog_1.jpg" width="150px" alt=""> – Bhakti Thakkar Aug 22 '18 at 4:46
  • @sania I added this image blog_1.jpg using MySQL. how can I add the image in Magento backend? – Bhakti Thakkar Aug 22 '18 at 4:49
0

Please check below code, I think you might help this.

Grid.php

public function callback_image($value)
{
    return "<img src='".$value."' width='150' height='100'/>";
}
protected function _prepareColumns()
{
    $this->addColumn("customsliderreport_id", array(
        "header" => Mage::helper("customsliderreport")->__("ID"),
        "align" =>"right",
        "width" => "50px",
        "type" => "number",
        "index" => "customsliderreport_id",
    ));
    $this->addColumn("image", array(
        "header" => Mage::helper("customsliderreport")->__("Image"),
        "index" => "image",
        'filter' => false,
        'frame_callback' => array($this, 'callback_image')
    ));
....
}

Form.php

$fieldset->addField("image", "text", array(
    "label" => Mage::helper("customsliderreport")->__("Image"),
    "name" => "image",
));
0

Use below code in TV/Faq/Block/Adminhtml/Renderer/Image.php

<?php
class TV_Faq_Block_Adminhtml_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row){

            $html='<img ';
            $html.= 'id="'.$this->getColumn()->getId().'"';
            $html.= 'src="'.Mage::getBaseUrl('media') . DS.$row->getData($this->getColumn()->getIndex()).'"';
            $html.= 'width="100" height="100"';
            $html.= 'class="grid-image '.$this->getColumn()->getInlineCss(). '"/>';
            return $html;
    }
}
?>

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.