1

I have a question about php include. Here goes the existing code that i have.

<?php
srand();
$files = array("folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php");
$rand = array_rand($files);
include ($files[$rand]);
?>

This code actually works very well for me now as the content displaying randomly. But, i would like to get something better. Because, by using this code, everytime i have added a new content like content5.php, content6.php and so on, i have to bother to update the code above to make new content.php appear.

Is there any solution i can have, to not to bother the code above everytime i add a new content.php and the new content.php appears automatically when added? Is it possible?

Updated: New testing code(tested failed again with part of my page mising)

    <?php
$sDirectory     = 'myfolder';
if( is_dir( $sDirectory ) ) 
{
    $rDir = opendir( $sDirectory );
    while( ( $sFile = readdir( $rDir ) ) !== FALSE ) 
    {
        if( ( $sFile == '.' ) || ( $sFile === '..' ) )
        {
            continue;
        }
        $aFiles[] = $sDirectory . '/' . $sFile;
    }
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );
?>
Jornes
  • 287
  • 1
  • 3
  • 15
  • for that you have to update the $files array dynamically, there is no enough code to proceed, you just hard-coded the content1.php,content2.php,..... you are not getting it dynamically, then how can you add new content.php there? – Ayyanar G Mar 21 '15 at 04:50
  • $sDirectory = 'myfolder/'; should be $sDirectory = 'myfolder'; as the directory separator is already appended between $sDirectory and $sFile. Or just remove the bottom one. – Vladimir Ramik Mar 21 '15 at 05:29
  • I have updated again. But, it seems not to work. And part of my page missing. I'm wondering if the code format i implemented was wrong. – Jornes Mar 21 '15 at 05:37
  • @AyyanarG, i don't understand what are you trying to ask. I'm looking for a solution. Hope that i can get this sorted out with a best solution. Thanks! – Jornes Mar 21 '15 at 08:45

1 Answers1

1
$sDirectory     = 'includes';
if( is_dir( $sDirectory ) ) 
{
    $rDir = opendir( $sDirectory );
    while( ( $sFile = readdir( $rDir ) ) !== FALSE ) 
    {
        if( ( $sFile == '.' ) || ( $sFile === '..' ) )
        {
            continue;
        }
        $aFiles[] = $sDirectory . '/' . $sFile;
    }
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );
Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
  • Hi @Vladimir Ramik , thank you for prompt answer! I tried your code. But, it doesn't work. I'm wondering if i have wrongly placed the code to my site? Would you please provide a proper code or a further explanation? I'm still new in php coding. Thanks! – Jornes Mar 21 '15 at 05:06
  • Missed the random bit at the bottom. Fixed. – Vladimir Ramik Mar 21 '15 at 05:14
  • Hi @Vladmir Ramik, i have updated my question with your code in. Is that the way to implement the code to my site? But, it tested failed. Is there anything wrong? – Jornes Mar 21 '15 at 05:21
  • you have 2 /s 'myfolder/' and $aFiles[] = $sDirectory . '/' . $sFile; – Vladimir Ramik Mar 21 '15 at 05:24
  • Sorry! I don't understand. Would you please amend the code in my question or your answer so that i know what's going wrong? I'm totally new in php coding. Thanks! :) – Jornes Mar 21 '15 at 05:28
  • Hi @Vladimir Ramik, your code is working. But the only condition is i have to place a full path name in the `myfolder` as you mentioned just now. Example: `/home/mixwebde/public_html/{my username}/wp-content/themes/mytheme/myfolder`. By the way, thanks for your code! Without your code, i can't make it work in a short period of time. :) – Jornes Mar 21 '15 at 10:08
  • try relative if your script is in /home/mixwebde/public_html/{my username}/wp-content/themes/mytheme/myfolder and includes folder is in /home/mixwebde/public_html/{my username}/wp-content/includes you can go back up a directory using '../includes' http://stackoverflow.com/questions/13898894/php-relative-paths-require – Vladimir Ramik Mar 21 '15 at 13:02