Advertisement

External database connect and display table info in article

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
zc00lness
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Dec 17, 2014 6:29 pm

External database connect and display table info in article

Post by zc00lness » Wed Dec 17, 2014 6:39 pm

I've got an external database where I need to connect, and then display data within a Joomla article.

I'm not a big php guy and expesially in Joomla

I have the following to connect:

<?php
$option = array(); //prevent problems

$option['driver'] = 'mysql'; // Database driver name
$option['host'] = 'localhost'; // Database host name
$option['user'] = 'myusername'; // User for database authentication
$option['password'] = 'mypass'; // Password for database authentication
$option['database'] = 'zcars'; // Database name

$db = JDatabaseDriver::getInstance( $option );
?>

I have the following query where I want to get the data from the db and then display within the page

select * from cars where Make_ID = 13 order by model;

Any ideas on best way to get this query to connect to an external db, and then display the data within an article or even a separate php page?

Advertisement
User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 107
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: External database connect and display table info in arti

Post by sohopros » Wed Dec 17, 2014 7:03 pm

Hi zc00lness!

There is several ways to approach your issue. Here are a couple suggestions:

1 -Create a Joomla module that executes your query and embed it inside an article using load position.
2 - Create an external php file and load it inside the article with an iframe.

Hope it send you in the correct path!
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: External database connect and display table info in arti

Post by sovainfo » Wed Dec 17, 2014 8:42 pm

Add the following:

Code: Select all

$query = $db->getQuery(true)
->select('*')
->from($db->qn('cars'))
->where($db-qn('Make_ID') . ' = 13')
->order($db->qn('model') . ' ASC');

$db->setQuery($query);
$rows = $db->loadObjectList();
Next you will need to add the html code in a foreach $rows as $row and mix it with echo $row-><column name> to put the content of the column there.

Suggest to create a module for it. See docs.jooma.org or take any module as example. No need for the complete structure. Everything could be in <name of module>.php to keep it simple.
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

zc00lness
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Dec 17, 2014 6:29 pm

Re: External database connect and display table info in arti

Post by zc00lness » Thu Dec 18, 2014 4:55 pm

Thank you. I have the connection working and will now just see if i can get the html built. I support this all can go into the one php file .. like the helper.php?

zc00lness
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Dec 17, 2014 6:29 pm

Re: External database connect and display table info in arti

Post by zc00lness » Thu Dec 18, 2014 6:07 pm

Also, is there a way to do this without creating a module? if I just have a standalone php file, it should still be able to use the joomla connection right? can i use $db = JDatabaseDriver::getInstance( $option ); from a php file that is not in a module?

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: External database connect and display table info in arti

Post by sovainfo » Thu Dec 18, 2014 6:20 pm

Have you seen how many php scripts there are outside of the module folders?
They all run in the context of Joomla which allows them to use the Joomla API.

A module script only determines the context for when the script is run. For different needs, you write different scripts (module/component/plugin/template/library/layout). Sounds like you don't understand the structure!
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

zc00lness
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Dec 17, 2014 6:29 pm

Re: External database connect and display table info in arti

Post by zc00lness » Thu Dec 18, 2014 6:27 pm

For sure, I;m a Joomla newbie :)
I'll give it a shot and build a module. Was just curious. I had a feeling I;d need to be inside the Joomla container to use the api. I may still try to make this a one file module however. Thank you for your help - I should be able to get this working.

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: External database connect and display table info in arti

Post by sovainfo » Thu Dec 18, 2014 6:39 pm

Have a look at cli/deletefiles.php or any of the other scripts in cli. Just as the index.php in the root of the joomla installation it starts with setting up the environment so you can use the joomla API.

So, yes you would need to be inside the Joomla container to use the API. The problem is that these are your words to describe something that is not industry standard, meaning there is no such thing as 'Joomla container'! That makes it very difficult to answer such questions.
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

zc00lness
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Dec 17, 2014 6:29 pm

Re: External database connect and display table info in arti

Post by zc00lness » Thu Dec 18, 2014 6:54 pm

yeah sorry about that. I live on the JMS world where we use the term "container" quite a bit. I'm not much of a php or Joomla person but helping someone out. If they'd asked me to put this in weblogic and hook up a small small workflow, i'd be done by now. All about what we know I suppose.

Advertisement

Locked

Return to “General Questions/New to Joomla! 3.x”