1

Having issues trying to perform a foreach within a while loop in PHP. I'm extracting build names from a database table. Those build names happen to be folder names. Within those folders is a folder (either Client or Clients) and within those are folders named after the clients business name. I want the script to show me the build name (accomplished already with an echo) and then list each folder below the build name.

$build_grab = mysql_query("SELECT b_id,b_name FROM builds", $link);
while($build_row = mysql_fetch_array($build_grab))
{
  $b_id = $build_row['b_id'];
  $b_name = $build_row['b_name'];

  $client_dir = is_dir("/somepath/builds/$b_name/Client/");
  $clients_dir = is_dir("/somepath/builds/$b_name/Clients/");

  if ($client_dir == '1') {
    $client_folders = explode(",", exec("ls -1 /somepath/builds/$b_name/Client/"));
  } elseif ($clients_dir == '1') {
    $client_folders = str_replace("\n","", explode(",", exec("ls -1 /somepath/builds/$b_name/Clients/")));
  };

  echo $b_name;
  echo "\r\n";

  foreach($client_folders as $folder)
  {
    echo $folder;
    echo "\r\n";
  }
}

Unfortunately this seems to only give me one (1) client folder name per build and it's always the last client (a-z) in the folder.

Example Output:

build-4.0.0.0
Client-f
build-4.0.0.1
Client-d
build-4.0.0.2
Client-j

What am I missing here or better yet, is there a more efficient way of doing this? Any help is greatly appreciated!

4

4 回答 4

1

You're replacing the $client_folders variable each time. You want to create it as an array before you enter the while loop, then do this

if ($client_dir == '1') {
  $client_folders[] = explode(",", exec("ls -1 /somepath/builds/$b_name/Client/"));
} elseif ($clients_dir == '1') {
  $client_folders[] = str_replace("\n","", explode(",", exec("ls -1 /somepath/builds/$b_name/Clients/")));
};

By the way, this will only enter the first IF, since the second is exactly the same

于 2013-10-18T17:55:26.257 回答
0

Rather than using

$client_folders = str_replace("\n","", explode(",", exec("ls -1 /somepath/builds/$b_name/Clients/")));

I would recommend using the following code to list the folders.

if ($folderHandle = opendir(clients_dir)) {
    while ($file = readdir($folderHandle) {
        echo "$file\n";
    }
    closedir($folderHandle);
}

You can change that code to be conditional, but it should give you a good list.

于 2013-10-18T17:59:49.987 回答
0

exec() only returns the last line of output from the command. You should use the native PHP functions to read the directory list - either opendir/readdir or glob will work.

if ($client_dir == '1') {
  $client_folders = glob("/somepath/builds/$b_name/Client/*", GLOB_ONLYDIR);
} elseif ($clients_dir == '1') {
  $client_folders = glob("/somepath/builds/$b_name/Clients/*", GLOB_ONLYDIR);
};
于 2013-10-18T19:31:00.860 回答
0

Thanks much to mcrumley, your suggestion of using 'glob' fixed it! Sorry crumley, I am apparently to newb to vote up your response. Perhaps other users can help with that.
Thanks also Chris for suggesting mysqli, I will code with it from here out, late adopter here.

Below the new code that resolved the issue.

$build_grab = $mysqli->query("SELECT b_id,b_name FROM builds");
  while($build_row = $build_grab->fetch_array())
{
  $b_id = $build_row['b_id'];
  $b_name = $build_row['b_name'];

  $client_dir = is_dir("/somepath/builds/$b_name/Client/");
  $clients_dir = is_dir("/somepath/builds/$b_name/Clients/");

  if ($client_dir == '1') {
    $client_folders = glob("/somepath/builds/$b_name/Client/*", GLOB_ONLYDIR);
  } elseif ($clients_dir == '1') {
    $client_folders = glob("/somepath/builds/$b_name/Clients/*", GLOB_ONLYDIR);
  };

  echo $b_name;
  echo "\r\n";

  foreach($client_folders as $folder)
  {
    echo $folder;
    echo "\r\n";
  }
}

Thanks again for everyones help!

于 2013-10-20T23:57:25.153 回答