donglun1918
2016-05-23 13:37
浏览 147
已采纳

如何在PHP标记内添加css属性

I need one help.i need to restrict some css display property with PHP condition.I am explaining my code below.

<div style="width:24%; float:left; padding:10px;display:none;" id="compid">Select Company :


</div>

Here initially my div's css property display:none is there.But i need to block this using some php condition like below.

<?php if($getcustomerobj->companypro == 1 and $_REQUEST['edit']!=""){display:block}else{display:none}   ?>

I need to put the above condition inside the style properties.Please help me how to do it proper way.

图片转代码服务由CSDN问答提供 功能建议

我需要一个help.i需要用PHP条件限制一些css显示属性。我在下面解释我的代码。

 &lt; div style =“width:24%; float:left; padding:10px; display:none;”  id =“compid”&gt;选择公司:
 
 
&lt; / div&gt; 
   
 
 

这里最初我的div的css属性 display:none < / code>在那里。但我需要阻止这个使用下面的php条件。

 &lt;?php if($ getcustomerobj-&gt  ; companypro == 1和$ _REQUEST ['edit']!=“”){display:block} else {display:none}?&gt; 
   
 
 

I 需要将上述条件放在样式属性中。请帮助我如何正确地做到这一点。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doumao9363 2016-05-23 13:40
    已采纳

    you can instanciate your display value before outputing :

    <?php
    $display = ($getcustomerobj->companypro == 1 && $_REQUEST['edit']!="") ? 'block' : 'none';
    ?>
    
    <div style="width:24%; float:left; padding:10px;display:<?= $display ?>;" id="compid">Select Company :
    </div>
    

    If you want to mix php and html, you can use php tags inside html :

    <p><?php echo $something ?></p>
    

    or short tags if enabled on your server

    <p><?= $something ?></p>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyuan9892 2016-05-23 13:41

    Do something like

    <div style="width:24%; float:left; padding:10px;<?php if($getcustomerobj->companypro == 1 && $_REQUEST['edit']!=""){echo 'display:block;';}else{echo 'display:none;';}?>" id="compid">Select Company :</div>
    

    add your php and echo in the style attribute

    Hope it helps !

    评论
    解决 无用
    打赏 举报
  • dongli7870 2016-05-23 13:41

    You just need to set a variable to the string you want to use as the CSS property.

    Just for brevity, I tend to use ternaries for this sort of thing:

    <?php $display = ($getcustomerobj->companypro == 1 and $_REQUEST['edit']!="") ? 'display:block;' : 'display:none;'; ?>
    
    <div style="width:24%; float:left; padding:10px;<?php echo $display;?>" id="compid">Select Company :
    
    </div>
    

    Its not the tidiest solution, you would be better of using HTML classes and separating your CSS, but this should get the job done.

    评论
    解决 无用
    打赏 举报