douwei8672 2018-03-03 10:23
浏览 115
已采纳

通过从php中读取数据库中的值来更改css

I am trying to change my css dynamically. My css value should come from a database. I have a goals table in my database named order and it contains id and goal_1.

Here is the code I'm using:

<?php
    $conn = mysqli_connect('localhost','root','','order');
    $query1 = "SELECT * FROM `goals`";
    $result = mysqli_query($conn, $query1);
    while ($row = mysqli_fetch_array($result)) {
        $width = $row["goal_1"]; `// storing value in a variable from db.`
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>project-1</title>
    <link rel="stylesheet" type="text/css" href="style.php">
</head>
    <body class="container">
        <div>
            <h4>Goal_1</h4>
            <h5><?php echo $width ?></h5> // this value is supposed to came from db.
            <hr align="left" class="goal goal_1">
        </div>
    </body>
</html>

I want to make the width of tag <hr> dynamic. Suppose the value of goal_1 came from db 2. Now the width of my <hr> should become 2px. For this purpose, I am using a style.php file.

// my css file
<?php 
    header('Content-Type: text/css');
?>
.container {
    width: 1170px;
    margin: 0 auto;
}
div{
    float: left;
    width: 330px;
    margin-top: 20px;
}
.goal{
    height:15px;
    background-color:#32CD32;
}
.goal_1{
    width: <?php $width ?>px; `// i am trying to do this to take the value form my db. but its not working`
}
h4{
    margin-bottom: 20px;
}
h5{
    float: left;
    margin-left: 35px;
    margin-right: 20px;
}

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dtvgo28624 2018-03-03 10:26
    关注

    try changing

    .goal_1{
        width: <?php $width ?>px;
    }
    

    to

    .goal_1{
        width: <?php echo $width; ?>px;
    }
    

    and also possibly

    <h5><?php echo $width ?></h5>
    

    to

    <h5><?php echo $width; ?></h5>/* trailing semi-colon */
    

    --

    A possible work-around that does not mean putting the db code inside style.php would be to use a session variable which can be populated in index.php and made available within style.php

    So, for example:

    <?php
        /* index.php */
        session_start();
    
        $_SESSION['style']=(object)array(
            'h1'    =>  (object)array('color'=>'red','font-size'=>'1.25rem'),
            'hr'    =>  (object)array('width'=>'500px'),
        );
    ?>
    <html>
        <head>
            <title>Dynamic styles</title>
            <link rel='stylesheet' href='style.php' />
        </head>
        <body>
            <h1>Dynamic style</h1>
            <hr />
        </body>
    </html>
    
    <?php
        /* style.php */
        session_start();
        if( !empty( $_SESSION['style'] ) ){
    
            $obj=$_SESSION['style'];
    
            $h1=$obj->h1;
            $hr=$obj->hr;
    
    
    
        }
    
    ?>
    h1{
        color:<?php echo $h1->color; ?>;
        font-size:<?php echo $h1->{'font-size'}; ?>
    }
    hr{
        width:<?php echo $hr->width;?>;
    }
    

    etc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题