使用css的if-loops的php语法问题

时间:2013-04-18 14:16:31

标签: php syntax

我不是一个非常好的程序员,我的技术主要包括复制粘贴和编辑,直到它起作用。

我的index.php中有一个joomla 2.5站点的代码,它只在主页上输出一定的CSS样式。

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()): ?>
<style type="text/css">
  #contentonderaan {min-height: 462px;}
</style>
<?php endif; ?>

这样可行,但现在我想对所有不是主页的页面使用'else'语句来提供备用的最小高度css。这应该很容易吗?但是我的语法有问题,因为我无法让它工作。那怎么写呢?谢谢

6 个答案:

答案 0 :(得分:6)

你的语法不正常?

它应该是else:

<style type="text/css">
<?php if ($menu->getActive() == $menu->getDefault()): ?>
    #contentonderaan {min-height: 462px;}
<?php else: ?>
    #contentonderaan {min-height: YOUR_HEIGHTpx;}
<?php endif; ?>
</style>

答案 1 :(得分:1)

<?php
 $app = JFactory::getApplication();
 $menu = $app->getMenu(); ?>
<style type="text/css">
<?php if ($menu->getActive() == $menu->getDefault()): ?>

       #contentonderaan {min-height: 462px;}

<?php else?>

      #contentonderaan {min-height: 100px;} //giving min height 

<?php endif; ?>
</style>

快乐编码:)

答案 2 :(得分:0)

<?php
   $app = JFactory::getApplication();
   $menu = $app->getMenu();
   if ($menu->getActive() == $menu->getDefault()) { 
?>
<style type="text/css">
  #contentonderaan {min-height: 462px;}
</style>
<?php }
else{
 ?>
Some other thing

<?php } ?>

答案 3 :(得分:0)

您可以使用大括号:

<?php
    $app = JFactory::getApplication();
    $menu = $app->getMenu();
?>
<?php if ($menu->getActive() == $menu->getDefault()){ ?>
    <style type="text/css">
        #contentonderaan {min-height: 462px;}
    </style>
<?php }else{ ?>
    <!-- foo -->
<?php } ?>

答案 4 :(得分:0)

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()) { ?>
<style type="text/css">
  #contentonderaan {min-height: 462px;}
</style>
<?php } else { ?>
<style type="text/css">
  #contentonderaan {min-height: 500px;}
</style>
<?php } ?>

或者您也可以使用else:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu(); ?>
<?php if ($menu->getActive() == $menu->getDefault()): ?>
<style type="text/css">
  #contentonderaan {min-height: 462px;}
</style>
<?php else: ?>
<style type="text/css">
  #contentonderaan {min-height: 500px;}
</style>
<?php endif; ?>

答案 5 :(得分:0)

使用简写语法以提高可读性:

<style type="text/css">
<?php if ($menu->getActive() == $menu->getDefault()): ?>
    #contentonderaan {min-height: 462px;}
<?php else: ?>
    #contentonderaan {min-height: 642px;}
<?php endif; ?>
</style>