简体   繁体   中英

If else condition in a php script

I am trying to modify this search engine script provided by codecanyon. Here I can see a code like

<div class="{$contentClass}">

   <div class="resultarea" style="width:55%;float:left;">
      {$content}
   </div>

</div>

Here I need to set the width as 100% if $contentClass="mainpage" and would like to set the width as 55% for other pages.

I have tried the following code

<div class="{$contentClass}">

<div class="resultarea" <?php if ($contentClass=="mainpage"){?> style="width:100%;float:left;" <?php }else{?>style="width:100%;float:left;"<?php} ?>>
    {$content}
</div>

I think PHP codes is not working here , I can print the value of contentClass with out providing the php echo statement as {$contentClass}. I am not sure about the framework used here , please help me to solve this issue.

Please check my work here

Wouldn't this work?

<div class="resultarea" <?php if ($contentClass=="mainpage"){?> style="width:100%;float:left;" <?php }else{?>style="width:50%;float:left;"<?php} ?>>

If you look at the CSS inside your else block, you'll see that you were setting the width to 100%. Basically, your CSS is the same, regardless of whether $contentClass=="mainpage" or not.

Your IF statement doesn't actually do anything:

<?php if ($contentClass=="mainpage"){?> 
    style="width:100%;float:left;" 
<?php }else{?>
    style="width:100%;float:left;"
<?php} ?>>

Given that $contentClass is a variable, you've got to use "echo" command. Also, since there is an excluding condition (do the following everywhere except the mainpage), it will be reasonable to use $contentClass!="mainpage".

The code will look like

<?php
if ($contentClass!="mainpage") 
  {
   echo 'style="width:55%;float:left;"';
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM