Module storing and edit & update values to database

Path of phtml  D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\template\testmodule\testmodule.phtml

code:

<form action="<?php echo $this->getUrl().'testmodule/index/loginu'; ?>" method="post">
                    <div class="col-sm-12">
                        <div class="row">
                            <div class="col-sm-6 form-group">
                                <label>testmodule_id</label>
                                <input type="id" placeholder="id" name="id" >
                            </div><br>
                            <div class="col-sm-6 form-group">
                                <label>title</label>
                                <input type="text" placeholder="title" name="title"required >
                            </div>
                        </div><br>                 
                        <div class="form-group">
                            <label>filename</label>
                            <input type="text" placeholder="filename" name="filename"required >
                           
                        </div><br> 
                        <div class="form-group">
                            <label>name</label>
                            <input type="text" placeholder="name" name="name"required >
                           
                        </div><br>
                        <div class="col-sm-6 form-group">
                                <label>content</label>
                                <input type="text" placeholder="content" name="content"required >
                            </div><br>
                            <div class="col-sm-6 form-group">
                                <label>status</label>
                                <input type="text" placeholder="status" name="status"required >
                            </div><br>
                           
                   <input type="Submit" title="Save" value="Save">
                   <input type="Reset" title="Reset" value="Reset">                 
                    </div>
                </form>

                <div>

                    <?php $id = $this->getRequest()->getPost('id');
// print_r($id);
$model = Mage::getModel('testmodule/testmodule')->getCollection();

print_r($configValue);
?>

<table >
  <thead>
    <tr>
     <th>ID</th>
      <th>Title</th>
      <th>Filename</th>
      <th>name</th>
      <th>Content</th>
      <th>Status</th>
     
    </tr>
  </thead>
  <tbody>

<?php

foreach ($model->getData() as $key => $value) {
  //echo "<pre>"; print_r($value); echo "</pre>"; ?>
    <tr>
     <td><?php echo $value['testmodule_id']; ?></td>
    <td><?php echo $value['title']; ?></td>
    <td><?php echo $value['filename']; ?></td>
    <td><?php echo $value['name']; ?></td>
    <td><?php echo $value['content']; ?></td>
    <td><?php echo $value['status']; ?></td>
     
    <td><a href="<?php echo $this->getUrl('testmodule/index/update').'?id='.$value['testmodule_id']; ?>">Update</a></td>
    <td><a href="<?php echo $this->getUrl('testmodule/index/delete').'?id='.$value['testmodule_id']; ?>">Delete</a></td>
    
    
  </tr>

  <?php
}

?>
</tbody>
</table>

<style type="text/css">
   table, td, th {   
    border: 1px solid #ddd;
    text-align: left;
}

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    padding: 15px;
}
}</style>
                </div>



Path of phtml  D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\template\testmodule\update.phtml

code:

<?php $id = $this->getRequest()->getParams();
print_r($id);

$model = Mage::getModel('testmodule/testmodule')->load($id['id']);
// echo "<pre>"; print_r($model->getData());  echo "</pre>";
?>

<form class="form-horizontal" method="post" action="<?php echo Mage::getUrl().'testmodule/index/updates'?>">

     <div class="col-sm-12">
                        <div class="row">
                            <div class="col-sm-6 form-group">
                                <label>testmodule_id</label>
                                <input type="text" name="testmodule_id" id="testmodule_id" value="<?php echo $model['testmodule_id']; ?>"class="form-control">
                            </div><br>
                            <div class="col-sm-6 form-group">
                                <label>title</label>
                                <input type="text" name="title" id="title" value="<?php echo $model['title']; ?>" class="form-control">
                            </div>
                        </div><br>                 
                        <div class="form-group">
                            <label>filename</label>
                            <input type="text" name="filename" id="filename"  value="<?php echo $model['filename']; ?>" class="form-control">
                           
                        </div><br> 
                        <div class="col-sm-6 form-group">
                                <label>content</label>
                                <input type="text" name="content" id="content" value="<?php echo $model['content']; ?>" class="form-control">
                            </div><br>
                            <div class="col-sm-6 form-group">
                                <label>status</label>
                                <input type="text" name="status" id="status" value="<?php echo $model['status']; ?>" class="form-control">
                            </div><br>
                           
                   <input type="Submit" title="Save" value="Save">
                   <input type="Reset" title="Reset" value="Reset">                 
                    </div><br>

 <?php $id = $this->getRequest()->getPost('id');
