Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 862129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:05:27+00:00 2026-05-15T09:05:27+00:00

I have this code i have been using….but i need a conditional where it

  • 0

I have this code i have been using….but i need a conditional where it will read all the sub directories of /bg to select an image as opposed to a specific folder if they were on a subpage.

Heres my code so far which works perfectly for all subpages to display specific images:

//This would tell us its on the homepage if it helps:
$this->level() == 0

//This is the code so far
$path = '/home/sites/mydomain.co.uk/public_html/public/images/bg/'.$this->slug;


$homepagefile = URL_PUBLIC.'public/images/bg/'.$this->slug.'/main.jpg';

$bgimagearray = array();
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile() && !preg_match('\.jpg$/', $fileinfo->getFilename())) {
        $bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
    }
}

$bgimage = array_rand($bgimagearray);

?>

<div id="bg">
    <div>
        <table cellspacing="0" cellpadding="0">
            <tr>
                <td><img src="<?php echo $file.trim($bgimagearray[$bgimage], "'"); ?>" alt=""/></td>
            </tr>
        </table>
    </div>
</div>

Any help would be appreciated, im sure its not rocket science but ive tried a few ways and cant get my head around it.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:05:29+00:00Added an answer on May 15, 2026 at 9:05 am

    This is what I ‘ve come up with. Haven’t tested it though…

    <?php
    $isHome = $this->level() == 0
    
    $path = '/home/sites/mydomain.co.uk/public_html/public/images/bg/';
    if (!$isHome) $path .= $this->slug;
    
    $homepagefile = URL_PUBLIC.'public/images/bg/'.$this->slug.'/main.jpg';
    
    $bgimagearray = array();
    $iterator = new DirectoryIterator($path);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile() && !preg_match('\.jpg$/', $fileinfo->getFilename()) &&  !$isHome) {
            $bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
        } else if ($fileinfo->isDir() && $isHome) {
            $iterator2 = new DirectoryIterator($path . $fileinfo->getFilename());
            foreach ($iterator2 as $fileinfo2) {
                if ($fileinfo2->isFile() && !preg_match('\.jpg$/', $fileinfo2->getFilename())) {
                    $bgimagearray[] = "'" . $fileinfo->getFilename() . '/' . $fileinfo2->getFilename() . "'";
                }
            }
        }
    }
    
    $bgimage = array_rand($bgimagearray);
    
    ?>
    
    <div id="bg">
        <div>
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="<?php echo $file.trim($bgimagearray[$bgimage], "'"); ?>" alt=""/></td>
                </tr>
            </table>
        </div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using this code, but have been running into some memory issues:
I have been using this code to read in a file from a document
I have been using this code to implement a Popup JDialog of sorts, like
I have been using this code with great success to pull out the first
I have been using this code to parse xml till now. Its was working
I found this code and have been using this to automatically close the window...It
I have been using the following code for loading JQuery in all of my
In Objective C I have been using this code to format a value so
Hi I have been using this code to fetch data from mysql database and
I have been using this code to check if a string is empty: if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.