如何根据php条件显示不同的ccs样式

时间:2016-12-22 02:25:46

标签: php css

基本上,我为用户提供了一个upvote或downvote按钮。当用户单击upvote或downvote时,相应的按钮会根据用户输入而改变。

我的问题是,当用户刷新页面时,如何显示用户上调或下调的帖子?

这是我的代码:

<?php
if(user_upvoted){
?>
    <style>
    .button<?php echo $button_id;?>
    {//css for upvote}
    </style>
<?php
}
else if(user_downvote){
?>
    <style>
    .button<?php echo $button_id;?>{//ccs for downvote}
    </style>
<?php
}
?>

这是我的按钮&#39;代码:

while($row=$query -> fetch(PDO::FETCH_ASSOC))
$nid = $row['nid'];
{
    <button class="button<?php echo $nid;?>" id="<?php echo $nid ; ?>" name="up">
}

这样可行,但是这段代码看起来很脏,如果你理解了我的意思,感觉不对。 PS:这也是一个php while循环,这就是为什么这是我试过的方法。 什么是更清洁的方法

2 个答案:

答案 0 :(得分:1)

您可以按如下方式编码:

+++ running this Java
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
+++ trying to run SikuliX
+++ using: -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dsikuli.FromCommandLine -jar C:\Users\GGARCIA\Desktop\Sikulix\sikulix.jar -d 3
[debug] RunTimeINIT: loadOptions: check: C:\Users\GGARCIA\Desktop\Sikulix
[debug] RunTimeINIT: loadOptions: check: C:\Users\GGARCIA
[debug] RunTimeINIT: loadOptions: check: C:\Users\GGARCIA\AppData\Roaming\Sikulix\SikulixStore
[debug] RunTimeINIT: global init: entering as: IDE
[debug] RunTimeINIT: ScreenDevice 0 has (0,0) --- will be primary Screen(0)
[debug] RunTimeINIT: Monitor 0: (0, 0) 1920 x 1080
[debug] RunTimeINIT: runs as sikulix.jar in: C:\Users\GGARCIA\Desktop\Sikulix
[debug] RunTimeINIT: exists libs folder at: C:\Users\GGARCIA\AppData\Roaming\Sikulix\SikulixLibs_201612100100
[debug] RunTimeINIT: addToWindowsSystemPath: added to systempath:
C:\Users\GGARCIA\AppData\Roaming\Sikulix\SikulixLibs_201612100100
[debug] RunTimeINIT: checkJavaUsrPath: added to ClassLoader.usrPaths
***** show environment for IDE (build 201612100100)
user.home: C:\Users\GGARCIA
user.dir (work dir): C:\Users\GGARCIA\Desktop\Sikulix
user.name: GGARCIA
java.io.tmpdir: C:\Users\GGARCIA\AppData\Local\Temp
running 64Bit on Windows (10.0) from a jar
java 8-64 version 1.8.0_111-b14 vm 25.111-b14 class 52.0 arch amd64
app data folder: C:\Users\GGARCIA\AppData\Roaming\Sikulix
libs folder: C:\Users\GGARCIA\AppData\Roaming\Sikulix\SikulixLibs_201612100100
executing jar: C:\Users\GGARCIA\Desktop\Sikulix\sikulix.jar
*** classpath dump sikulix
  0: /C:/Users/GGARCIA/Desktop/Sikulix/sikulix.jar
*** classpath dump end
***** show environment end
[debug] RunTimeIDE: global init: leaving
[debug] RunTimeIDE: initIDEbefore: entering
[debug] RunTimeIDE: initIDEbefore: leaving
[debug] RunTimeIDE: initAPI: entering
[debug] RunTimeIDE: resourceList: enter
[debug] RunTimeIDE: resourceLocation: (class org.sikuli.ide.SikuliIDE) /Lib/sikuli
[debug] RunTimeIDE: resourceList: having jar: jar:file:/C:/Users/GGARCIA/Desktop/Sikulix/sikulix.jar!/Lib/sikuli
[debug] RunTimeIDE: files exported: 7 from: Lib/sikuli to:
 C:\Users\GGARCIA\AppData\Roaming\Sikulix\Lib\sikuli
[debug] RunTimeIDE: initAPI: leaving
[debug] init user preferences
[debug] IDE: running with Locale: pt_BR
[debug] --- Sikuli parameters ---
[debug] 1: -d
[debug] 2: 3
[info] HotkeyManager: add Capture Hotkey: CTRL+SHIFT 2 (50, 3)
[debug] HotkeyManager: add Capture Hotkey: CTRL+SHIFT 2 (50, 3)
[error] WindowsHotkeyManager: JIntellitype problem: Could not load JIntellitype.dll from local file system or from inside JAR
[debug] RunTimeIDE: final cleanup
[debug] FileManager: deleteFileOrFolder:
C:\Users\GGARCIA\AppData\Local\Temp\Sikulix_1659037070

然后单独编写CSS规则。

<?php
while($row=$query -> fetch(PDO::FETCH_ASSOC))
{
    $nid = $row['nid'];
    // Write your logic here for $user_upvoted and $user_downvote variable

    $btnExtraClass = '';
    if($user_upvoted){
    $btnExtraClass = 'button_upvote';
    } 
    if($user_downvote){
    $btnExtraClass = 'button_downvote';
    }
?>
<button class="button<?php echo $nid;?> <?php echo $btnExtraClass;?>" id="<?php echo $nid ; ?>" name="up">
<?php
}
?>

答案 1 :(得分:0)

你的问题似乎有点令人困惑。你能先帮我解释一下

1)您是否将用户选择的upvote / downvote存储在数据库中? 你也为什么在这里上课

if(user_upvoted){
?>
<style>
.button<?php echo $button_id;?>
{//css for upvote}
</style>

它应该是一个id。如果你是唯一识别它们,那么去找一个id

相关问题