Friday, July 2, 2010

Add new record to table Joomla component part 3

Lesson part 2, used to Describe only displaying information from table. But do you know how to add recodes to table? Answer is Nop.

Ok let's see how to insert data to table. Can u see there is button called "New"? Admin panel will popup html form once you click "New" button. What are the place and which are the file we used in here?

1.First Change
open - com_lanka1->views->lanka1s->tmpl->default.php

can u see there is code like,


<input type="hidden" name="controller" value="lanka1"/>



In above code, ple change "lanka" instead of" lanka1" where value of controller

2.Add new controller
add new folder "controllers" to your component folder[com_lanka1]
make new file "lanka.php"
com_lanka1->controllers->lanka.php





<?php
/**
* Lanka1 default controller
*
* @package Joomla.component
* @subpackage Components
* @link http://inetlanka.com
* @license GNU/GPL
* @auth inetlanka web team - [ info@inetlanka.com / wapnishantha@gmail.com ]
*/


// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
* Lanka1 Controller
*
* @package Joomla.component
* @subpackage Components
*/
class Lanka1sControllerLanka extends Lanka1sController
{
/**
* constructor (registers additional tasks to methods)
* @return void
*/
function __construct()
{
parent::__construct();

// Register Extra tasks
$this->registerTask( 'add' , 'edit' );
}

/**
* display the edit form
* @return void
*/
function edit()
{

JRequest::setVar( 'view', 'Lanka1' );
JRequest::setVar( 'layout', 'form' );
JRequest::setVar('hidemainmenu', 1);

parent::display();
}

/**
* save a record (and redirect to main page)
* @return void
*/
function save()
{
$model = $this->getModel('Lanka1');

if ($model->store($post)) {
$msg = JText::_( 'Lanka1 map Saved!' );
} else {
$msg = JText::_( 'Error Saving Lanka1 ' );
}

// Check the table in so it can be edited.... we are done with it anyway
$link = 'index.php?option=com_Lanka1';
$this->setRedirect($link, $msg);
}

/**
* remove record(s)
* @return void
*/
function remove()
{
$model = $this->getModel('Lanka1');
if(!$model->delete()) {
$msg = JText::_( 'Error: One or More Lanka1 map Could not be Deleted' );
} else {
$msg = JText::_( ' Lanka1 map(s) Deleted' );
}

$this->setRedirect( 'index.php?option=com_Lanka1', $msg );
}

/**
* cancel editing a record
* @return void
*/
function cancel()
{
$msg = JText::_( 'Operation Cancelled' );
$this->setRedirect( 'index.php?option=com_Lanka1', $msg );
}
}

Can u see wt are the code of function edit()
  1. JRequest::setVar( 'view', 'Lanka1' ); call Lanka1 folder under view folder
  2. JRequest::setVar( 'layout', 'form' ); call form.php
  3. JRequest::setVar('hidemainmenu', 1); hide main manu

Next lesson will explain about form.php

wAp

No comments:

Post a Comment