• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reusability Problem

#1
[eluser]Lazos[/eluser]
I have these 2 functions, they look the same just what is repeated is different. How can I make this function to be used just once but only change what it repeated inside the while itteartor?

Image Manager
Code:
function getDir($path, $original){
        $directory = '';
        $len = strlen($original);
        $back = substr($original, 0, $len - 1);
        $len = strripos($back, '/');
        $back = substr($back, 0, $len + 1);
        if ($back == '') {$back = '/';}
        $ignore = array('cgi-bin', '..');
        $dh = @opendir($path);
        $i = 0;
        while (false !== ($file = readdir($dh))){
            if(!in_array($file, $ignore)){  
                if(is_dir($path.'/'.$file)){
                    $i++;
                    if ($i==1) { $directory .= "<tr>";}
                    if ($file == '.' && ($path != $this->imagepath)) {
                    $directory .= '<td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$back.'" title="Go Back">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/folder.gif', 'Go Back', '80','80','').'</a>';
                    $directory .= '</td></tr><tr><td class="edit">Go Back</td></tr></table></td>';    
                    }
                    if ($file != '.') {
                    $directory .= '<td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$original.$file.'/" title="'.$file.'">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/folder.gif', ''.$file.'', '80','80','').'</a>';
                    $directory .= '</td></tr><tr><td class="edit">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$original.'&amp;deld='.$original.$file.'" title="Trash">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/edit_trash.gif', 'Trash', '','','').'</a>';
                    $directory .= $file.'</td></tr></table></td>';
                    }
                    if ($i==8) { $directory .= "</tr>"; $i = 0;}
                }        
            }
        }
        closedir( $dh );
        return $directory;
    }

