Ricardo Martins
Ricardo Martins

Reputation: 28

If condiction in HTML with URL

I'm trying to do an if condiction in my HTML file in codeigniter that checks the URL and if it contains part of the URL it does one thing if doesnt it does something else

For example:

My URL is

localhost/index.php/cart/galery1

and when I click a photo it applies a filter with this URL

localhost/index.php/cart/galery1/2

the thing is that when I click in a photo after the filter it goes to something like

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

There's a way I can do an if condiction checking the URL in my controler or my html ?

My code is something like that right now

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>

Controler

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

Upvotes: 0

Views: 68

Answers (3)

Nadeem Ijaz
Nadeem Ijaz

Reputation: 599

you have to set base_url(); here application\config\config.php

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

and then use this base_url() in link like this

<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">

i think it will solve your problem

Upvotes: 0

Ricardo Martins
Ricardo Martins

Reputation: 28

Create a new .tpl igual as the other one only changing what you want to change and then change the controller to

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);
        }

Upvotes: 0

sauhardnc
sauhardnc

Reputation: 1961

To get the segment from URL you should use CI's built-in library 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

For more info read here.

Hope it helps you. :)

Upvotes: 0

Related Questions