1

我试图使当一个MYSQL属性的值改变时,其他属性的其他值也不同。

例如:

我有值为“RED”的 color_scheme 属性。如果 color_scheme 值更改为“BLUE”,则值:

menu_color changes to #30618F
font_color changes to #FFFFFF
footer_color changes to #1A92CA

可能吗?

它必须只更新一次值,因为用户必须能够单独更改特定属性(如 menu_color、font_color、footer_color)。

像这样:

根据其他更新值

在此处输入图像描述

4

4 回答 4

2

尝试这个:

CREATE TABLE color_scheme (
  id_color_sheme INT AUTO_INCREMENT PRIMARY KEY,
  color_esquema VARCHAR(30),
  color_front_banners VARCHAR(7),
  color_front_banners_bottom VARCHAR(7),
  color_front_botoes VARCHAR(7),
  color_front_links VARCHAR(7),
  color_front_menu VARCHAR(7),
  color_front_rodape VARCHAR(7),
  index(color_esquema) 
  );



CREATE TABLE user_interface (
  id INT AUTO_INCREMENT PRIMARY KEY,
  color_front_banners VARCHAR(7),
  color_front_banners_bottom VARCHAR(7),
  color_front_botoes VARCHAR(7),
  color_front_links VARCHAR(7),
  color_front_menu VARCHAR(7),
  color_front_rodape VARCHAR(7)


  );

-- SAMPLE DATA
INSERT INTO user_interface (
                          color_front_banners,
                          color_front_banners_bottom,
                         color_front_botoes,
                         color_front_links,
                         color_front_menu,
                         color_front_rodape)
VALUES ('#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF'),
('#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE') ; 



INSERT INTO color_scheme (color_esquema,
                          color_front_banners,
                          color_front_banners_bottom,
                         color_front_botoes,
                         color_front_links,
                         color_front_menu,
                         color_front_rodape)
VALUES ('black','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF'),
('grey','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE') ;

-- UPDATE based on color_esquema
SET @color_name := 'black'; 
UPDATE user_interface 
SET 
  color_front_banners = (
SELECT color_front_banners FROM color_scheme
WHERE color_esquema = @color_name ),


  color_front_banners_bottom = (
SELECT color_front_banners_bottom FROM color_scheme
WHERE color_esquema = @color_name ),

  color_front_botoes = (
SELECT color_front_botoes FROM color_scheme
WHERE color_esquema = @color_name ),

  color_front_links = (
SELECT color_front_links FROM color_scheme
WHERE color_esquema = @color_name ),

  color_front_menu = (
SELECT color_front_menu FROM color_scheme
WHERE color_esquema = @color_name ),

  color_front_rodape = (
SELECT color_front_rodape FROM color_scheme
WHERE color_esquema = @color_name )

WHERE id = 2;

-- If you want to change just one color, you can use this query.
UPDATE user_interface 
SET 
  color_front_botoes = '#DDDDDD'
WHERE id = 1;

这是如何运作的?首先,您创建一个带有颜色的表(例如“color_scheme”)。然后,您从插入到您的“user_interface”表中的该表中获取值(例如“蓝色”、“#....”、...)。顺便说一句,我假设您会将其用于用户界面。无论如何,您可以根据需要采用它。需要注意的重要一点是,您只能更新首次插入数据库的数据。因此,您应该在添加用户时使用默认颜色。我什至建议您使用颜色创建一个单独的表,与用户表分开。

接下来,您要根据选定的配色方案更新默认颜色。假设您想使用“蓝色”。然后您应该使用更新查询来更新 user_interface 表(或颜色表)中的值。最基本的方法是像我一样做子查询,然后说 WHERE color_esquema = 'blue'。我添加了 set color_name 变量来简化条目。使用此语句,您开始声明一个名为 color_name 的变量,并且您只需要在整个查询过程中使用它就声明它。如果您想恢复,您可以插入您的 color_esquema 名称。

SQL 小提琴演示

我首先在 user_interface 的最后一行添加灰色。然后我使用更新查询将值更改为黑色的新颜色模式。将其分解为步骤。

CREATE TABLE color_scheme (
  id_color_sheme INT AUTO_INCREMENT PRIMARY KEY,
  color_esquema VARCHAR(30),
  color_front_banners VARCHAR(7),
  color_front_banners_bottom VARCHAR(7),
  color_front_botoes VARCHAR(7),
  color_front_links VARCHAR(7),
  color_front_menu VARCHAR(7),
  color_front_rodape VARCHAR(7),
  index(color_esquema) 
  );



CREATE TABLE user_interface (
  id INT AUTO_INCREMENT PRIMARY KEY,
  color_front_banners VARCHAR(7),
  color_front_banners_bottom VARCHAR(7),
  color_front_botoes VARCHAR(7),
  color_front_links VARCHAR(7),
  color_front_menu VARCHAR(7),
  color_front_rodape VARCHAR(7)


  );

-- SAMPLE DATA
INSERT INTO user_interface (
                          color_front_banners,
                          color_front_banners_bottom,
                         color_front_botoes,
                         color_front_links,
                         color_front_menu,
                         color_front_rodape)
VALUES ('#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF'),
('#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE') ; 



INSERT INTO color_scheme (color_esquema,
                          color_front_banners,
                          color_front_banners_bottom,
                         color_front_botoes,
                         color_front_links,
                         color_front_menu,
                         color_front_rodape)
VALUES ('black','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF'),
('grey','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE','#EEEEEE') ;

第一步

然后与更新相同的数据。顺便说一句,小提琴不允许我在右侧面板中执行此操作。见顶部的决赛。

第二步

PHP

用于更改配色方案的 PHP 脚本。

    <?php

    $color_name = 'black';

    $host = 'host';
    $user = 'user';
    $password = 'password';
    $database = 'database';

    $link = mysqli_connect($host, $user, $password, $database);
    IF (!$link) {
        echo ('Unable to connect to database');
    }
    ELSE{
        $query = "
            SET @color_name := '".$color_name."'; 
    UPDATE user_interface 
    SET 
      color_front_banners = (
    SELECT color_front_banners FROM color_scheme
    WHERE color_esquema = @color_name ),


      color_front_banners_bottom = (
    SELECT color_front_banners_bottom FROM color_scheme
    WHERE color_esquema = @color_name ),

      color_front_botoes = (
    SELECT color_front_botoes FROM color_scheme
    WHERE color_esquema = @color_name ),

      color_front_links = (
    SELECT color_front_links FROM color_scheme
    WHERE color_esquema = @color_name ),

      color_front_menu = (
    SELECT color_front_menu FROM color_scheme
    WHERE color_esquema = @color_name ),

      color_front_rodape = (
    SELECT color_front_rodape FROM color_scheme
    WHERE color_esquema = @color_name )

    WHERE id = 2;
            ";
        mysqli_query($link, $query);
    }
    mysqli_close($link);
    ?>

**Edit Wordpress udpate **

这只会加载配色方案名称:

   <?php
// insert database connection to get the colors from the color scheme 
   function retrieve_color_scheme() {
       $host = 'host';
       $user = 'user';
       $password = 'password';
       $database = 'database';

       $link = mysqli_connect($host, $user, $password, $database);
       IF (!$link) {
           echo ('Unable to connect to database');
       } ELSE {
           $query = "SELECT color_esquema FROM color_scheme";
           $result = mysqli_query($link, $query);
           $choices = mysql_fetch_array($result);
           return $choices;
       }
       mysqli_close($link);
   }


                    $this->add_control( 'color_esquema', array(
                            'label'   => 'Esquema de cores:',
                            'section' => 'custom_colors',
                            'type'    => 'select',
                            'priority'=> 9,
                            'choices'    => retrieve_color_scheme()
                    ) );                

                    $this->add_setting( 'color_menu', array(
                            'default'        => '#65696E',
                            'type'           => 'option',
                            'capability'     => 'edit_theme_options',
                            'transport'      => 'postMessage',
                            'priority'       => 10
                    ) );

                    $this->add_control( new WP_Customize_Color_Control( $this, 'color_menu', array(
                            'label'   => __( 'Fundo do Menu'),
                            'section' => 'custom_colors',
                            'settings'   => 'color_menu',
                            'priority'       => 10
                    ) ) );

                    $this->add_setting( 'color_titulo_menu', array(
                            'default'        => '#FFFFFF',
                            'type'           => 'option',
                            'capability'     => 'edit_theme_options',
                            'transport'      => 'postMessage',
                            'priority'       => 15
                    ) );

                    $this->add_control( new WP_Customize_Color_Control( $this, 'color_titulo_menu', array(
                            'label'   => __( 'T&iacute;tulo'),
                            'section' => 'custom_colors',
                            'settings'   => 'color_titulo_menu',
                            'priority'       => 15
                    ) ) );                
?>                   
    <!-- Page -->
    <?php $color_menu = get_option('color_menu');  ?>
    <?php $color_titulo_menu = get_option('color_titulo_menu');?>

        #container{border-top:82px solid <?php if(empty($color_menu)){echo "#65696E";} else { echo "$color_menu";}; ?>}
        #logo a span{color:<?php if(empty($color_titulo_menu)){echo "#FFFFFF";} else { echo "$color_titulo_menu";}; ?>}

    <!-- My try - In Page -->
于 2013-03-08T22:59:51.950 回答
0

You can use a CASE statement for this. This is a pretty simplified version, but this should get you on the right track.

UPDATE mytable
SET menu_color = CASE WHEN color_scheme = 'Red' THEN '#30618F' WHEN color_scheme = 'Blue' THEN  '#FFFFFF' ELSE '' END
于 2013-03-08T22:17:33.257 回答
0

You should make a color_schemes table and store the details for each color scheme there.

于 2013-03-08T22:17:40.930 回答
0

激进的,

我是阿图尔的朋友,昨天我尝试了不同的方法来解决同样的问题,我不知道它是否更容易还是什么......请看看我的另一个问题

我们使用的是 Wordpress,我们不太擅长编写 PHP,我们可以调整脚本和东西,但我们不太擅长创建自己的。

感谢您的努力,我们就快到了!

于 2013-03-09T13:17:50.097 回答