views:

127

answers:

4

Hello,

I have these files:

"id_1_1.php", "id_1_2.php", "id_1_3.php" etc "id_2_1.php", "id_2_2.php", "id_2_3.php" etc

the number of files is not known because will always grow..

all the files are in same directory..

I want to make a if statement:

  1. to include the files only if their name ends with "_1"
  2. another function to load all the files that start with "id_1"

How can I do this? Thank you!

edit1: no the numbers will not be skipped, once I have another item for id_1_ collection of products I will add new ones as id_1_1, id_1_2 etc.. so no skipping..

+1  A: 
function my_include($f, $s)
{
    @include_once("id_" . $f . "_" . $s . ".php");
}


function first_function($howmany = 100, $whatstart = '1')
{
    for ($i=1; $i <= $howmany; $i++)
    {
        my_include('1', $i)
    }
}

function second_function($howmany = 100, $whatend = '1')
{
    for ($i=1; $i <= $howmany; $i++)
    {
        my_include($i, '1');
    }
}
Svisstack
You might want to make this start at 2 for both functions and check for id_1_1.php manually. Otherwise if you run both of these functions you will include 1_1 twice.
aslum
"the number of files is not known because will always grow.." - your code doesn't really do anything about this.
Cam
@aslum: this file was include only once.
Svisstack
@increditman: yes, i don't read it or this was be added in edit.
Svisstack
+1  A: 

Loosely based on Svisstack's original answer (untested):

function doIncludes($pre='',$post=''){
    for ($i=1;1;$i++)
        if (file_exists($str=$pre.$i.$post.'.php'))
            include($str);
        else
            return;
}

function first_function(){
    doIncludes('id_','_1');
}

function second_function(){
    doIncludes('id_1_');
}
Cam
+1  A: 

This will parse through every file incrementing by one until it finds a file that doesn't exist. Assuming contiguous numbers it should catch every existing file. If you want to include files with a number other then 1 in the name, just change $lookingfor as appropriate.

$lookingfor = 1;
$firstnum=1;
while ($firstnum>0) {
$secondnum=1;
  while ($secondnum>0) {
    $tempfilename = "id_".$firstnum."_".$secondnum.".php";
    if file_exists($tempfilename) {
      if (($firstnum==$lookingfor)||($secondnum==$lookingfor)) {include $tempfilename; }
      $secondnum++;
    } else {
    $secondnum=-1;
    }
  }
$firstnum++;
}
aslum
+1  A: 
// Each of these:
//   - scans the directory for all files
//   - checks each file
//   - for each file, does it match the pattern described
//   - if it does, expand the path
//   - include the file once

function includeFilesBeginningWith($dir, $str) {
    $files = scandir($dir);
    foreach ($files as $file) {
        if (strpos($file, $str) === 0) {
            $path = $dir . '/' . $file;
            include_once($path);
        }
    }
}

function includeFilesEndingWith($dir, $str) {
    $files = scandir($dir);
    foreach ($files as $file) {
        if (strpos(strrev($file), strrev($str)) === 0) {
            $path = $dir . '/' . $file;
            include_once($path);
        }
    }
}

/* To use: - the first parameter is ".",
   the current directory, you may want to
   change this */
includeFilesBeginningWith('.', 'id_1');
includeFilesEndingWith('.', '_1.php');
artlung
great explanation, really really precious for a novice like me.. muchas gracias senor :)
Adrian M.
De nada! It was a helpful exercise for me as well, I used to do this with `exec` and doing an `ls` inside that. `scandir` is a much better starting point.
artlung