// print_r($id);
$model = Mage::getModel('testmodule/testmodule')->getCollection(); ?>

<table >
  <thead>
    <tr>
      <th>id</th>
      <th>Title</th>
      <th>Filename</th>
      <th>Content</th>
      <th>Status</th>
     
    </tr>
  </thead>
  <tbody>

<?php

foreach ($model->getData() as $key => $value) {
  //echo "<pre>"; print_r($value); echo "</pre>"; ?>

       <tr>
     <td><?php echo $value['testmodule_id']; ?></td>
    <td><?php echo $value['title']; ?></td>
    <td><?php echo $value['filename']; ?></td>
    <td><?php echo $value['content']; ?></td>
    <td><?php echo $value['status']; ?></td>
     
   </tr>

  <?php
}

?>
</tbody>
</table>


<style type="text/css">

   table, td, th {   
    border: 1px solid #ddd;
    text-align: left;
}

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    padding: 15px;
}
}</style>


Path of phtml  D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\template\testmodule\edit.phtml

code:

<?php $id = $this->getRequest()->getParams('id');
// print_r($id);
$model = Mage::getModel('testmodule/testmodule')->load($id);

?>

<form class="form-horizontal" method="post" action="<?php echo Mage::getUrl().'testmodule/index/edit'?>">

     <fieldset>
    <legend></legend>
    <div class="form-group">
      <label for="inputTitle" class="col-lg-2 control-label">Title</label>
      <div class="col-lg-10">
        <input class="form-control" id="inputTitle" name="title" placeholder="Title" type="text" value="<?php echo $model['title']; ?>">
        <input class="form-control"  name="id"  type="hidden" value="<?php echo $model['testmodule_id']; ?>">
      </div>
    </div>
    <div class="form-group">
      <label for="inputFileName" class="col-lg-2 control-label">FileName</label>
      <div class="col-lg-10">
        <input class="form-control" id="inputFileName" name="filename" placeholder="FileName" type="text" value="<?php echo $model['filename']; ?>">
       
      </div>
    </div>
    <div class="form-group">
      <label for="textArea" class="col-lg-2 control-label">Content</label>
      <div class="col-lg-10">
        <input class="form-control" name="content"  id="content" ><?php echo $model['content']; ?>
      </div>
    </div>
    <div class="form-group">
      <label for="textArea" class="col-lg-2 control-label">Status</label>
      <div class="col-lg-10">
         <input class="form-control" name="content"  id="content" ><?php echo $model['content']; ?>
       
      </div>
    </div>
   
    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="reset" class="btn btn-default">Reset</button>
        <button type="Update" class="btn btn-primary">Update</button>
      </div>
    </div>
  </fieldset>
</form>


<?php $id = $this->getRequest()->getPost('id');
// print_r($id);
$model = Mage::getModel('testmodule/testmodule')->getCollection(); ?>

<table >
  <thead>
    <tr>
      <th>Title</th>
      <th>Filename</th>
      <th>Content</th>
      <th>Status</th>
     
    </tr>
  </thead>
  <tbody>

<?php

foreach ($model->getData() as $key => $value) {
  //echo "<pre>"; print_r($value); echo "</pre>"; ?>

   
    <tr>
    <td><?php echo $value['title']; ?></td>
    <td><?php echo $value['filename']; ?></td>
    <td><?php echo $value['content']; ?></td>
    <td><?php echo $value['status']; ?></td>
     
    <td><a href="<?php echo $this->getUrl('testmodule/index/edit').'id/'.$value['testmodule_id']; ?>">Edit</a></td>
    <td><a href="<?php echo $this->getUrl('testmodule/index/delete').'id/'.$value['testmodule_id']; ?>">Delete</a></td>
    
   
  </tr>

  <?php
}

?>
</tbody>
</table>


<style type="text/css">

   table, td, th {   
    border: 1px solid #ddd;
    text-align: left;
}

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    padding: 15px;
}
}</style>

Path of phtml D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\layout\testmodule.xml

code:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
    </default>
    <testmodule_index_index>
        <reference name="content">
            <block type="testmodule/testmodule" name="testmodule" template="testmodule/testmodule.phtml" />
        </reference>
    </testmodule_index_index>
    <testmodule_index_update>
        <reference name="content">
            <block type="testmodule/testmodule" name="updatemodule" template="testmodule/update.phtml" />
        </reference>
    </testmodule_index_update>
    <catalog_product_view>
        <reference name="content">
            <block type= "core/template" name="catalogoverride" template="testmodule/catalog/product/view/new.phtml" before="product.info">
                <block type="core/template" name="catalogochild" template="testmodule/catalog/product/view/new1.phtml" before="product.info">
                     <block type="core/template" name="catalogochildchild" template="testmodule/catalog/product/view/new2.phtml" before="catalogochild"/>
                </block>
            </block>
        </reference>
    </catalog_product_view>
    </layout> 


Path of phtml D:\xampp\htdocs\magento1\app\code\local\Bluethink\Testmodule\controllers\IndexController.php

code:


<?php
class Bluethink_Testmodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
       
        $this->loadLayout();    
        $this->renderLayout();
    }
    public function editAction()
     {
         //Mage::getModel('hello/hello')->getEdit();
           $this->loadLayout();
       $this->renderLayout();
     }

    public function updateAction()
    {
        $this->loadLayout();    
        $this->renderLayout();
    }
    
    public function updatesAction(){
      //print_r($id['id']); exit;
       // echo"id===>".$id['id'];
        $id   = $this->getRequest()->getPost('testmodule_id');
        // echo "<pre>"; print_r($id); echo "</pre>";
        // exit();
        $title   = $this->getRequest()->getPost('title');   
        $filename = $this->getRequest()->getPost('filename');
        $name = $this->getRequest()->getPost('name');
        $content = $this->getRequest()->getPost('content');
        $status = $this->getRequest()->getPost('status');

       $model = Mage::getModel('testmodule/testmodule')->load($id);
       /*echo "<pre>++";
       print_r($model->getData());
       exit;*/

        $model->setTitle($title);
        $model->setFilename($filename);
        $model->setName($name);
        $model->setContent($content);
        $model->setStatus($status);
       // $model->setProduct($this->getRequest()->getPost('title'));
        $model->save();
        //echo "===>".$model->getTestmoduleId();
        //exit();
        Mage::getSingleton('core/session')->addSuccess('edited sucessfully');
        $this->_redirect('testmodule/index/update');

    }
    
    public function deleteAction()
    {
      $id = $this->getRequest()->getParam('id');
        $model = Mage::getModel('testmodule/testmodule')->load($id);
        $model->delete();
       // echo "deleted sucessfully";
         Mage::getSingleton('core/session')->addSuccess('deleted sucessfully');
       $this->_redirect('testmodule/index/index');
    }
    public function loginuAction()
  {
        $title   = $this->getRequest()->getPost('title');   
        $filename= $this->getRequest()->getPost('filename');
        $name= $this->getRequest()->getPost('name');
        $content = $this->getRequest()->getPost('content');
        $status = $this->getRequest()->getPost('status');
        
 //print_r($images); exit();
        /*echo "<pre>";
        print_r($this->getRequest()->getPost());
        echo "+++++".;
        exit;*/
        $model = Mage::getModel('testmodule/testmodule');//->load(2);

        /*echo"<pre>++++";
        print_r($model->getData());
        exit;*/
       
        $model->setTitle($title);
        $model->setFilename($filename);
        $model->setName($name);
        $model->setContent($content);
        $model->setStatus($status);
       //  $model->setProduct($this->getRequest()->getPost('title'));
        $model->save();
               
        $this->_redirect('testmodule/index/index');   // redirect the page
     }
     public function showAction()
     {
     $data = Mage::getModel('testmodule/testmodule')->getCollection();   // this is for fetch the data from the database
     foreach($data as $da)
     {
         echo $da->getTitle();
         echo "<br>";
         echo $da->getFilename();
         echo "<br>";
         echo $da->getName();
         echo "<br>";
         echo $da->getContent();
         echo "<br>";
         echo $da->getStatus();
         echo "<br>";
        
     }
     }

 }

You have to make Database with table {magento1/testmodule} as shown in
 Pic:




Comments

Popular posts from this blog

Through Postman Api send data through JSON and create simple product in Magento2

Getting product Image in PHTML in Magento2

programmaticlly Create Order in Magento2 with custom options in order