Welcome Guest, Not a member yet? Register   Sign In
Passing anchor attribute or title as variable to a controllers display function
#1

[eluser]debow[/eluser]
I'm creating a work related content site for personal use and need some help to clean up repeated code in my controllers. Right now I have 4 display functions that do exactly the same thing but just display info based on OS. I think I should be able to get this into 1 function.


In my navigation div I have links you can choose from.

Code:
<li>&lt;?php echo anchor('filesystems/display', 'Filesystems', 'title="View ALL OSs filesystem commands"');?&gt;
<ul class='children'>
<li>&lt;?php echo anchor('filesystems/aix_display', 'AIX', 'title="View only AIX filesystem commands"');?&gt;
</li>
<li>&lt;?php echo anchor('filesystems/hpux_display', 'HPUX', 'title="View only HPUX filesystem commands"');?&gt;
</li>
<li>&lt;?php echo anchor('filesystems/linux_display', 'LINUX', 'title="View only LINUX filesystem commands"');?&gt;
</li>
</ul>

In my controller(filesystems.php) I have a function called display that will query the db for all OS filesystem commands. I also have this function repeated as (aix_display), (hpux_display) and (linux_display). They only query the db for the specific OS. I feel there is a way to clean that filesystems.php file up and depending on the LINK I choose pass the OS into the display function as a variable and from there I can create a if statement of some kind and determine what gets displayed in the view based of the link. I can get the uri-segement in from the link but that just calls the function and since display is its own function I cant use that without have 4 diff segment(2) calling 4 diff functions.

I want my links to look something link this passing the OS type to the one display function. Then in the function I'll determine what to query based off the OS. If no OS is chosen then display them all.

Code:
<li>&lt;?php echo anchor('filesystems/display', 'Filesystems', 'title="View ALL OSs filesystem commands"');?&gt;
<ul class='children'>
<li>&lt;?php echo anchor('filesystems/display', 'AIX', 'title="View only AIX filesystem commands"');?&gt;
</li>
<li>&lt;?php echo anchor('filesystems/display', 'HPUX', 'title="View only HPUX filesystem commands"');?&gt;
</li>
<li>&lt;?php echo anchor('filesystems/display', 'LINUX', 'title="View only LINUX filesystem commands"');?&gt;
</li>
</ul>


This is my display function right now.

Code:
function display($sort_by = 'ostype', $sort_order = 'asc', $offset = 0) {
        
        $id = $this->uri->segment(2); //Stuff that is not working, that I've been playing with
        $id2 = $this->uri->segment(1); //Stuff that is not working, that I've been playing with
        //$id3 = $title; //Stuff that is not working, that I've been playing with
        
        $data['item'] = $id; //Stuff that is not working, that I've been playing with
        $data['item2'] =$id2; //Stuff that is not working, that I've been playing with
        $data['item3'] =$id2; //Stuff that is not working, that I've been playing with
        
        $limit = 10;
        $data['fields'] = array(
        'command' => 'Command',
        'description' => 'Description',
        'comments' => 'Comments',
          'ostype' => 'OS',
        );

        //This below if section is just what I've been playing with but isn't working
        if($data['item'] == "display"){
            $results = $this->Fs_model->search($limit, $offset, $sort_by, $sort_order);
            
        }
        elseif($data['item'] == "linux"){
            $results = $this->Fs_model->search_linux($limit, $offset, $sort_by, $sort_order);
                
        }//Above is what I'm trying to get working or something like it
        
        
        $data['commands'] = $results['rows'];
        $data['num_results'] = $results['num_rows'];
        $data['num_pages'] = $data['num_results']/$limit;        

        // pagination
        $this->load->library('pagination');
        $config = array();
        $config['base_url'] = site_url("filesystems/display/$sort_by/$sort_order");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;

        $data['main_content'] = 'filesystems/filesystem_index';
        $this->load->view('includes/header', $this->data);
        $this->load->view('includes/template', $data);

    }


Thanks for any help or suggestions.
#2

[eluser]debow[/eluser]
I've been able to work out a solution. I'm not sure if its the best solution but it works.

I've changed my links to the below code in my home.php/navigation div.

Code:
<li class="cat-item cat-item-14">&lt;?php echo anchor('filesystems/display?id=1', 'Filesystems', 'title="View All OS filesystem commands"');?&gt;
<ul class='children'>
<li class="cat-item cat-item-16">&lt;?php echo anchor('filesystems/display?id=2', 'AIX', 'title="View only AIX filesystem commands"');?&gt;
</li>
<li class="cat-item cat-item-17">&lt;?php echo anchor('filesystems/display?id=3', 'HPUX', 'title="View only HPUX filesystem commands"');?&gt;
</li>
<li class="cat-item cat-item-18">&lt;?php echo anchor('filesystems/display?id=4', 'LINUX', 'title="View only LINUX filesystem commands"');?&gt;
</li>
</ul>
</li>


Then in my controller.

I'm using one function.

Code:
function display($sort_by = 'ostype', $sort_order = 'asc', $offset = 0, $id='') {
        
    $id3 = $_GET['id'];    /Added this
    $data['item3'] =$id3;  /Added this
        
        $limit = 10;
        $data['fields'] = array(
        //'command' => 'Command',
        //'description' => 'Description',
        //'comments' => 'Comments',
          'ostype' => 'OS',
        );//The above is really to allow certain colums to be sortable, can get them with out the array but unable to sort them

if($data['item3'] == "1"){
   $results = $this->Fs_model->search($limit, $offset, $sort_by, $sort_order);
   $data['htitle'] = "ALL OS FileSystem Commands";
        }
   elseif($data['item3'] == "2"){
     $results = $this->Fs_model->search_aix($limit, $offset, $sort_by, $sort_order);    
     $data['htitle'] = "AIX Only FileSystem Commands";    
        }
   elseif($data['item3'] == "3"){
      $results = $this->Fs_model->search_hpux($limit, $offset, $sort_by, $sort_order);
      $data['htitle'] = "HPUX Only FileSystem Commands";        
        }
   elseif($data['item3'] == "4"){
     $results = $this->Fs_model->search_linux($limit, $offset, $sort_by, $sort_order);    
     $data['htitle'] = "Linux Only FileSystem Commands";
        }

.......rest of code

Again not sure if this is the best way but it worked for what I was trying to get done.




Theme © iAndrew 2016 - Forum software by © MyBB