Joomla - Component: Creating the Database Table



Now, we create a table for preparing place where data will go. For first time, make sure what prefix database you use. You can check on menu Site > Global Configuration > Server. Assuming, that your database prefix is jos_.

Joomla: Look database prefix

If you prefer SQL statement, you can write following code:

CREATE TABLE `jos_hello` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`message` TEXT NOT NULL ,
`published` TINYINT( 1 ) NOT NULL 
);

If you prefer use web-based database manager such phpMyAdmin, folow this steps:

  1. Open your phpMyAdmin.
  2. Select you Joomla! database.
  3. Register new table on form like following images:
    Joomla: Register new table
    Click go.
  4. You will get a form for define columns. Do like following images:
    Joomla: define columns

    Don't forget define primary key and auto_increment for id.
    Joomla: define primary key and auto increment

    Click Save button.

Creating a Table Class

We could write individual functions to take care of the queries necessary to add, update, and delete data. Fortunately, the Joomla! team has ready done this for you. The JTable class provides functions for performing modify data from single table in the database. We just extend the class.

Following steps to create table class:

  1. Create a folder named "tables" within administrator/components/com_hello.
  2. Create the hello.php file and enter the following code:
    <?php
    defined('_JEXEC') or die('Restricted Access');
    
    class TableHello extends JTable
    {
    	var $id 		= null;
    	var $message 		= null;	
    	var $published 		= 0;	
    	
    	/**
    	* @param database A database 
            connector object */
    	function __construct(&$db)
    	{
    		parent::__construct( '#__hello', 'id', $db );
    	}	
    	
    }
    
    ?>
    


Series this article:
Joomla - Component: Create Hello World Component on Front End
Joomla - Component: Create Hello World Component on Back End
Joomla - Component: Registering Your Component in database
Joomla - Component: Create Component Link for Front End
Joomla - Component: Creating Toolbars
Joomla - Component: Available Toolbar Buttons
Joomla - Component: Creating the Database Table
Joomla - Component: Creating Form Input Data at Back End
Joomla - Component: Saving Data to Database
Joomla - Component: Creating a List Screen
Joomla - Component: Creating Advance List Screen
Joomla - Component: Editing Data
Joomla - Component: Deleting Data
Joomla - Component: Updating publish state
Joomla - Component: Hello From Database at Front End
Joomla - Component: a Hello page
Joomla - Component: Generating Friendly Links
Joomla - Component: Adding Comment ( Create Table )
Joomla - Component: Adding Comment (Create Form)
Joomla - Component: Adding Comment ( Insert Data )
Joomla - Component: Adding Comment ( Displaying Comments )
Joomla: Fast Road to Understand Component Programming


Tag: joomla, cms, Joomla Component Category: PHP Framework Post : November 04th 2007 Read: 22,104 Bookmark and Share

blog comments powered by Disqus