【问题标题】:If condiction in HTML with URL如果在带有 URL 的 HTML 中条件
【发布时间】:2020-09-02 20:43:56
【问题描述】:

我正在尝试在我的 HTML 文件中的 codeigniter 中做一个 if 条件来检查 URL,如果它包含 URL 的一部分,它会做一件事,如果它不做其他事情

例如:

我的网址是

localhost/index.php/cart/galery1

当我点击一张照片时,它会应用带有此 URL 的过滤器

localhost/index.php/cart/galery1/2

问题是,当我在过滤器后点击照片时,它会变成类似

localhost/index.php/cart/galery1/2/2

有一种方法可以执行 if 条件检查我的控制器或我的 html 中的 URL 吗?

我的代码现在是这样的

HTML

<div id="top-industries" class="gallery">
        <div class="container">
          <h3 class="tittle">As Nossas Casas</h3>
                <div class="gallery-bottom">

                {foreach $products as $product}
                     <div class="view view-ninth">
                       <a href="galery1/{$product.cat_id}" class="b-link-stripe b-animate-go  swipebox"  title="Image Title"><img src="{$product.image}" alt="" class="img-responsive" width="330" height="219" target="_blank">
                        <div class="mask mask-1"></div>
                        <div class="mask mask-2"></div>
                        <div class="content">
                            <h2>CASA 1</h2>
                            <!--<p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>-->

                        </div></a>
                    </div>
                {/foreach}
                    <div class="clearfix"> </div>
                </div>
        </div>
    </div>

控制器

            if($cat_id){
            $data['catID'] = $cat_id;
            $data['products'] = $this->Cart_model->get_casa($cat_id);
        }
        else{
            $data['products'] = $this->Cart_model->get_products();
        }

【问题讨论】:

    标签: php html codeigniter if-statement url


    【解决方案1】:

    你必须设置 base_url();这里 应用程序\config\config.php

    $config['base_url'] = 'www.yoursite url.com';

    然后在这样的链接中使用这个 base_url()

    <a href="<?php echo base_url()?>galery1/{$product.cat_id}" class="b-link-stripe b-animate-go  swipebox"  title="Image Title"><img src="{$product.image}" alt="" class="img-responsive" width="330" height="219" target="_blank">
    

    我想它会解决你的问题

    【讨论】:

      【解决方案2】:

      要从URL 获取片段,您应该使用CI 的内置库URI Class

      $this->uri->segment(n);
      
      // your url - localhost/index.php/cart/galery1/2/3
      
      $this->uri->segment(1); // returns conrtoller ie cart
      $this->uri->segment(2); // returns function ie gallery1
      $this->uri->segment(3); // returns 1st segment ie 2
      $this->uri->segment(4); // returns 2nd segment ie 3
      
      // you can also return default value when a uri segment is not available
      $this->uri->segment(5, 0); // returns 0 because there is no 5th segment. Comes handy in condition -- will definitely help you here
      
      // you can also use
      uri_string();  // returns complete uri as a string ie cart/galery1/2/3
      

      更多信息请阅读here

      希望它可以帮助你。 :)

      【讨论】:

        【解决方案3】:

        创建一个新的 .tpl igual 作为另一个只更改您想要更改的内容,然后将控制器更改为

        if($cat_id){
                    $data['catID'] = $cat_id;
                    $data['products'] = $this->Cart_model->get_casa($cat_id);
                    $this->smarty->view('fotos_galeria.tpl', $data);
        
                }
                else{
                    $data['products'] = $this->Cart_model->get_products();
                    $this->smarty->view('galeria.tpl', $data);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-08
          • 2021-09-08
          • 1970-01-01
          • 2015-07-09
          • 2018-07-15
          • 2021-02-28
          相关资源
          最近更新 更多