简体   繁体   English

如何在PHP标签内添加CSS属性

[英]How to add css properties inside the PHP tag

I need one help.i need to restrict some css display property with PHP condition.I am explaining my code below. 我需要一个帮助。我需要用PHP条件限制一些CSS显示属性。我在下面解释我的代码。

<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. 这里最初是我的div的css属性display:none但是我需要使用如下所示的php条件来block它。

<?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. 我需要将上述条件放入样式属性中。请帮助我如何正确进行设置。

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 : 如果要混合使用php和html,可以在html内使用php标签:

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

or short tags if enabled on your server 或短标签(如果在服务器上启用)

<p><?= $something ?></p>

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 样式属性中添加您的phpecho

Hope it helps ! 希望能帮助到你 !

You just need to set a variable to the string you want to use as the CSS property. 您只需要为要用作CSS属性的字符串设置一个变量即可。

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. 它不是最整洁的解决方案,您最好使用HTML类并分离CSS,但这应该可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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