本文介绍了如何将数据从MySQL数据库拉到自己的文件/文件夹中的样式表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立网站模板。这个模板我将用于我的20个网站。我正在做数据库驱动。这种方式,当我想要编辑它,我可以从我的数据库,无需上传任何东西或无需编辑每个页面上的项目/属性。



我想要能够将css属性的颜色放在我的数据库中。这里有一点我的代码。这个代码是css。

  @charsetutf-8 
/ * CSS文档* /
a {
display:block;
width:100%;
color:<??php echo $ styles_query_result [1] ['a_color']; ?
}
a:link {
text-decoration:none;
color:<??php echo $ styles_query_result [1] ['a_link']; ?
}
a:visited {
text-decoration:none;
color:<??php echo $ styles_query_result [1] ['a_visited']; ?
}

现在,只要样式在主页上, 。一旦我创建一个样式表并附加它,使样式作为styles.css在自己的文件夹中的代码不再起作用。我不想在主页上保留样式。



一旦样式在自己的文件和文件夹中,如何将数据库中的数据拉入样式表。 styles.css在根目录中的文件夹中。



根目录中的文件夹/文件:



php(从此页面中删除样式)



css(并将样式放在此处styles.css在此文件夹中)



图片



包括



提前感谢!



$

b $ b

如果您有两个表:





您可以使用它来创建css文件。伪代码:

 <?php 

// $ db = new PDO('mysql: host = localhist; dbname = cms','root','root');

// $ query = $ db-> query(SELECT * FROM` selector`);
// $ query = $ query-> fetchAll();

$ query = array(
array('id'=> 1,'selector'=>'.thisone'),
array('id'=> ; 2,'selector'=>'#thatone'),
array('id'=> 3,'selector'=>'.body')
);

$ probs = array(
1 => array(
array('id'=> 1,'selector_id'=> 1,'css_element'=> ;'border','element_value'=>'1px solid'),
array('id'=> 2,'selector_id'=> 1,'css_element'=& ('id'=> 1,'selector_id'=> 2,'css_element','element_value'=>'10px')
),
2 = ='border','element_value'=>'1px solid')
),
3 => array(
array('id'=> 1,'selector_id ','=> 2,'css_element'=>'width','element_value'=>'40px'),
array('id'=> 2,'selector_id'=& css_element'=>'height','element_value'=>'40px')

);

$ css ='';
foreach($ query as $ selector){
// $ properties = $ db-> query(SELECT * FROM`properties` WHERE`selector_id` = $ selector-> id
// $ properties = $ properties-> fetchAll();

$ properties = $ probs [$ selector ['id']];

$ rules ='';
foreach($ properties as $ element){
$ rules。=\\\
\t $ element [css_element]:$ element [element_value];;
}

$ css。=$ selector [selector]。'{'。$ rules \\\
。'}'。\\\
\\\
;
}

$ fp = fopen('style.css',w);
fwrite($ fp,$ css);
fclose($ fp);

echo< pre>;
echo$ css;
echo< / pre>;

?>

结果将是:

  .thisone {
border:1px solid;
padding:10px;
}

#thatone {
border:1px solid;
}

.body {
width:40px;
height:40px;
}



您可以使用css_file(id,name)添加表, css_file_id到选择器表。



您还可以创建一个编辑器表单,您可以在其中添加/删除/更改css规则/选择器/文件。


I am building a website template. This template I will use for 20 of my websites. I am making it database driven. This way when I want to edit it I can do so from my database with out ever uploading anything or with out having to edit the item/property on each page.

I want to be able to put the css properties for color in my database. Here is a bit of my code. This code is the css.

@charset "utf-8";
/* CSS Document */
a {
    display: block;
    width: 100%;
    color: <?php echo $styles_query_result[1]['a_color']; ?>;
}
a:link {
    text-decoration: none;
    color: <?php echo $styles_query_result[1]['a_link']; ?>;
}
a:visited {
    text-decoration: none;
    color: <?php echo $styles_query_result[1]['a_visited']; ?>;
}

Now, so long as the styles are on the main page this code above works. Once I create a style sheet and attach it so that the styles are in their own folder as styles.css the code no longer functions. I do not wish to keep the styles on the main page.

How can I pull the data in my database into the stylesheet once the styles are in their own file and folder. styles.css is in a folder in the root.

Folders/files in the root:

index.php (removed the styles from this page)

css (and put the styles in here. styles.css inside this folder)

images

includes

Thanks in advance!

解决方案

If you have a database template you can use it to create a .css file without using php inside the .css file.

If you have two tables:

You can use that to create a css file. Pseudo code:

<?php

    //$db = new PDO('mysql:host=localhist;dbname=cms', 'root', 'root');

    //$query = $db->query("SELECT * FROM `selector`");
    //$query = $query->fetchAll();

    $query = array(
        array('id' => 1, 'selector' => '.thisone'),
        array('id' => 2, 'selector' => '#thatone'),
        array('id' => 3, 'selector' => '.body')
    );

    $probs = array(
        1 => array(
            array('id' => 1, 'selector_id' => 1, 'css_element' => 'border', 'element_value' => '1px solid'),
            array('id' => 2, 'selector_id' => 1, 'css_element' => 'padding', 'element_value' => '10px')
        ),
        2 => array(
            array('id' => 1, 'selector_id' => 2, 'css_element' => 'border', 'element_value' => '1px solid')
        ),
        3 => array(
            array('id' => 1, 'selector_id' => 2, 'css_element' => 'width', 'element_value' => '40px'),
            array('id' => 2, 'selector_id' => 2, 'css_element' => 'height', 'element_value' => '40px')
        )
    );

    $css = '';
    foreach($query as $selector){
        //$properties = $db->query("SELECT * FROM `properties` WHERE `selector_id` = $selector->id");
        //$properties = $properties->fetchAll();

        $properties = $probs[$selector['id']];

        $rules = '';
        foreach($properties as $element){
            $rules .= "\n \t$element[css_element]:$element[element_value];";
        }

        $css .= "$selector[selector]".'{'."$rules \n".'}'."\n\n";
    }

    $fp = fopen('style.css',"w");
    fwrite($fp,$css);
    fclose($fp);

    echo "<pre>";
    echo "$css";
    echo "</pre>";

?>

Result will be:

.thisone{
    border:1px solid;
    padding:10px; 
}

#thatone{
    border:1px solid; 
}

.body{
    width:40px;
    height:40px; 
}

You can add a table with css_file(id, name) and then add a css_file_id to selector table. Then you can also write multiple files.

Also you can create an editor form where you can add/remove/change css rules/selectors/files.

这篇关于如何将数据从MySQL数据库拉到自己的文件/文件夹中的样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:31