The Joomla! Forum ™

Forum rules

Please use the mailing list here: rather than this forum.

Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Feb 18, 2012 2:52 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Dec 16, 2011 8:50 pm
Posts: 5
Hi All,I need to understand this, hopefully someone can help.

I'm working on a component and need to add form data to a table. I have soe experience with php, but am new to Joomla!

I have a view (myart/view/add/tmpl/defaut.php) which has:
<form id="form1" name="form1" method="post" action="">

      <td><label>Art Title: </label></td>
      <td><input type="text" name="title" id="title" /></td>
      <td><input type="text" name="sku" id="sku" /></td>
      <td><input type="text" name="price" id="price" /></td>

I have a model(myart/model/add.php) which has:
defined('_JEXEC') or die;
      // get email variables
      $title             = $_POST['title'];
      $price              = $_POST['price'];   
      $sku            = $_POST['sku'];
class myartModelAdd extends JModelList
   public function getListQuery()
      $user =& JFactory::getUser();
      $user_id = $user->id;
      $db =& JFactory::getDBO();

      $query = "INSERT INTO #__myart (user_id, title, price, sku,)
         VALUES ($user_id, $title, $price, $sku,)";
      $db->setQuery( $query );
      $new_image = $db->loadResult();
      return $new_image;

Can someone shoe me how to code this correctly, I’m kinda lost. Also what should I put as an action for the form?


PostPosted: Tue Feb 21, 2012 1:18 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Aug 19, 2009 9:47 pm
Posts: 32
Location: Bogotá, Colombia

o.k., you're doing it the hard way (you won't benefit from abstraction if you hard code...). Joomla has a class called JTable and it is meant to be used for things like this. Here's a quick overview of what you should do:

1) create a JTable extension class having attributes matching those from your table.
CREATE TABLE IF NOT EXISTS `#__smart_pictures`
`id` int(11) NOT NULL auto_increment,
`path` varchar(255),
`path_thumbnail` varchar(255),
`name` varchar(255),
`description` varchar(255),
`keywords` varchar(255),
`related_products` varchar(255),

and JTable, located at admin/tables/pictures.php:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
 *  Table class
class SmartTablePictures extends JTable
   /*primary key*/
   var $id=null;
   var $path=null;
   var $path_thumbnail=null;
   var $name=null;
   var $description=null;
   var $keywords=null;
   var $related_products=null;
    * Constructor
    * @param object Database connector object
   function __construct(&$db)
      parent::__construct('#__smart_pictures', 'id', $db);

2) it's "better" if you place methods in controllers, so I would suggest you move the method to site/controller.php
public function save_picture(){
      //get id number
      $id = JRequest::getVar('id', 0);
      //variable for error counting
      //create a database object instance
      $type ='Pictures';
      $prefix = 'SmartTable';
      $config = array();
      $row = JTable::getInstance($type, $prefix, $config);
      if($id==null || $id='') {
         //create picture
         //edit picture
      //associate the database object to the form
      if (!$row->bind(JRequest::get('post'))) {
         if (!$row->store()) {
            JError::raiseWarning(100, $message1);
         else {
            $backlink = JRoute::_('index.php?option=' . $option . '&view=pictures' );


This is just a method in your class extension, not the whole file.
3) in your form assign the method's name to the task atribute, i.e., you should have something like this:
<input type="hidden" name="id" value="<?php echo $this->item->id; ?>" />
   <input type="hidden" name="task" value="save_picture" />
   <input type="hidden" name="option" value="<?php echo (JRequest::getCmd('option')) ?>" />
   <input type="submit" class="button" id="button" value="Aceptar"/>

Joomla tries to match the values in your table to those in the form, so make sure you use the same names. Refer to the MVC tutorials in

It might seem as overkill, but it's not that hard once you get used it.


PostPosted: Thu Feb 23, 2012 10:05 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Dec 16, 2011 8:50 pm
Posts: 5
thanks so much for your reply. I will work on this...

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group