2017-02-13 98 views
0

我有这个php文件,我想从数据库中检索数据并以表格格式显示。如何在php代码嵌入时创建html元素的样式?

<html> 

<head> 

    <title>Admin</title> 
    <link rel="stylesheet" type="text/css" href="adminPanel.css"/> 

</head> 

<body> 

    <div class="header"> 

    <h1>Admin</h1> 

    </div> 

    <div class="center"> 

    <?php 

    include "db_connection.php"; 

    $sql = "SELECT * FROM appointments;"; 

    $result = mysqli_query($db,$sql); 

    If(mysqli_query($db,$sql) == TRUE){ 

    ?> 

     <table> 

     <th>Name</th> 
     <th>Address</th> 
     <th>Phone</th> 
     <th>License</th> 
     <th>Engine</th> 
     <th>Appointment Date</th> 
     <th>Preferred Mechanic</th>  

    <?php 

    while($row = mysqli_fetch_assoc($result)){ 

    ?> 

     <td><?php echo $row['Name'];?></td></br> 
     <td><?php echo $row['Address'];?></td> 
     <td><?php echo $row['Phone'];?></td> 
     <td><?php echo $row['Car_license_No'];?></td> 
     <td><?php echo $row['Car_Engine_No'];?></td> 
     <td><?php echo $row['Date'];?></td> 
     <td><?php echo $row['Mechanic'];?></td> 

    <?php 

    } 

    ?> 

    </table> 

    <?php  

    } 

    ?> 

    </div> 

    <div class="footer"> 

    <p id="lastMod"> 
    <script language="Javascript"> 
    document.write("Last modified on " + document.lastModified + " "); 
    </script> 
    </p> 

    </div> 

</body> 

</html> 

但问题是我不能改变表中显示的数据样式。 .footer,.center,.header这些都显示出完美的风格。但是,在php块中写入的html元素的样式不起作用,例如,。

这里是我的样式表

.header{ 
    background-color: black; 
    color: DC143C; 
    height: 150px; 
    margin-bottom: 0px; 
    font-family: "Impact" 
} 

.center{ 
    background-image: url(background.jpg); 
    height: 400px; 
    margin-top: 0px; 
    margin-bottom: 0px; 
} 

.footer{ 
    background-color: black; 
    font-family: "Impact"; 
    color: DC143C; 
    margin-top: 82px; 
    height: 95px; 
} 

th, td{ 
    color: white; 
} 

#lastMod{ 
    margin-top: 0px; 
    padding: 5px; 
} 

搜索了很多,但havn`t发现任何相关的答案。提前致谢。

+0

那么,您通过php定义的html表格不包含任何在CSS样式规则中使用的类名称。那么你为什么期望他们得到应用? – arkascha

+0

在你的表中添加class,比如'class =“mytable”',并且通过指向父母的这种'.mytable th {color:#fff; }' –

回答

0

您应该将类​​应用于单元格,然后您可以对其进行设置。

 <td class="name"><?php echo $row['Name'];?></td></br> 
... 
     <td class="mechanic"><?php echo $row['Mechanic'];?></td> 

<style> 
td.name { 
    background-color: #0f0; 
} 
.... 
td.mechanic { 
    background-color: #f00; 
} 
</style> 
0

你不想用父母如

<style> .mytable th { color:#fff; } </style>

指向你的PHP添加任何类,你应该在你的表中添加类像

<table class="mytable">

和风采吧