Page 1 of 1
Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 10:46 am
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
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 11:04 am
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...
( 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
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 11:15 am
by php_east
maybe you want this
Code: Select all
$this->db->setQuery($query);
$this->db->Query($query);
$rows = $this->db->loadObjectList();
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 11:19 am
by ben.artiss
Hmmm, I'm only after one row, will that work in the same way php_east? Thanks for the replies.
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:05 pm
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.
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:10 pm
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?
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:16 pm
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";
}
}
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:22 pm
by ben.artiss
You're a good man you know that

thanks for your help and patience man.
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:25 pm
by php_east
for a single item you can use $row = $database -> loadObject();
Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:26 pm
by php_east

thanks. have fun. please don't get sucked too deep into joomla

Re: Adding new function to Joomla! RDAutos
Posted: Sat Mar 28, 2009 12:29 pm
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.