I have the following code. I'm looking to do what's in the comment which is to populate a selectbox with a name of directories. How would I do that?
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//extract directory names and populate selectbox
}
}
At maybe its simplest:
echo "<select name=\"something\">";
foreach(glob("repository/{$name[0]}/*", GLOB_ONLYDIR) as $dir) {
echo "<option>$dir</option>";
}
echo "</select>";
A quick and dirty solution, assuming result is the directory you want to show:
echo '<select id="directories" name="directories">';
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo '<option value="' . $result . '">' . $result . '</option>';
}
}
echo '</select>';
Perhaps I've not understood the question but surely it's just:
<?php
$selectString = '<select>';
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') {
continue;
}
if (is_dir($path . '/' . $result)) {
$selectString .= "<option>{$result}</option>";
}
}
$selectString .= "</select>"
You can then put $selectString wherever you need it.
Related
I have one little question, this is my code to list all files from a folder and subfolders;
if ($handle = opendir($dir)) {
$allFiles = array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir . "/" . $entry)){
$allFiles[] = "D: " . $dir . "/" . $entry;
}else{
$extension = strtoupper(pathinfo($entry, PATHINFO_EXTENSION));
$fileNoExten = pathinfo($entry, PATHINFO_FILENAME);
$directory = substr(str_replace('/', ' > ', $dir), $rootLenOnce + 3);
$listagem .= '<tr>';
$listagem .= "<td><a href='../" . $dir . "/" . $entry . "' ' target='_blank'>" . $entry . "</a></td>";
//$listagem .= "<td><small>" . $directory . "</small></td>";
$listagem .= "<td>" . $extension . "</td>";
$listagem .= "<td><a class='download-cell' href='../".$dir ."/". $entry."' ' download> <i class='fa fa-download' ></i></a></td>";
$listagem .= "<td class='display-none'>" . $fileNoExten . "</td>";
$allFiles[] = "F: " . $dir . "/" . $entry;
$listagem .= '</tr>';
echo "<pre>"; print_r(glob("*.pdf")); echo "</pre>";
}
}
}
closedir($handle);
foreach($allFiles as $value){
$displayName = substr($value, $rootLen + 4);
$fileName = substr($value, 3);
$linkName = str_replace(" ", "%20", substr($value, $pathLen + 3));
if (is_dir($fileName)) {
myScanDirPdf($fileName, $level + 1, strlen($fileName),$rootLenOnce);
}
}
}
return $listagem;
}
what i need is to filtrate the search, to search only .pdf files.
Someone can help me plz!
Thks!
i try with the glob function, but not with great results.
Thks!
When you are looping through each file, you can use
if(stripos($fileName, ".pdf"))
Hope this will help
I have one more suggestion in listing all the files and subfolders.
You can use Recursive Iterator
$folderName = $_POST['folderName'];
$dir = new RecursiveDirectoryIterator($folderName);
$it = new RecursiveIteratorIterator($dir);
foreach ($it as $fileinfo) {
if ($fileinfo->isDir()) {
}elseif ($fileinfo->isFile()) {
$fileName = $fileinfo->getFileName();
if(stripos($fileName, ".pdf")) {
//do what you need to do
}
}
}
Why does foreach stop after include (second)? Here is my code:
define('MONITORING_MOD', dirname(__FILE__) . '/');
$entries = scandir(MONITORING_MOD);
foreach($entries as $entry) {
if (preg_match('%[a-zA-Z0-9_-]%', $entry)) {
if(is_file(MONITORING_MOD . $entry) && $entry != 'init.php') {
include_once(MONITORING_MOD . $entry);
} else if ($entry != 'init.php') {
$sub_entries = scandir(MONITORING_MOD . $entry);
foreach($sub_entries as $sub_entry) {
echo MONITORING_MOD . $entry . '/' . $sub_entry;
if (preg_match('%[a-zA-Z0-9_-]%', $sub_entry)) {
include(MONITORING_MOD . $entry . '/' . $sub_entry);
}
}
}
}
}
I tried to use include_once, making special function and it did not help.
Another include_once in this code works perfectly and outside foreach too.
I was asked to modify existing code to allow multi-level page structure. Each page can have one parent, and one page can have multiple children.
I will present following code. My question is is there a more efficient way of doing this? I am hard-coding the number of levels that are reachable.
Is there easier way to do this automatically without hard coding? Recursion perhaps?
function createMenu($parents) {
foreach ($parents as $parent) {
$parentName = $this->getPagefromID($parent);
$kids = $this->areThereKids($parent);
if ($kids == "yes") {
echo "<ul class=\"children\">\n";
$query = "SELECT * FROM pages";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$thisparent = $row['Parent'];
$name = $row['Name'];
$id2 = $row['ID'];
if ($thisparent == $parent) {
echo "<li>" . $name . "</li>\n";
if($this->areThereKids($id2) == "yes") {
$children = $this->getMyKids($id2);
foreach($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// more sub
if($this->areThereKids($kidid) == "yes") {
$children1 = $this->getMyKids($kidid);
foreach($children1 as $kid1) {
$info1 = $this->getPersonInformation($kid1);
$kidid1 = $info1[0]['ID'];
echo "<li>-" . $info1[0]['Name'] . "</li>\n";
if($this->areThereKids($kidid1) == "yes") {
$children2 = $this->getMyKids($kidid1);
foreach($children2 as $kid2) {
$info2 = $this->getPersonInformation($kid2);
$kidid2 = $info2[0]['ID'];
echo "<li>--" . $info2[0]['Name'] . "</li>\n";
// No need for 6 levels.
}
}
}
}
}
}
}
}
echo "</ul>\n";
}
echo "</li>\n";
}
}
This would be more efficiently done with recursion.
Identify the steps of the code that are repeated and turn them into a subroutine. From your code, we have the following repeated unit:
(...)
if($this->areThereKids($id2) == "yes") {
$children = $this->getMyKids($id2);
foreach($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// more sub
if($this->areThereKids($kidid) == "yes") {
(...)
Turn that into a function (assuming you're working with an object):
function dealWithKids($par) {
if ($this->areThereKids($par) == 'yes') {
$children = $this->getMyKids($par);
foreach ($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// instead of repeating the same steps again,
// call the dealWithKids function here
$this->dealWithKids($kidid);
}
}
}
...and then call that function instead of the repeated units of code:
while ($row = mysql_fetch_assoc($result)) {
$thisparent = $row['Parent'];
$name = $row['Name'];
$id2 = $row['ID'];
if ($thisparent == $parent) {
echo "<li>" . $name . "</li>\n";
// instead of this:
// if($this->areThereKids($id2) == "yes") {
// do this:
$this->dealWithKids($parent);
}
}
My code loops through a directory and displays all the files and folders where I have a index.php file that I don't want to be displayed.
<?php
$directory = 'jocuri';
$row = 0;
if ($handle = opendir($directory.'/'))
{
echo '<table border="1">';
while ($cat = readdir($handle))
{
if ($cat != '.' && $cat != '..')
{
if($row==0) echo '<tr>';
echo '<td align="center">';
echo ' <a href="'.$directory.'/'.$cat.'" style="text-decoration:none">';
echo ' <img src="'.$directory.'/'.$cat.'/image.php" style="display:block" />'.str_replace('_', ' ', $cat);
echo ' </a>';
echo '</td>';
if($row == 2)
{
echo '</tr>';
$row = -1;
}
$row++;
}
}
echo '</table>';
}
?>
How can i achieve that?
Staying quick and dirty:
if ($cat != '.'&&$cat != '..' && $cat != 'index.php'){ ... }
But I'll definitely move to some more consistent method like FilesystemIterator or glob().
while($cat = readdir($handle)) {
if (in_array($cat, array('.', '..', 'index.php')))
continue;
// display the file here
}
Hello i have an Undefined Offset 0 . inside this code.
$q = mysql_query("SELECT * FROM category");
if (false === $q) {
echo mysql_error();
}
while ($r = mysql_fetch_row($q)) {
$names[$r[0]] = $r[2];
$children[$r[0]][] = $r[1];
}
function render_select($root=0, $level=-1) {
global $names, $children;
if ($root != 0)
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
render_select($child, $level+1);
}
echo '<select>';
render_select();
echo '</select>';
The exact line where the error is :
foreach ($children[$root] as $child)
render_select($child, $level+1);
This is for a selectbox with a tree format, i found this code in this question
More efficient hierarchy system
There is some ambiguity in your code here:
if ($root != 0)
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
render_select($child, $level+1);
If you are attempting to execute these three lines only if $root != 0, you will need to add curly braces like this:
if ($root != 0)
{
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
{
render_select($child, $level+1);
}
}
Otherwise, anytime render_select is called without a parameter (or with a first parameter value of '0') you will attempt to access the element of $children at array key '0'. As your error indicates, $children does not contain a value at that key.