从多个主题中选择用户(PHP)的动态主题

发布于 2025-01-23 03:25:12 字数 113 浏览 1 评论 0原文

我想将动态主题添加到我的PHP网站上,该主题将从用户的数据库中存储,下次用户使用该网站应显示该主题,该主题已由用户选择并保存到数据库中。我到处都在搜索,但由于我是PHP的新手,但没有获得逻辑和解决方案。请帮助我。

I want to add dynamic themes to my php website which will be stored from the user's database and next time when user uses the website that theme should be shown which has been selected by user and saved to database. I have searched everywhere but not getting the logic and solution as I am new to php. Please help me out.

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

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

发布评论

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

评论(1

会发光的星星闪亮亮i 2025-01-30 03:25:12

由于摘要的沙箱,下面的片段将在此处丢弃错误,但如果复制到您自己的文档中,则可以使用。

不同主题的选择机制可以是超链接,按钮,无线电按钮或下拉菜单,如下所示。当然,需要修改活动的侦听器,以适合任何选择的机制。

当用户制作选择时,使用setItem将关联的值保存到localstorage。加载页面后,检查了localStorage,并为样式表分配了正确的URL。

const _STORE = 'site_SHEET';
const oSelect = document.querySelector('select[name="theme"]');

// function that saves the selected value to localStorage
const eventhandler = function(e) {
  localStorage.setItem( _STORE, this.value );
  setsheet( this.value );
};

// function to return the full path to the actual stylesheet. Change path to suit...
const setpath=( theme )=>`css/${theme}.css`;

// method to assign the new style
const setsheet=( theme )=>document.getElementById('theme').setAttribute('href', setpath( theme ) );

// Set the dropdown to the stored theme when the page loads...
const setoption=(theme)=>oSelect.value=theme;

// assign the event listener to whatever selection mechanism you use to select themes
oSelect.addEventListener('change', eventhandler );

// if the localStorage contains a selected theme, load it...
if( localStorage.getItem( _STORE )!=null ){
  setsheet( localStorage.getItem( _STORE ) );
  setoption( localStorage.getItem( _STORE ) )
}
<link rel='stylesheet' type='text/css' id='theme' />

<select name='theme'>
  <option disabled hidden selected>Select Theme
  <option value='modern'>Modern
  <option value='standard'>Standard
  <option value='dark'>Dark
  <option value='visually-impaired'>Visually-Impaired
</select>

Update

我忽略了上述代码使用的四个样式表,这些样式表按选择菜单中的选项命名,例如:Modern.css &amp; dark.css等。使用简单setPath函数设置样式表目录的路径,该功能将路径硬编码为返回字符串(更改以适合自己的环境)

The snippet below will throw an error here because of the sandboxing of the snippet but will work if copied to your own document.

The selection mechanism for different themes could be hyperlinks, buttons, radio buttons or a dropdown menu as shown here. The event listener would need to be modified to suit whatever mechanism chosen of course.

When the user makes a selection the value associated is saved to localStorage using setItem. When the page is loaded the localStorage is checked and the stylesheet is assigned the correct url.

const _STORE = 'site_SHEET';
const oSelect = document.querySelector('select[name="theme"]');

// function that saves the selected value to localStorage
const eventhandler = function(e) {
  localStorage.setItem( _STORE, this.value );
  setsheet( this.value );
};

// function to return the full path to the actual stylesheet. Change path to suit...
const setpath=( theme )=>`css/${theme}.css`;

// method to assign the new style
const setsheet=( theme )=>document.getElementById('theme').setAttribute('href', setpath( theme ) );

// Set the dropdown to the stored theme when the page loads...
const setoption=(theme)=>oSelect.value=theme;

// assign the event listener to whatever selection mechanism you use to select themes
oSelect.addEventListener('change', eventhandler );

// if the localStorage contains a selected theme, load it...
if( localStorage.getItem( _STORE )!=null ){
  setsheet( localStorage.getItem( _STORE ) );
  setoption( localStorage.getItem( _STORE ) )
}
<link rel='stylesheet' type='text/css' id='theme' />

<select name='theme'>
  <option disabled hidden selected>Select Theme
  <option value='modern'>Modern
  <option value='standard'>Standard
  <option value='dark'>Dark
  <option value='visually-impaired'>Visually-Impaired
</select>

update

I neglected to state that the above code uses four stylesheets that are named as per the options in the select menu - eg: modern.css & dark.css etc. The path to the stylesheet directory is set with the simple setpath function that has the path hardcoded into the return string ( change to suit own environment )

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