Search code examples
urlcodeigniterviewsegment

Codeigniter : Loading a different view in a loop according to URI segment


I have a list of buildings that I load from my model. Those buildings are showed in a loop within my view as so :

Controller

public function index() {
 $data['buildings'] = $this->Base_Model->getUserBuildings();
 $this->load->view('game', $data); }

View

<?php foreach($buildings as $b): ?>
    <div class="building">[...]</div>
<?php endforeach; ?> 

Each build has a few "actions" possible such as "info", "price", "color". the default state shows the "info".

I want to be able to load a specific action for a specific building.

For example I have a factory and a drugstore, by default both show the "info" tab. How to, if I click for the "price" tab of the drugstore, show the "info" tab for the factory AND the "price" tab for the drugstore ?

My URLs looks like these :

default one

site/buildings

show price of the drugstore

site/buildings/price/drugstore

Maybe I should look for a partial view solution but I really need an advise or a solution ;)

Thank you very much in advance (I apologize for my bad English too)


Solution

  • without knowing more about the set up its hard to say, but you'll want some kind of conditional in your price() function

    are the info/price actions obtained from the database?

    if so something like

    <?php foreach($buildings as $b): ?>
        <div class="building">
        <?php if($this->uri->segment(3)==$b['building_name']) {
            echo 'Price';
        } else {
            echo 'Info';
        }
        ?>    
        </div>
    <?php endforeach; ?> 
    

    but as stated without knowing more about the system it is hard to give a decent answer.