Adding new function to Joomla! RDAutos

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Adding new function to Joomla! RDAutos

Post by ben.artiss »

Hi, I'm having a bit of trouble trying to duplicate rows in a database using the Joomla! framework. So far, I've got it to say it's duplicated a row, which means I'm heading on the right track, but it doesn't actually duplicate the row which is most likely due to the function. I'm not very confident with classes and it's all object orientated, and I'm certainly not used to handling a database in this way!

I think the problem lies in this function:

Code: Select all

    function duplicate($cid) {
        if (is_numeric($cid) && $cid>0) {
            JArrayHelper::toInteger($cid);
            $query = "SELECT * FROM #__rdautos_information WHERE carid=$cid LIMIT 1";
            $this->_db->setQuery( $query );
 
            if (!$this->_db->query()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
 
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            $rowKeys = array_keys($row);
            $rowValues = array_values($row);
 
            $sql = "INSERT INTO #__rdautos_information SET ";
 
            for ($i=3; $i<count($rowKeys); $i+=2) {
                if ($i!=3) {$sql .= ", ";}
                $sql .= $rowKeys[$i]." = '".$rowValues[$i]."'";
            }
 
            $this->_db->setQuery( $sql );
 
            if (!$this->_db->query()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
 
        }
 
        return true;
    }
I have a feeling that I'm accessing the row's information incorrectly, i.e. everything is object-orientated until $result = mysql_query($query); but I don't know how to access the row using e.g. $this->_db->getRow

Is anyone familiar with how Joomla communicates with the database?

Thanks, Ben
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

ben.artiss wrote:but I don't know how to access the row using e.g. $this->_db->getRow
Is anyone familiar with how Joomla communicates with the database?
Thanks, Ben
strange are you getting $this->_db from. the usual way of defining a db access is $this->db, $db being obvious what it is.
but some may have purposedly used $this->_db, which is still fine of course, provided it is declared properly.

what you can do is try and use your own declaration before entering that sequence, just to be sure. here are some examples...

Code: Select all

 
$database = &JFactory::getDBO();
or
$db = &JFactory::getDBO();
or
$this->db= &JFactory::getDBO();
 
so in your case, for $this->_db you may wish to declare the db at the top like this...

Code: Select all

$this->_db= &JFactory::getDBO();
( i am assuming it isn't declared properly elsewhere )

the db class provides numerous facilties. normal ueful ones as follows...

Code: Select all

$db->setQuery($query);
$db->Query($query);
$db->getErrorNum();
$db->getErrorMsg();
 
it is all in the Joomla API, which of course is quite a bit to digest
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

maybe you want this

Code: Select all

    $this->db->setQuery($query);
    $this->db->Query($query);
    $rows = $this->db->loadObjectList(); 
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Adding new function to Joomla! RDAutos

Post by ben.artiss »

Hmmm, I'm only after one row, will that work in the same way php_east? Thanks for the replies.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

yes, just use $rows[0]; then.

p/s there are several db access methods for joomla, i use only this method to simplify things and keep everything in object. in case it does not return you what you want, just ring. but i think you should be alright with it. its good for me.
Last edited by php_east on Sat Mar 28, 2009 12:11 pm, edited 1 time in total.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Adding new function to Joomla! RDAutos

Post by ben.artiss »

I've taken a bit more of a direct approach than before, and I've narrowed down the problem (but still have a problem):

Code: Select all

    function duplicate() {
        global $option;
 
        $cid = JRequest::getInt('cid', 0);
 
        if ($cid==0) {
            global $mainframe;
            JError::raiseWarning(100, JText::_( 'Failed to duplicate car' ));
            $mainframe->redirect('index.php?option=com_rdautos');
        }
 
        $db =& JFactory::getDBO();
        $query = "SELECT * FROM #__rdautos_information WHERE carid='$cid' LIMIT 1";
        $db->setQuery($query);
 
        if (!$db->query()) {
            echo "<script>alert('Error in controllers/application.php - duplicate broken - Please report this error to the webmaster.');
            window.history.go(-1);</script>\n";      
        }
 
        [color=#FF5500]$row = $db->loadRow();[/color]
        [color=#FF5500]$rowKeys = array_keys($row);[/color]
        $rowValues = array_values($row);
 
        $sql = "INSERT INTO #__rdautos_information SET ";
 
        for ($i=3; $i<count($rowKeys); $i+=2) {
            if ($i!=3) {$sql .= ", ";}
            $sql .= $rowKeys[$i]." = '".$rowValues[$i]."'";
        }
 
        die($sql);
 
        $db->setQuery($sql);
 
        if (!$db->query()) {
            echo "<script>alert('Error in controllers/application.php - duplicate broken - Please report this error to the webmaster.');
            window.history.go(-1);</script>\n";      
        }
 
        $this->setRedirect('index.php?option='.$option, JText::_( 'Car has been duplicated successfully' ));
    }
The $row = $db->loadRow(); part successfully fetches the information (i.e. each item of the array has the right value), however, the keys in the array need to be the names of the columns and not numbers (that's where the trouble is coming from). Is there a way to change the keys to be the names of the columns easily?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

use the method i gave, the column names are in it...here another good example...

Code: Select all

function getCategories() {
  global $database;
 
  $sql = 'SELECT * FROM #__categories';
  $database->setQuery( $sql );
  $rows = $database->loadObjectList();
  foreach ( $rows as $row ) {
    echo "$row->title: $row->description\n";
  }
 
}
 
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Adding new function to Joomla! RDAutos

Post by ben.artiss »

You're a good man you know that :wink: thanks for your help and patience man.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

for a single item you can use $row = $database -> loadObject();
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding new function to Joomla! RDAutos

Post by php_east »

:drunk: thanks. have fun. please don't get sucked too deep into joomla :)
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Adding new function to Joomla! RDAutos

Post by ben.artiss »

It's a project for someone else, and will most certainly be the last time I use Joomla I promise! Thanks again.
Post Reply