The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed May 23, 2012 9:05 pm 
Joomla! Intern
Joomla! Intern

Joined: Thu Sep 13, 2007 3:20 pm
Posts: 59
I'm working on a component for Joomla 2.5 and am having some issues with how to work with the database. Particularly in wrapping an array within an option tag. I'm assuming I'll probably need to do a foreach loop but I'm not 100% sure.

Here's my normal query:
Code:
$countryopt = array();
$cquery = "SELECT DISTINCT my_column FROM #__my_table";
$db->setQuery($cquery);
$crow = $db->loadRow();
 $variablename = $crow['my_column'];
 $selectoptraw[] = "<option value=\"".$variablename."\">".$variablename."</option>";
$selectopt = implode("\n", $selectoptraw);


How would I duplicate this within Joomla?
I can get it to output the array using loadResultArray but I need to be able to wrap it in the option tags, that's where I guess I need to do a foreach, but it seems most of what I try to do ends up simply resulting in an error and prevents the entire page from loading.

At least in 1.5 I could use normal queries if I needed to and could access external files for posting and running ajax queries and database dumps by simply adding the api to the top of the file. This 2.5 in my opinion makes it 1,000 times harder to develop anything on.


Top
 Profile  
 
PostPosted: Wed May 23, 2012 10:33 pm 
User avatar
Joomla! Master
Joomla! Master

Joined: Wed Aug 17, 2005 10:27 pm
Posts: 14709
Location: Kent, England
[Mod note: Moved from General Forum to 2.5 Coding Forum;]


Top
 Profile  
 
PostPosted: Wed May 23, 2012 11:11 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 435
Location: Chicago, IL
Hey SFraise,

Joomla 2.5 is the LTS which shores up a LOT of security vulnerabilities from 1.5 and depreciates legacy code, which is probably why your Joomla coding world has been turned upside down. How are your OOP and MVC skills? I ask, because Joomla is starting to mature, and what you're probably viewing as "harder" is actually more secure and proper OOP implementation of Joomla's MVC Framework.

I wrote components the exact same way you started, took the HelloWorld compnent example and hand rolled all the code myself, I've since re-written all the components properly and fully integrated into Joomla's Framework. I can only sum up my diatribe with it is much harder to learn, but so much easier to execute once you do.

Now to your question. You need a static function in your component helper file to pull the data, normally you would put this in your model file, but I'm guessing you want universal access to these variables from all views to display these options. If not, the abstract helper class with static calls is still the best place IMO.

Code:
   
static function getOptions($table)
   {
      $db = JFactory::getDbo();
      $db->setQuery(
            'SELECT o.id AS value, o.title AS text' .
            ' FROM ' . $db->nameQuote($table) . ' AS o' .
            ' ORDER BY o.id ASC'
      );
      $options = $db->loadObjectList();
   
      // Check for a database error.
      if ($db->getErrorNum())
      {
         JError::raiseNotice(500, $db->getErrorMsg());
         return null;
      }
   
      foreach ($options as &$option)
      {
         $option->text = '- ' . $option->text;
      }
   
      return $options;
   }


Now, using your helper class you can load a list of whatever custom table names you need by passing in your table name in the #__myTableName format. To display these options in a drop down you put the following line of code in your view file, usually default.php:

Code:
echo JHtml::_('select.options', MyComponentHelper::getOptions('#__myCustomTable'), 'value', 'text', $mySelectedValue, true);


You can even integrate your options with the usual categories, access drop downs so your presentation is seamless and automatically updates and registers its state when you select a new option without the need to create a form or submit button.

Hope this helps...

http://docs.joomla.org/JHtmlSelect::options/1.6
http://docs.joomla.org/Accessing_the_da ... _JDatabase


Top
 Profile  
 
PostPosted: Sat May 26, 2012 8:40 pm 
Joomla! Intern
Joomla! Intern

Joined: Thu Sep 13, 2007 3:20 pm
Posts: 59
Thanks bbolli, I know not just Joomla, but php as a whole is making the move over to a more object oriented design which I understand, it's just a pain having to learn something new and change with the times lol. I only wish there was a good tutorial with better examples than what's in the current Joomla docs to better learn all of the classes and how to properly make the move away from the api framework of old to the new.

Another issue I need to tackle is my post files and php files that my ajax functions call. Before I would add the api to the top of the file and away it went, now I guess I'll have to set up switch functions and call them through a url within the joomla environment.


Top
 Profile  
 
PostPosted: Fri Jun 01, 2012 8:24 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 435
Location: Chicago, IL
lol, I know how you feel. I *was* a PHP web developer and was recently hired through a contract to hire IT firm. I tried to quick my first day when I realized I'd being doing Joomla!

Needless to say I didn't, and have come full circle and am a complete advocate. I was banging my head against the walls like you last year trying to piece-meal my hand rolled classes into the Hello World example. I was probing for PHP and OOP experience, because if you have it, buy the Joomla Programming yesterday. I pasted a link to Amazon, who sells it for around $30.

That book will provide EVERYTHING a season PHP developer looking to full integrate into Joomla's 2.5 MVC Framework, and more importantly addresses each and every concern you listed for the online documentation. There's even a section on using Joomla's built in AJAX starting at page 444. It not only reviews in detail how core template, components, modules and plugins work; but walks you through the customization and creation of customized versions of each as well.

It will lead you to the promise land! :D

http://www.amazon.com/Joomla-Programmin ... 013278081X


Top
 Profile  
 
PostPosted: Fri Jun 08, 2012 8:43 pm 
Joomla! Intern
Joomla! Intern

Joined: Thu Sep 13, 2007 3:20 pm
Posts: 59
Got the book, extremely helpful.
Have an excellent handle on using the database within Joomla now.
Not 100% sure I want to make the leap into using mootools for my ajax stuff quite yet, I've always ditched mootools for jQuery whenever possible but I'll play with it.


Top
 Profile  
 
PostPosted: Fri Jun 08, 2012 9:36 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 435
Location: Chicago, IL
Funny you should mention that as I recently came to the same conclusion. It appears mootools is fantastic for creating dynamic web 2.0 user interfaces, but nothing trumps jQuery's DOM traversing and manipulation in my opinion.

There's a section in Chapter 12 on loading jQuery (and other frameworks) without conflicts you might want to check out. Good Luck!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 



Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group