1

This question already has an answer here:

I want to Know what is the process of ADD Update Delete in Magento from front side. so can any one help with code.

marked as duplicate by Siarhey Uchukhlebau, Murtuza Zabuawala, Ashish Jagnani, Raphael at Digital Pianism, Rakesh Jesadiya Apr 14 '17 at 13:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Your description of problem is a little bit... modest. Can you extend your question? – Konrad Siamro Apr 13 '17 at 8:21
  • No this is not duplicate it is front side crud operation and that was backside means admin side crud operation @DouglasRadburn – Jaimin Apr 14 '17 at 7:37
4

After a Long Research i found this full module.

Here Namespace is Crud and Module name is Pro

First you want to Create Module File.

This file is under etc folder Path :- app\etc\modules\Crud_Pro.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Crud_Pro>
            <active>true</active>
            <codePool>local</codePool>
        </Crud_Pro>
    </modules>
</config> 

Step 2.:- Create config.xml file in local. Path :- app\code\local\Crud\Pro\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Crud_Pro>
            <version>0.4.0</version>
        </Crud_Pro>
    </modules>  
    <admin>
        <routers>
            <pro>
                <use>admin</use>
                <args>
                    <module>Crud_Pro</module>
                    <frontName>pro</frontName>
                </args>
            </pro>
        </routers>
    </admin>
    <adminhtml>
        <menu>
            <pro module="pro">
                <title>pro</title>
                <sort_order>80</sort_order>               
                <children>
                    <items module="pro">
                        <title>childs</title>
                        <sort_order>0</sort_order>
                        <action>pro/adminhtml_pro</action>
                    </items>
                </children>
            </pro>
        </menu> 
        <layout>
            <updates>
                <pro>
                    <file>pro.xml</file>
                </pro>
            </updates>
        </layout>       
    </adminhtml> 

    <frontend>
        <routers>
            <pro>
                <use>standard</use>
                <args>
                    <module>Crud_Pro</module>
                    <frontName>pro</frontName>
                </args>
            </pro>
        </routers>   
        <layout>
            <updates>
                <pro>
                    <file>pro.xml</file>
                </pro>
            </updates>
        </layout>
    </frontend>    
    <global>    
        <blocks>
            <pro>
                <class>Crud_Pro_Block</class>
            </pro>
        </blocks>       

        <models>
            <pro>
                <class>Crud_Pro_Model</class>
                <resourceModel>pro_resource</resourceModel>
            </pro>
            <pro_resource>
                <class>Crud_Pro_Model_Resource</class>
                <entities>
                    <pro>
                        <table>pro</table>
                    </pro>  

                </entities>
            </pro_resource>
        </models>       
        <resources>
            <pro_setup>
                <setup>
                    <module>Crud_Pro</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </pro_setup>
            <pro_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </pro_write>
            <pro_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </pro_read>
        </resources>    
        <helpers>
            <pro>
                <class>Crud_Pro_Helper</class>
            </pro>
        </helpers>      
    </global>
</config>

Step 3 :- Create Controller file Path:-app\code\local\Crud\Pro\controllers\IndexController.php

<?php
class Crud_Pro_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {       
        $this->loadLayout();     
        $this->renderLayout(); 
    }
    public function saveAction()
    {
        $data = $this->getRequest()->getPost();
        //print_r($_FILES['filename']['name']);  // name che etle image nu name levu che jo image ni type levi hoy to name ni jagya e type lakhvanu
        //exit;


        $filename = date("dmYhisa").".jpg";
        $contact = Mage::getModel('pro/pro');

        if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '')
        {
            $path = Mage::getBaseDir('media') . DS. 'test' ;

            move_uploaded_file($_FILES["filename"]["tmp_name"],$path."/". $filename);

            $contact->setData('filename',$filename);
        }

        $name = $this->getRequest()->getPost('name');  // post thi database ma thi data lese (name) e form nu use karelu che and getdata ma database ma name nakhse
        $email = $this->getRequest()->getPost('email');
        $rollno = $this->getRequest()->getPost('rollno');
        $gender = $this->getRequest()->getPost('gender');
        $status = $this->getRequest()->getPost('status');




        $contact->setData('stdname',$name);
        $contact->setData('email',$email);
        $contact->setData('rollno',$rollno);
        $contact->setData('gender',$gender);
        $contact->setData('status',$status);




        $contact->save();       
        $this->_redirect('pro/index/index');
    }

    public function deletedataAction()
    {

            $id     = $this->getRequest()->getParam('id');

            $model2  = Mage::getModel('pro/pro')->load($id);

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

    public function editDataAction()
    {

        $this->loadLayout();        
        $this->renderLayout();

    }   

    public function editAction()
    {

        $id     = $this->getRequest()->getParam('id');      
        $model  = Mage::getModel('pro/pro')->load($id);
        $data = $this->getRequest()->getPost();  // form no data laya and ema apdo data set karava exit niche badhu lakhyu che



        $filename = date("dmYhisa").".jpg";
        $contact = Mage::getModel('pro/pro');

        if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '')
        {
            $path = Mage::getBaseDir('media') . DS. 'test' ;

            move_uploaded_file($_FILES["filename"]["tmp_name"],$path."/". $filename);

            $model->setData('filename',$filename);
        }



        $model->setData('stdname',$data['name']);
        $model->setData('email',$data['Email']);
        $model->setData('rollno',$data['Roll']);
        $model->setData('gender',$data['gender']);
        $model->setData('status',$data['status']);
        $model->save();
        $this->_redirect('*/*/');   
    }   
}

