Welcome Guest, Not a member yet? Register   Sign In
Simpler way to remove link [uri->segment()]
#1

[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
#2

[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>";
        }
    }
}
#3

[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




Theme © iAndrew 2016 - Forum software by © MyBB