php - 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();
}
Answer
Solution:
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
Answer
Solution:
To get the segment fromURL
you should useCI
's built-in libraryURI 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. :)
Answer
Solution:
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);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.