在管理面板的 dashboard.php 中 Style Sheet One Style Sheet Two -6ren">
gpt4 book ai didi

php - 风格切换器 PHP 和 MySQL

转载 作者:行者123 更新时间:2023-11-27 23:58:13 24 4
gpt4 key购买 nike

我试图从我的管理面板制作一个样式切换器,我添加了:

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 表或存储键值对的表。然后你可以更新键为“样式”的行并将值设置为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com