Step 4:- Now Come To Design Part. Path:-app\design\frontend\rwd\default\layout\pro.xml

<?xml version="1.0"?>

<layout version="0.1.0">
    <pro_index_index>
        <reference name="content">
            <block type="pro/pro" name="template_file" template="pro/pro.phtml"></block>
        </reference>
    </pro_index_index>
    <pro_index_editdata>
        <reference name="content">
            <block type="pro/pro" name="template_file" template="pro/edit.phtml"></block>
        </reference>
    </pro_index_editdata>
</layout>

Step 5. Create Template File Assign by layout in which there are two types of form 1.save data form 2.edit data form

app\design\frontend\rwd\default\template\pro\pro.phtml

<form method="POST" action="<?php echo $this->getUrl('pro/index/save'); ?>" enctype="multipart/form-data">
  <fieldset>
    <legend>Student intion:</legend>
    Name:<br>
    <input type="text" name="name" minlength="3" placeholder="Enter Name" required>
    <br>
    Email:<br>
    <input type="email" name="email" placeholder="Enter Email" required>
    <br>
    Roll No.:<br>
    <input type="number" name="rollno" min="1" placeholder="Enter Roll No." required>
    <br><br>

    <br>
    gender:<br>
          <input type="radio" name="gender" value="male" checked> Male<br>
            <input type="radio" name="gender" value="female"> Female<br>
    <br>

        <label for="filename"><?php echo $this->__('File1') ?></label><br />
        <input type="file" id="filename" name="filename" /><br/>
    <br/>
 status:<br>
 <select name="status">
 <option value="0">Enable</option>
 <option value="1">Disable</option>
 </select>

    <br>    

    <input type="submit" value="Submit">
  </fieldset>
</form>




<?php

$collection = Mage::getModel('pro/pro')->getCollection()->setOrder('rollno', 'DESC');



?>
<div id="table_id">
      <table width="80%" border="1px" style="text-align:center">
<?php
foreach($collection as $collect)
{
    //if($collect->getStatus() == 1)
    {
    ?>

    <tr>
        <td>
                <?php echo $collect->getstd_id(); ?>
        </td>   
        <td>
                <?php echo $collect->getStdname(); ?>
        </td>
        <td>
                <?php echo $collect->getEmail(); ?>
        </td>
        <td>
                <?php echo $collect->getRollno(); ?>
        </td>
                <td>
                <?php echo $collect->getCountry(); ?>
        </td>
                <td>
                <?php echo $collect->getGender(); ?>
        </td>
        <td>
                <?php $v  = Mage::getBaseUrl("media")."test/".$collect->getFilename();?>
<?php// echo $v; ?>             
                <img src='<?php echo $v; ?>' height=50px width= 50px/>
        </td>
        <td>
                <a href="<?php echo $this->getBaseUrl().'pro/index/deletedata?id='.$collect->getstd_id() ?>">
                <?php echo $this->__('DELETE'); ?></a>
        </td>
        <td>
                <a href="<?php echo $this->getBaseUrl().'pro/index/editData?id='.$collect->getstd_id() ?>">
                <?php echo $this->__('EDIT'); ?></a>
        </td>


    </tr>


<?php 
    }
}

?>
</table>
</div>

Create Edit form file app\design\frontend\rwd\default\template\pro\edit.phtml

<?php


    $models = Mage::getModel('pro/pro')->getCollection();
    $edit = Mage::getModel('pro/pro')->load($_GET['id']);
    echo "<pre/>";
    print_r($edit->getData());

