php - PHP 和 MySQL 风格切换器

标签 php html mysql css

我尝试从管理面板制作样式切换器,我添加了:

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。

我担心的是,将偏好设置为全局并能够像这样设置有点奇怪。看起来这对以下任一者都更有意义:

  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/

相关文章:

html - 如何使一组图像与 CSS 中的中心对齐?

html - 一旦我们在文本字段中单击光标,背景颜色就会发生变化

mysql - 连接到2sxc模块中的其他数据库类型

javascript - NaN 倒计时器 JavaScript

javascript - 如何通过 PHP 填充 Bootstrap 弹出窗口

html - css 选择选项页面

mysql - AES_ENCRYPT 返回的字符串是否唯一?

php - print_r($ _ FILES);输出为空Array()**。这意味着没有文件上传,但是我已经上传了文件

php - 如何修复opencart默认时区

PHP SimpleXML + 获取属性