根据用户偏好从数据库加载CSS

发布于 2024-12-15 13:39:01 字数 745 浏览 2 评论 0原文

我目前正在尝试为我的基本 Intranet 推出自己的 CSS 样式切换器。

这并不是很重要,但我知道一些员工的视力稍差,并且认为高对比度风格可能会有一些用处。

这是我理想的设置,以及迄今为止我尝试实现的方式。

基本上我已经有一个 mysql 数据库,其中包含用户登录详细信息以及有关该站点的其他次要首选项。我希望为样式首选项添加一个额外的列,并在用户区域中有一个简单的页面,用户可以在其中预览、选择和应用样式。

如果当用户最初选择样式时,将其应用到页面,以便可以看到它的实际效果,然后可以确认它是否是用户选择的样式,这显然会很好。

到目前为止我所尝试的:

基本上,当用户登录时,它创建了一个会话变量,其中包含该用户的“样式”列的值。我使用数字进行测试,可选择 3 种样式:1、2 和 3。然后在页面的标题部分,对于样式表的 href,我添加了一些 PHP 来评估会话变量并回显样式表根据设定的内容。

效果很好,样式表根据样式列的 mysql 表中的内容而有所不同。在用户首选项页面上,我有点崩溃了。我创建了一个带有 3 个单选按钮的小表单,每个按钮对应一种样式。我认为从这里开始的唯一方法是将表单发布到 PHP 文件,该文件根据选择更新表格。

我以这种方式执行此操作的主要问题是用户必须注销并重新登录才能使更改生效。我想要一个可以当场或刷新时进行更改的设置。

我可以选择饼干,但如果可能的话我宁愿避免。不知道是否有人有任何建议可以帮助我,或者是否有任何预先编写的脚本可以完成此任务?

非常感谢

编辑

I am currently trying to roll my own CSS style switcher for my basic intranet.

It's not hugely important, but I know a couple of employees have slightly poor eyesight and thought a high contrast style may be of some use.

Here is my ideal set up, and the way I have tried to implement so far.

Basically I already have a mysql database that house user log on details, and other minor preferences regarding the site. I would like to have an extra column for the style preference, and have a simple page in the user area where the user can preview, select and apply the style.

It would obviously be good if when the user selects the style initially, it applies it to the page so that it can be seen in action, and then can be confirmed if it is what the user chooses.

What I have attempted so far:

Basically, when the user logged on, it created a session variable which contains the value of the "style" column for that user. I went with numbers for the test, with a choice of 3 styles, 1, 2 and 3. Then in the head section of the page, for the href of the stylesheet, I added some PHP that evaluated the session variable and echoed a stylesheet based on what was set.

That worked fine, the stylesheet was different depending on what was in the mysql table for the style column. On the user preferences page is where I fell apart a bit. I created a small form with 3 radio select buttons, one for each style. The only way I could see to go from here, was for the form to post to a PHP file that updates the table based on selection.

The main problem I have doing it in this manner is that the user has to log out and back in for the changes to take effect. I would like a setup where the change happens on the spot or on a refresh.

I could go with cookies, but would rather avoid if possible. Don;t know if anyone has any suggestions to help me along or if there are any pre written scripts that could accomplish this?

Many thanks

Eds

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仙女 2024-12-22 13:39:02

除非您很小心,否则从数据库提供 CSS 会破坏浏览器缓存 CSS 文件的能力。这会减慢每个页面的加载速度。

您应该做的是将默认样式表作为文件提供,然后,如果需要,根据用户的会话变量从数据库提供自定义样式表。自定义样式应该简单地覆盖现有样式。

Unless you are careful, serving up the CSS from a database breaks the ability for browsers to cache the CSS file. This will slow down every page load.

What you should do is serve up the default stylesheet as a file, then, if needed, serve up the custom one from from the database, based on the user's session variable. The custom one should simply overwrite existing styles.

单身狗的梦 2024-12-22 13:39:02

我不明白为什么用户必须注销并重新登录才能使更改生效,或者他们必须登录和退出什么。这听起来像是您的应用程序的问题。

也许你的意思是这个?

IE 和 Firefox 都会缓存从文件加载的 CSS。只要 CSS 文件名相同,用户就不会看到更新后的样式,直到手动刷新页面。要解决此问题,如果您从文件加载 CSS,则每个样式都应该有自己唯一的文件名。

I don't understand why the user has to log out and back in in order for the changes to take effect, or what they have to log in and out of. This sounds like an issue with your application.

Maybe you mean this?

Both IE and Firefox will cache the CSS that is loaded from a file. As long as the CSS filename is the same, the user won't see the updated style until they manually refresh the page. To work around this, if you are loading the CSS from a file, then each style should have its own, unique filename.

心头的小情儿 2024-12-22 13:39:01

听起来您也必须将会话变量设置为新值。
因此,请在同一位置更新数据库和会话变量。

Sounds like you have to set the Session Variable to the new value too.
So update your database and your session variable at the same place.

倾城泪 2024-12-22 13:39:01

要编写一个执行此操作的脚本,我将使用 JQuery 并发布帖子或获取更新用户首选项的脚本。然后应用帖子返回的CSS或相应地获取。

比如:

$.get('coolCss.php','cssid=3',function(data){
    $('#myCssTag').empty();
    $('#myCssTag').append(data);
});

希望这能解决这个问题。

但我会考虑使用不同的 css 文件并更改所选文件。

to write a script that do this i would use JQuery and do a post or get to the script updating the user-pref. and then apply the css returned by the post or get accordingly.

someting like:

$.get('coolCss.php','cssid=3',function(data){
    $('#myCssTag').empty();
    $('#myCssTag').append(data);
});

hope this can solve it.

but i would consider using diffrent css-files and change the the selected file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文