• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] isset not working correct.

#1
Hello all, I am trying to make a <body class="<?php echo $class;?>"> to help with my specialized css 

For example if isset uri->segment(1) and (2) then would show something like this <body class="page-category-121"> which is correct

But if the uri segments not set I need it to be <body class="common-home"> that would mean it on base url page the home page for some reason when on home page it shows just <body class="-">

When on main url / base url how can I make it show  <body class="common-home">


PHP Code:
$page_id $this->input->get('page_id');
$path $this->input->get('path');
$route $this->uri->segment(1).'/'.$this->uri->segment(2);

if (isset(
$route)) {

if (isset(
$page_id)) {
$class '-' $page_id;
} elseif (isset(
$path)) {
$class '-' $path;
} else {
$class '';
}

$data['class'] = str_replace('/''-'$route).$class;
        
} else {

$data['class'] = 'common-home';


There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
The problem is that the variables $page_id and $path are set so will always evaluate to TRUE. Use empty() instead

Code:
$page_id = $this->input->get('page_id');
$path = $this->input->get('path');
$route = $this->uri->segment(1).'/'.$this->uri->segment(2);

if ($route != '/')
{
   if (! empty($page_id))
   {
       $class = '-' . $page_id;
   } elseif (! empty($path))
   {
       $class = '-' . $path;
   } else
  {
       $class = '';
   }
}
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#3
$this->uri->segment(x) always returns a value, and so will $this->input->get(x), so they will always pass "isset". If you are using CI2.x, they return FALSE if the segment or get parameter didn't exist. So that's probably what you want to check for (!==FALSE) instead of isset().
Reply

#4
(04-22-2015, 07:29 AM)kilishan Wrote: The problem is that the variables $page_id and $path are set so will always evaluate to TRUE. Use empty() instead


Code:
$page_id = $this->input->get('page_id');
$path = $this->input->get('path');
$route = $this->uri->segment(1).'/'.$this->uri->segment(2);

if ($route != '/')
{
   if (! empty($page_id))
   {
       $class = '-' . $page_id;
   } elseif (! empty($path))
   {
       $class = '-' . $path;
   } else
  {
       $class = '';
   }
}

Thanks every one works great now.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.