<!-- bectin -->
<?php
// This code sets the background images of the body tag to be the full_text image from the article.
// If no article is assigned, it chooses the image from the first article in the category of the selected article.
// If the first article in the category doesn't have an image then it chooses the default.
$app = JFactory::getApplication();
if($app->input->getCmd('option') == "com_content" && $app->input->getCmd('view') == "article" ) {
$article_id = $app->input->getCmd('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('catid'));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id') . ' = '. $db->quote($article_id));
$db->setQuery($query);
$cat_id = $db->loadResult();
// if the current page has an assigned background photo
$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID
$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
// Print the image
if ($pictures->{'image_fulltext'}!='') // there is an image in the image_fulltext field
{
echo "<style> body {background: url('../" . $pictures->{'image_fulltext'} . $pictures->{'image_fulltext_alt'} . "');background-attachment: fixed; color: #444;}</style>";
}
else
{
//If the current article does not have an assigned background photo choose category image
$query="SELECT params FROM #__categories WHERE id=$cat_id";
$db->setQuery($query);
$db->query();
$article_images= $db->loadAssocList();
$pictures = json_decode($article_images[0][params]); // Split the parameters apart
if ($pictures->{'image'}!='') // there is an image in the image field
{
echo "<style> body {background: url('../" . $pictures->{'image'} . "');background-attachment: fixed; color: #444;}</style>";
}
else
{
echo "<style> body {background: url('/images/backgrounds/bg2000.jpg');background-attachment: fixed; color: #444;}</style>";
}
}
}
// set background for contacts
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($view=='contact') echo "<style> body {background: url('/images/backgrounds/run.jpg');background-attachment: fixed; color: #444;}</style>";
?>
<!-- end bectin -->
I've often had customers that wanted to be able to configure a unique page for the background of each page on a site. Whether this is a good idea or not is irrelevant.
Here's how I do it.
Add the following text to the index.php file for the tempalte right before the </head> tag. .
$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID
$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
echo "<style> body {background: url('../" . $pictures->{'image_fulltext'} . $pictures->{'image_fulltext_alt'} . "');}</style>";
You may want to set a default background in the regular css file for the site so that no page will have a blank background.
This code is taken from: http://www.eighttwentydesign.com/blog/all/149-how-to-get-article-s-intro-image-in-joomla-using-php
They provide a more detailed explanation of how to use this code. Note, I had to make one change from their original code to include a / character after the img src=' text.
<?php
// Article Image
$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID
$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
// Print the image
echo "<img src='/" . $pictures->{'image_intro'} . "' alt='" . $pictures->{'image_intro_alt'} . "'>";
?>
If your data is loosing its accented characters when you retrieve and store it in your database, try using the following command:
SET NAMES 'utf8'
I never know when I should be going directly to the database to do a search and when I should search within a PHP array where I've already loaded the database data. Anyway, there are times when it's clear that searching an array is necessary and I like to be able to use a search which is essentially the same as an SQL query like:
SELECT `field_name` FROM table_name WHERE `field2` = 'something' AND `field3` = 'other' AND `field4` = 'another'
to do this I used the following function:
function searcharray2($value, $key, $value2, $key2, $value3, $key3, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
foreach ($array as $m => $val) {
if ($val[$key2] == $value2 AND $k==$m) {
foreach ($array as $n => $val) {
if ($val[$key3] == $value3 AND $k==$n) {
return $k;
}
}
}
}
}
}
return null;
}