Bectin
  1. You are here:  
  2. Home
  3. Random Insights
  4. PHP

PHP code for use in a Joomla template to set the background image to be the full_text image from the article or the image for the category

 

 

<!-- 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 -->

 

Using the Full Text image as a Background image

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.

To retrieve the URL of the intro image in a Joomla article

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'} . "'>";

?>

 

Problem with accented characters not showing up using PHP and MySQL

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'

Multi-dimensional search in an array like a MySQL SELECT with WHERE AND AND AND

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;
}

  1. PHP Age Check Function
  2. How to have a copyright date automatically generated by PHP
  3. PHP code for Joomla that is required to include CSS link in your index.php file with path generated automatically to match your template
  4. PHP code to display title of current page

Page 5 of 7

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Main Menu

  • Home
  • Services
  • Portfolio
  • About
    • Bectin's Founder
  • Contact
  • Random Insights
    • Joomla
    • Joomla Extensions
    • Web Sites
    • Windows Computers
    • Zen Cart
    • Magento
    • MySQL
    • Business
    • Technology
    • Google Analytics & Webmaster Tools
    • PayPal
    • PHP