CodeIgniter Forums

Full Version: Simpler way to remove link [uri->segment()]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

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

El Forum

[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