我尝试从管理面板制作样式切换器,我添加了:
index.php
<link rel="stylesheet" type="text/css" media="screen" href="<?php echo($thestyle);?>.css" >
<?php
$thestyle = $_GET['set'];
if ($thestyle == "style1")
{
$thestyle = "style2";
}
else
{
$thestyle = "style1";
}
?>
以及管理面板中的dashboard.php
<a href="index.php?set=style1">Style Sheet One</a>
<a href="index.php?set=style2">Style Sheet Two</a>
这会更改我网站上的样式表,但我怎样才能使更改也存储在 MySQL 中?
此代码仅适用于管理面板中的语言,不适用于用户,我想在mysql中制作一个简单的0-1样式
最佳答案
一种方法是为站点首选项创建一个 MySQL 表或存储键值对的表。然后您可以更新键为“style”的行并将值设置为1或2。
我担心的是,将偏好设置为全局并能够像这样设置有点奇怪。看起来这对以下任一者都更有意义:
- 使用与我上面描述的类似的方法将此偏好设置存储为用户表的一部分
- 将此偏好设置存储在 Cookie 中。
我会做一些类似的事情:
将值存储到 cookie
function updateStylePreference($style) {
$_COOKIE['stylePref'] = $style;
}
从 cookie 中检索值
function getStylePreference() {
if(isset($_COOKIE['stylePref'])) {
return $_COOKIE['stylePref'];
}
else {
// set a default
updateStylePreference('style1');
return 'style1';
}
}
既然您说您只需要自己使用,为什么不使用文本文件(如果您反对 cookie)?
将值写入文本文件
function updateStylePreference($style) {
file_put_contents("stylepref.txt", $style);
}
### Retrieving values from a text file
function getStylePreference() {
return file_get_contents("stylepref.txt");
}
不过我真的推荐这里的cookies。
关于php - PHP 和 MySQL 风格切换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22758587/