Welcome Guest, Not a member yet? Register   Sign In
get names of directories and place in a dropdown list
#1

[eluser]Unknown[/eluser]
I need to get all the folder names in a directory and place it in a dropdown list.

Can you suggest a method on how to get the names because I don't have the slightest idea to do this in PHP/CI.

Thanks in advance.
#2

[eluser]Unknown[/eluser]
this was only simple :p

scandir
#3

[eluser]eoinmcg[/eluser]
kathleenr,


I do it like this:

Code:
$this->load->helper('file');
            $path = APPPATH.'/views/snippets/';
            $snippets = get_filenames($path);
            $data['html'] .= cms_multi_select('Snippets', 'snippets', $snippets, $row->snippets);

that loads all files from the snippets folder, which is inside the views folder. Then the values are passed to a helper function (which of course you'll need to create then load)

Code:
function cms_multi_select($label, $name, $options, $val='')
    {

        $val = explode(',', $val);

        $form = "\n\t".'<div>'.$label.'';
        foreach($options as $option)
        {

            $option = str_replace('.php', '', $option);
            $selected = '';

            if(is_array($val))
            {
                if(in_array($option, $val))
                    $selected = ' checked';
            }

            $form .= '<div class="row">&lt;input type="checkbox" name="'.$name.'[]" value="'.$option.'"'.$selected.'&gt;'.ucfirst($option).'</div>';
        }

        $form .= "\n\t".'</div>';

        return $form;

    }

and that should do it
#4

[eluser]NetStranger[/eluser]
i have the question!
if i want to get name of folder in my directory , but folder name is "папка" ? Name of the folder on cyrillic ? this function get name and print only "?????"! plz help!
#5

[eluser]eoinmcg[/eluser]
NetStranger,

What operating system are using?

I just tried what you described with a cyrillic folder name and it worked fine for me on Linux.

Not sure if your problem could be related to the version of windows you're using... if so you may just have to rename the folder to have only latin characters
#6

[eluser]NetStranger[/eluser]
i modernizate default directory_helper :
Code:
if ( ! function_exists('directory_map'))
{
    function directory_map($source_dir, $top_level_only = FALSE)
    {
      
        if ($fp = @opendir($source_dir))
        {
            $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;        
            $filedata = array();
            
            while (FALSE !== ($file = readdir($fp)))
            {
                if (strncmp($file, '.', 1) == 0)
                {
                    continue;
                }
                
                if ($top_level_only == FALSE && @is_dir($source_dir.$file))
                {
                    $temp_array = array();
                
                    $temp_array = directory_map($source_dir.$file.DIRECTORY_SEPARATOR);
                $file = iconv("windows-1251", "UTF-8", $file);  // HERE !!!
                    $filedata[$file] = $temp_array;
                }
                else
                {
                    $filedata[] = $file;
                }
            }
            
            closedir($fp);
            return $filedata;
        }
    }
}

now it takes me right names of the folders , BUT if i want to get list of files in this cyrillic folder , i get an error! i think my problem is $source_dir ... but i dont know how to solve the problem (((
#7

[eluser]eoinmcg[/eluser]
Hmmmm....

I don't have a windows box handy to try it out.

What is the error message you get?
#8

[eluser]NetStranger[/eluser]
this error:
Code:
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/photo.php

Line Number: 38

this it controller Photo

Code:
<........>
35.
36. $data = directory_map('./img/photo/папка');
37.
38.   foreach ($data as $val => $img)
39.                 {
<.......>

when folder name is latin - it's ok
#9

[eluser]Evil Wizard[/eluser]
The function directory_map() returns NULL if the directory does not exist, otherwise it returns an array of the directory entries. try
Code:
<........>
35.
36. $data = directory_map('./img/photo/папка');
37.  if(!is_null($data)) {
38.      foreach ($data as $val => $img) {
39.      
<.......>
#10

[eluser]NetStranger[/eluser]
but my directory exists! )) i need to scan it)




Theme © iAndrew 2016 - Forum software by © MyBB