?>



    <form  method="post" name="item_form"  id="item_form" action="<?php echo $this->getBaseUrl().'pro/index/edit' ?>" enctype="multipart/form-data" >           
        <input type="hidden" name="id" value="<?php echo $edit->getstd_id(); ?>" />

        <label for="title"><?php echo $this->__('Stdname:- ') ?><span class="required">*</span></label>
        <input type="text" id="name" name="name" value="<?php echo $edit->getStdname(); ?>" class="input-text required-entry" />

        <label for="title"><?php echo $this->__('Email Id:- ') ?><span class="required">*</span></label>
        <input type="text" id="Email" name="Email" value="<?php echo $edit->getEmail(); ?>" class="input-text required-entry" />

        <label for="title"><?php echo $this->__('Rollno:- ') ?><span class="required">*</span></label>
        <input type="text" id="Roll" name="Roll" value="<?php echo $edit->getRollno(); ?>" class="input-text required-entry" />


        gender:<br>

        <input type="radio" name="gender" value="male" <?php echo ("male" == $edit->getGender()) ? 'checked' : ''; ?>> Male<br>
        <input type="radio" name="gender" value="female" <?php echo ("male" != $edit->getGender()) ? 'checked' : ''; ?>> Female<br>
        <br>


        <label for="title"><?php echo $this->__('filename:- ') ?><span class="required">*</span></label>
        <input type="text" id="filename" name="filename" value="<?php echo $edit->getRollno(); ?>" class="input-text required-entry" />



        status:<br>
        <select name="status">              
        <option value='1' <?php echo (1 == $edit->getStatus()) ? 'selected' : ''; ?>>Disable</option>
        <option value='0' <?php echo (0 == $edit->getStatus()) ? 'selected' : ''; ?>>Enable</option>
        </select>

        <?php

        echo '<img id=output src='.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'test/'.$edit->getFilename().' width="50px" height"50px"/>'

        ?>

<input type="file" accept="image/*" onchange="loadFile(event)" name="filename">

<script>
  var loadFile = function(event) {
    var output = document.getElementById('output');
    output.src = URL.createObjectURL(event.target.files[0]);
  };
</script>

        <button type="submit" title="Update" name="submit" >Update</button>

    </form>

Create Block file for front end app\code\local\Crud\Pro\Block\Pro.php

class Crud_Pro_Block_Pro extends Mage_Core_Block_Template
{

}

Create Helper file app\code\local\Crud\Pro\Helper\Data.php

<?php

class Crud_Pro_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Now we create database so we have to create sql files here app\code\local\Crud\Pro\sql\pro_setup\mysql4-install-0.3.0.php

    <?php

    $installer = $this;

    $installer->startSetup();

    $installer->run("

    -- DROP TABLE IF EXISTS {$this->getTable('pro')};
    CREATE TABLE {$this->getTable('pro')} (
      `std_id` int(11) unsigned NOT NULL auto_increment,
      `stdname` varchar(255) NOT NULL default '',
      `email` varchar(255) NOT NULL default '',
      `rollno` int(11) NOT NULL default '0',
      `gender` varchar(255) NOT NULL default '',
      `filename` varchar(255) NOT NULL default '',
      PRIMARY KEY (`std_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

        ");



    $installer->endSetup();

Next Update Database with version which connected to config file also.

app\code\local\Crud\Pro\sql\pro_setup\mysql4-upgrade-0.3.0-0.4.0.php

<?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn(
    $this->getTable('pro'),//table name
    'status',      //column name
    'varchar(255) NOT NULL'  //datatype definition
    );

$installer->endSetup();

Now We Create Model Path:-app\code\local\Crud\Pro\Model\Pro.php

<?php

    class Crud_Pro_Model_Pro extends Mage_Core_Model_Abstract
    {

        protected function _construct()
        {
            $this->_init('pro/pro');
        }
    }

2.Path:- app\code\local\Crud\Pro\Model\Resource\Pro.php

<?php
class Crud_Pro_Model_Resource_Pro extends Mage_Core_Model_Resource_Db_Abstract
{   
    protected function _construct()
    {      
        $this->_init('pro/pro','std_id');
    }

}

Now to Fetch collection of our database create collection file

app\code\local\Crud\Pro\Model\Resource\Pro\Collection.php

<?php
class Crud_Pro_Model_Resource_Pro_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{   
    protected function _construct()
    {      
        $this->_init('pro/pro');
    }

}
  • +1 , I will surely try this one as i am new in Magento 2 – inrsaurabh Jan 22 '18 at 7:01
  • @inrsaurabh this is for Magento 1. ;) – Ricardo Martins Aug 28 '18 at 1:48

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