GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


joomla:table_insert_and_update_helper_functions

Using JTable is a great, and the correct way to manipulate data tables in Joomla, but having some helper functions makes it much easier.

function InsertTable($tablename, $cols, $values) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
 
        $query
        ->insert($db->quoteName('#__'.$tablename))
        ->columns($db->quoteName($cols))
        ->values(implode(',', $values));
 
        $db->setQuery($query);
        $this->dmesg(2,2,"Query",$query->dump());
        try {
            $db->execute();
        } catch (Exception $e) {
            // call failed, do something here. 
        }
 
}
 
function UpdateTable($tablename, $values, $Where) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
 
        $query->update($db->quoteName('#__'.$tablename))->set($values)->where($Where);
 
        $db->setQuery($query);
        try {
            $db->execute();
        } catch (Exception $e) {
            // Failed, do something here. 
        }
 
}

InsertTable takes the table name (without the prefix), an string of comma separated column names and an array of values. UpdateTable takes the table name (without the prefix), a string of updates (column='value',) and a where clause as a string.

/home/gen.uk/domains/wiki.gen.uk/public_html/data/pages/joomla/table_insert_and_update_helper_functions.txt · Last modified: 2019/05/20 10:15 by genadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki