CodeIgniter Forums
Simpler way to remove link [uri->segment()] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Simpler way to remove link [uri->segment()] (/showthread.php?tid=38275)



Simpler way to remove link [uri->segment()] - El Forum - 02-03-2011

[eluser]carvingCode[/eluser]
I want to remove the link on the current page. The code blow allows me to do that, but seems a bit excessive, especially as I build out the rest of the options (approx 20).

Is there a simpler way to do this?

Code:
if ($this->uri->segment(2) == 'add_section') {
  echo "<li>Add section</li>";
} else {
  echo "<li><a >Add section</a></li>";
}
if ($this->uri->segment(2) == 'add_indicator') {
  echo "<li>Add indicator</li>";
} else {
  echo "<li><a >Add indicator</a></li>";
}
.
.
.

TIA

Randy


Simpler way to remove link [uri->segment()] - El Forum - 02-03-2011

[eluser]cideveloper[/eluser]
Using the below code you just need to add to the $data array your uri segments and text and it will generate you list items
Code:
$data = array (
    'add_section' => 'Add section',
    'add_indicator' => 'Add indicator',
    'add_garbage' => 'Add garbage'
);

$this->show_menu($data, $this->uri->segment(2));

function show_menu($data_array, $uri_segment) {
    foreach ($data_array as $key => $val){
        if ($uri_segment == $key) {
            echo "<li>$val</li>";
        } else {
            echo "<li>" . anchor($key,$val) . "</li>";
        }
    }
}



Simpler way to remove link [uri->segment()] - El Forum - 02-04-2011

[eluser]carvingCode[/eluser]
Thanks. Indeed a simpler way.

I made a slight modification to handle a 'controller/function' type of menu call.

Code:
function show_menu($data_array, $first_uri_segment, $second_uri_segment) {

        foreach ($data_array as $key => $val) {
            if ($second_uri_segment == $key) {
                echo "<li>" . $val . "</li>";
            } else {
                echo "<li>" . anchor($first_uri_segment . "/" . $key, $val) . "</li>";
            }
        }
    }

$this->cc_functions->show_menu($data, $this->uri->segment(1), $this->uri->segment(2));

I placed the function in a custom library in my project, hence the 'cc_functions' in the function call statement.

Thanks!

Randy