Codeigniter:根据URI段循环加载不同的视图

发布于 2024-09-29 11:44:15 字数 772 浏览 3 评论 0原文

我有一个从模型中加载的建筑物列表。这些建筑物在我的视图中循环显示,如下所示:

控制器

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

视图

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

每个构建都有一些可能的“操作”,例如“信息”、“价格”、“颜色” ”。默认状态显示“信息”。

我希望能够加载特定建筑物的特定操作。

例如,我有一家工厂和一家药店,默认情况下都显示“信息”选项卡。 如果我单击药店的“价格”选项卡,如何显示工厂的“信息”选项卡和药店的“价格”选项卡?

我的网址如下所示:

默认网址

场地/建筑物

显示药店的价格

网站/建筑物/价格/药店

也许我应该寻找部分视图解决方案,但我真的需要建议或解决方案;)

提前非常感谢您(我也为我的英语不好而道歉)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-10-06 11:44:15

如果不了解更多有关设置的信息,很难说,但您会希望在 Price() 函数中使用某种条件,

是从数据库获取的信息/价格操作吗?

如果是这样,

<?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; ?> 

但如前所述,在不了解有关系统的更多信息的情况下,很难给出一个像样的答案。

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.

硪扪都還晓 2024-10-06 11:44:15

像这样:

$this->Base_Model->getUserBuildingFromWebname($webname);

然后在您的建筑物控制器中您可以添加以下内容:

public function index( $webname = null )
{
  if( !is_null($webname) )
    $buildingId = $this->Base_Model->getUserBuildingFromWebname($webname);
  else
    $buildingId = null;

  $data["showBuildingDetails"] = $buldingId;

  //the rest of your index() function
}

public function price ( $webname )
{
  return $this->index($webname);
}

Something like this then:

$this->Base_Model->getUserBuildingFromWebname($webname);

Then in your Buildings controller you could add this:

public function index( $webname = null )
{
  if( !is_null($webname) )
    $buildingId = $this->Base_Model->getUserBuildingFromWebname($webname);
  else
    $buildingId = null;

  $data["showBuildingDetails"] = $buldingId;

  //the rest of your index() function
}

public function price ( $webname )
{
  return $this->index($webname);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文