Joomla database query in article - joomla

This seems to be such an easy thing to do but I can't fine any information on how to do it. Can someone please tell me how to add the results of a query to an article in Joomla 2.5?
In an article this is what I am trying to accomplish.
Bob's favorite color is: <<SELECT favorite_color FROM COLORS WHERE name = 'Bob'>>
I have no idea how to do this.

First thing is first, download and install the Sourcerer plugin which will allow you to add custom code to your articles.
One done, open your article and click the "Add Code" button below the textarea. Here you can add you custom code.
I'll write more or less the query you want using Joomla coding standards which most people seem to forget about:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('favorite_color')))
->from($db->quoteName('colors'))
->where($db->quoteName('name') . ' = '. $db->quote('bob'));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row) {
echo $row->favorite_color;
}
Notes that if the colors tables belongs to a Joomla extensions, then use #__colors
Hope this helps

You can make a content plugin. THe other thing is that you can make a module to show the data and then use loadposition to load the module.

Related

Custom SEO article title in Joomla

While it's possible to manually enter the full title of a page displaying a category, how would I go about doing the same for an article page in Joomla 2.5?
The default is to use the article title as the page title, e.g. "How to care for goldfish", with an option to add the site name.
What I want is to have the title as follows:
How to care for goldfish | Fish care guides | FishSite.com
The obvious solution would be just to put all that in the title of the article, but then I'd have a problem in the "Latest articles" module, it would become unreadable with all the extra text.
How to solve this? Plugin, coding, or some clever solution? It needs to be more or less automatic though, I can access the Joomla database and change each page title manually but that wouldn't be feasible. If manual text entry is needed, it has to be in Joomla.
The plugin system responds to events, so the easiest solution would be a either a Content or System plugin that manipulates the Title by appending the relevant information prior to display.
I think with a content plugin you could use the onContentPrepare event to do all the work.
In rough psuedo code (i.e. this won't run), I would probably end up with something like this:
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
// Don't run this plugin when the content is being indexed
if ($context == 'com_finder.indexer') {
return true;
}
// Perform a sanity check to make sure we're on an article layout
// Build new title
}

joomla Modules - Between Article or inside Article? What is correct?

I want to place articles and modules on my page. It should look like my scatch:
I am wondering if i can make 4 articles and one module (information box) and place this inbetween those articles, Or should i just create one article wich looks like the hole area (article 1-4 + INformation box)? So far i have tried to do {loadposition infobox-pos} into article 2. But than the box-width is not 100% but 50%.
Than aggain if i would make one big article with the contents from article 1-4 it would not fit great into my responsive layout.
I use T3-Framework.
Joomla doesn't support this natively, since module output is controlled by the template, and component output (in this case, com_content, view=category, layout=blog) is handled before modules are rendered.
You can proceed in many ways, I am listing them in order of decreasing ease:
Write a jquery script that moves the pieces in the browser. You could also use mootools and it's bundled with joomla, but odds are you already have jquery loaded anyways.
Write a view override for abovementioned view: so copy /components/com_content/view/category/tmpl to /templates/your_template/html/com_content/category (copy all files). Then edit blog.php and insert your module there, use something like /plugins/content/loadmodule/loadmodule.php :
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModules($position);
$params = array('style' => $style);
foreach ($modules as $module) {
echo $renderer->render($module, $params);
}
Write a content plugin
you can also use {loadmodule YourCustomModuleTitle} and place this in <div> with custom style attributes like
<div style="width:100%;float:right">{loadmodule MyModule}</div>
or
<div class="something">{loadmodule MyModule}</div>
Note: the module which loaded must be assigned to the same menu item or "all"

How to use breadcrumbs in Joomla in custom components?

I wrote simple component by this example, but when i entering it i see Home instead of my components name.
And also is it possible to get deeper, for example joomla->my_component->some_other_component, but exactly after mine.
P.S.
Found http://docs.joomla.org/How_to_add_breadcrumbs, but how use it automatically in every view?
Try this:
$mainframe = &JFactory::getApplication();
$pathway =& $mainframe->getPathway();
$breadcrumb = $pathway->setPathway(array());
$pathway->addItem( JText::_( 'YOUR_BREDCRUMB_ITEM' ),'');
Replace YOUR_BREDCRUMB_ITEM with your item name. Hope this will help.

Dynamic Image Links Depending on Excerpt/'Single Post' View in Wordpress

I'm working on a new Wordpress theme; the default index view displays the excerpts of recent posts. Some posts will be regarding file downloads, and include an image, description, and link to the location where the described files are hosted. The images for these types of posts will be anchored with links(other types of posts may contain images that are not linked).
For these types of posts, I would like the images to link to their entry's full post views(single.php) when displayed in excerpts, but for the same images to link to an external download link when displayed as part of the full post view.
I'm not sure how exactly I would accomplish that.
Any help would be greatly appreciated!
Because I don't have post thumbnails defined, and since posts may or may not contain multiple images, I ended up doing it this way:
I turned off tags in excerpts(I had an excerpt plugin enabling tags in excerpt), then I added the following to the function.php file:
function catch_that_image() {
global $post, $posts;
$first_img = ''; ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img = "/images/default.jpg"; }
return $first_img;}
Then I added the following to the main index template file in between the posts title and the excerpt/content:
<p><img src="<?php echo catch_that_image() ?>"></p>
Random note: It's wrapped in a for styling reasons.
Thanx again for guiding me in the right direction.
if your theme uses 'the_excerpt()' for the front page, I think you can add a filter in functions.php, and with a regexp change the link href from the download link to the permalink.
something like,
function replace_link($content) {
if (is_home())
return preg_replace('regular_expression', get_permalink(), $content);
else
return $content;
}
add_filter('the_excerpt', 'replace_link');
I can't create an actual regular expression without knowing what your download link looks like

Place certain article anywhere in joomla template

Is it possible in Joomla to place a certain article in a template, additionally to the normal content? I want the article to show up on every page.
You can simply take it from the databse and print its content. In the template, where you want to show the article, write this:
$id=/*Id of the article to show*/;
$db=&JFactory::getDBO();
$db->setQuery("SELECT * FROM #__content WHERE id=$id");
$item=$db->loadObject();
echo $item->introtext;
UPDATE: ENABLE PLUGINS
I can't find where i've used that code and i can't copy-paste it, so i try to write it again by looking at the view.html.php of the com_content:
JPluginHelper::importPlugin('content');
$dispatcher =& JDispatcher::getInstance();
$params = &$mainframe->getParams();
$dispatcher->trigger('onPrepareContent', array (&$item, &$params, 0));
//The last line triggers the onPrepareContent event, so if it does not work maybe you need other events, so try with onAfterDisplayTitle, onBeforeDisplayContent or onAfterDisplayContent
Have you seen this? http://extensions.joomla.org/extensions/news-display/content-embed/7528
It allows you to place any article as a module on your Joomla site. And with modules you can have them displayed site wide.

Resources