File Manager
Code:
function getDir($path, $original){
        $directory = '';
        $len = strlen($original);
        $back = substr($original, 0, $len - 1);
        $len = strripos($back, '/');
        $back = substr($back, 0, $len + 1);
        if ($back == '') {$back = '/';}
        $ignore = array('cgi-bin', '.');
        $dh = opendir($path);
        $i = 0;
        $j = 0;
        $k = 0;
        while (false !== ($file = readdir($dh))){
            if ($i % 2 == 0) { $row = 'row1';} else { $row = 'row2'; }
            if(!in_array($file, $ignore)){  
                if(is_dir($path.$file)){
                    if ($file == '..' && $path != $this->root) {
                    $directory['repeat'][$i]['U_DIR'] = base_url().'index.php/admin/filemanager?dir='.$back;    
                    $directory['repeat'][$i]['I_DIRECTORY'] = $this->getDirIcon('root');
                    $directory['repeat'][$i]['V_FILENAME'] = $file;
                    $directory['repeat'][$i]['U_NAME'] = base_url().'index.php/admin/filemanager?dir='.$back;
                    $directory['repeat'][$i]['V_FILEOWNER'] = '&nbsp;';
                    $directory['repeat'][$i]['V_PERMS'] = '&nbsp;';
                    $directory['repeat'][$i]['V_SIZE'] = '&nbsp;';
                    $directory['repeat'][$i]['V_DATE'] = '&nbsp;';
                /*    $directory['repeat'][$i]['I_DOWNLOAD'] = '&nbsp;';
                    $directory['repeat'][$i]['I_UPLOAD'] = '&nbsp;';
                    $directory['repeat'][$i]['I_VIEW'] = '&nbsp;';
                    $directory['repeat'][$i]['I_EDIT'] = '&nbsp;';
                    $directory['repeat'][$i]['I_VIEWONLY'] = '&nbsp;';*/
                    $directory['repeat'][$i]['I_DELETE'] = '&nbsp;';
                    $directory['repeat'][$i]['I_CHANGEPERMS'] = '&nbsp;';
                    $directory['repeat'][$i]['I_RENAME'] = '&nbsp;';
                    $directory['repeat'][$i]['I_LOCK'] = '&nbsp;';
                    $directory['repeat'][$i]['V_ROW'] = $row;
                    $directory['repeat'][$i]['V_CHECKBOX'] = '&nbsp;';
                    $i++;
                    
                    }
            }
        closedir( $dh );
}

#2
[eluser]davidbehler[/eluser]
You could add a third parameter to the function that determines what should be done inside while loop.

Code:
function getDir($path, $original, $type){
        $directory = '';
        $len = strlen($original);
        $back = substr($original, 0, $len - 1);
        $len = strripos($back, '/');
        $back = substr($back, 0, $len + 1);
        if ($back == '') {$back = '/';}
        $ignore = array('cgi-bin', '..');
        $dh = @opendir($path);
        $i = 0;
        while (false !== ($file = readdir($dh))){
            if(!in_array($file, $ignore)){  
                if(is_dir($path.'/'.$file)){
                  if($type == 'file') {
                    $i++;
                    if ($i==1) { $directory .= "<tr>";}
                    if ($file == '.' && ($path != $this->imagepath)) {
                    $directory .= '<td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$back.'" title="Go Back">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/folder.gif', 'Go Back', '80','80','').'</a>';
                    $directory .= '</td></tr><tr><td class="edit">Go Back</td></tr></table></td>';    
                }
                    if ($file != '.') {
                    $directory .= '<td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$original.$file.'/" title="'.$file.'">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/folder.gif', ''.$file.'', '80','80','').'</a>';
                    $directory .= '</td></tr><tr><td class="edit">';
                    $directory .= '<a href="'.base_url().'index.php/admin/imagemanager?dir='.$original.'&amp;deld='.$original.$file.'" title="Trash">';
                    $directory .= $this->CI->admin_theme->displayImage('images/admin/imagemanager/edit_trash.gif', 'Trash', '','','').'</a>';
                    $directory .= $file.'</td></tr></table></td>';
                    }
                    if ($i==8) { $directory .= "</tr>"; $i = 0;}
              }
              else
              {
                   if ($file == '..' && $path != $this->root) {
                    $directory['repeat'][$i]['U_DIR'] = base_url().'index.php/admin/filemanager?dir='.$back;    
                    $directory['repeat'][$i]['I_DIRECTORY'] = $this->getDirIcon('root');
                    $directory['repeat'][$i]['V_FILENAME'] = $file;
                    $directory['repeat'][$i]['U_NAME'] = base_url().'index.php/admin/filemanager?dir='.$back;
                    $directory['repeat'][$i]['V_FILEOWNER'] = '&nbsp;';
                    $directory['repeat'][$i]['V_PERMS'] = '&nbsp;';
                    $directory['repeat'][$i]['V_SIZE'] = '&nbsp;';
                    $directory['repeat'][$i]['V_DATE'] = '&nbsp;';
                /*    $directory['repeat'][$i]['I_DOWNLOAD'] = '&nbsp;';
                    $directory['repeat'][$i]['I_UPLOAD'] = '&nbsp;';
                    $directory['repeat'][$i]['I_VIEW'] = '&nbsp;';
                    $directory['repeat'][$i]['I_EDIT'] = '&nbsp;';
                    $directory['repeat'][$i]['I_VIEWONLY'] = '&nbsp;';*/
                    $directory['repeat'][$i]['I_DELETE'] = '&nbsp;';
                    $directory['repeat'][$i]['I_CHANGEPERMS'] = '&nbsp;';
                    $directory['repeat'][$i]['I_RENAME'] = '&nbsp;';
                    $directory['repeat'][$i]['I_LOCK'] = '&nbsp;';
                    $directory['repeat'][$i]['V_ROW'] = $row;
                    $directory['repeat'][$i]['V_CHECKBOX'] = '&nbsp;';
                    $i++;
                }
            }
                    
                    }              
                }        
            }
        }
        closedir( $dh );
        return $directory;
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.