Joomla - Component: Deleting Data
It is realy easy for adding delete functionality in Joomla! Follow this steps:
- Open "admin.hello.php" file, add following bold code:
switch($task){ case 'edit': editHello(); break; case 'add': addHello(); break; case 'cancel'; showHello(); break; case 'save'; case 'apply'; saveHello(); break; // This will work when you set task to 'remove case 'remove': removeHello(); break; default: showHello(); break; } - Add removeHello() function like following code:
function removeHello() { global $mainframe; // Initialize variables $db =& JFactory::getDBO(); // Define cid array variable $cid = JRequest::getVar( 'cid' , array() , '' , 'array' ); // Make sure cid array variable content integer format JArrayHelper::toInteger($cid); // If any item selected if (count( $cid )) { // Prepare sql statement, if cid array more than one, // will be "cid1, cid2, ..." $cids = implode( ',', $cid ); // Create sql statement $query = 'DELETE FROM #__hello' . ' WHERE id IN ( '. $cids .' )' ; // Execute query $db->setQuery( $query ); if (!$db->query()) { echo "<script> alert('".$db->getErrorMsg(true)."'); window.history.go(-1); </script>\n"; } } // After all, redirect again to frontpage $mainframe->redirect( "index.php?option=com_hello" ); }
