2017-10-14 79 views
0

我有一个表单,允许用户更改他们所在网站的样式。我尝试将我的样式表保存为一个PHP文件,但现在样式不显示,尽管我将样式表链接更改为href =“styles.php”。我怎样才能让PHP样式表影响我的页面?这里是样式表代码:如何将PHP和SQL添加到样式表中

<?php include("session_start.php")?> 

<?php 

$sql = "SELECT * FROM styles WHERE user_name='$user_name'"; 

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection)); 

?> 

<?php 
while($column = mysqli_fetch_assoc($result)){ 
?> 

body { 
padding-top: 40px; 
padding-bottom: 40px; 
background-color: <?php echo $column['background_color'] ?>; 
color: <?php echo $column['text_color'] ?>; 
font-size: <?php echo $column['body_size'] ?>; 
} 

h1,h2,h3,h4,h5,h6 { 
color: <?php echo $column['header_color'] ?>; 
} 

h1 { 
font-size: <?php echo $column['h1_size'] ?>; 
} 

h2 { 
font-size: <?php echo $column['h2_size'] ?>; 
} 

h3 { 
font-size: <?php echo $column['h3_size'] ?>; 
} 

h4 { 
font-size: <?php echo $column['h4_size'] ?>; 
} 

h5 { 
font-size: <?php echo $column['h5_size'] ?>; 
} 

h6 { 
font-size: <?php echo $column['h6_size'] ?>; 
} 

.registration-form { 
max-width: 300px; 
margin: 100px auto; 
} 

.align-right { 
text-align: right; 
} 

#post_content { 
width: 100%; 
height: 200px; 
} 

.view-post-content { 
padding-top: 10px; 
padding-bottom: 10px; 
} 

.category { 
font-weight: bold; 
} 

hr { 
border-top: 1px solid grey; 
} 

a:hover { 
color: purple; 
} 

<?php 
} 
?> 
+0

你在哪里设置'$ user_name'? – Barmar

+0

你的查询能否真的返回多行?如果是这样,你会得到相同元素的多个样式。如果没有,为什么你有一个'while'循环来处理结果? – Barmar

+0

@Barmar $ user_name在会话中设置。至于while循环,我只使用它,因为这是我学会如何返回数据的方式。 – Julian

回答

0
<style type="text/css"> 
<?php 
while($column = mysqli_fetch_assoc($result)){ 
?> 

body { 
padding-top: 40px; 
padding-bottom: 40px; 
background-color: <?php echo $column['background_color'] ?>; 
color: <?php echo $column['text_color'] ?>; 
font-size: <?php echo $column['body_size'] ?>; 
} 

h1,h2,h3,h4,h5,h6 { 
color: <?php echo $column['header_color'] ?>; 
} 

h1 { 
font-size: <?php echo $column['h1_size'] ?>; 
} 

h2 { 
font-size: <?php echo $column['h2_size'] ?>; 
} 

h3 { 
font-size: <?php echo $column['h3_size'] ?>; 
} 

h4 { 
font-size: <?php echo $column['h4_size'] ?>; 
} 

h5 { 
font-size: <?php echo $column['h5_size'] ?>; 
} 

h6 { 
font-size: <?php echo $column['h6_size'] ?>; 
} 

.registration-form { 
max-width: 300px; 
margin: 100px auto; 
} 

.align-right { 
text-align: right; 
} 

#post_content { 
width: 100%; 
height: 200px; 
} 

.view-post-content { 
padding-top: 10px; 
padding-bottom: 10px; 
} 

.category { 
font-weight: bold; 
} 

hr { 
border-top: 1px solid grey; 
} 

a:hover { 
color: purple; 
} 

<?php 
} ?> 
</style> 
0

创建一个PHP文件,并 将此代码添加到您的文件简单=> style.php 标题(“内容类型:text/CSS;字符集= UTF- 8" ); 并添加你的风格并添加文件到你的页面样式

相关问题