CodeIgniter Forums
database driven CSS? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: database driven CSS? (/showthread.php?tid=10021)



database driven CSS? - El Forum - 07-16-2008

[eluser]kwhitaker[/eluser]
Good afternoon all,

I'm writing an app that needs to let the end user input custom CSS for an item, and then have it called via a query.

Now, I know I could embed the styles into the HTML, but that goes against my anal-retentive-standards-based-to-a-fault methodology Smile

Is there a way to link to something like styles.php?css=some-number in the header, and then the styles.php could pull data from the DB based on that number?

Thanks!


database driven CSS? - El Forum - 07-16-2008

[eluser]Pascal Kriete[/eluser]
Not quite as you put it, but sure it's possible:
Code:
class Assets extends Controller {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function Assets()
    {
        parent::Controller();
    }

    // --------------------------------------------------------------------
    
    /**
     * Returns css files
     *
     * @access    public
     * @param    string    css name
     */
    function css($file = '')
    {
        if ($file == '')
        {
            die('Directory Listing Not Available');
        }
        
        $query = $this->db->get_where('css_table', array('css_row' => $file), 1, 0);

        if ($query->num_rows() > 0)
        {
            header('Content-Type: text/css');
            $result = $query->row();
            echo $result->content;
            exit;
        }
        
        die('Invalid CSS File: '.$file);
    }
}

So now linking to http://www.example.com/assets/css/something will try to load 'something' from the db.


database driven CSS? - El Forum - 07-17-2008

[eluser]Bramme[/eluser]
You can do this with a .htaccess file. You could implement the php queries in your .css file and then tell apache (through the .htaccess file) that it needs to handle the css file as php (and thus processes the queries).

I don't know the code for it, so you'll have to google that.

Of you could make a simple .htaccess redirect:
make a css.php file in your css folder that queries the db and builds the css file and then redirect using .htaccess. Adding an id to the url is easy with this:

Code:
RewriteEngine on
RewriteRule /css/styles.css?css=(.*) /css/css.php?css=$1 [L]



database driven CSS? - El Forum - 07-17-2008

[eluser]xwero[/eluser]
Why not use javascript and a cookie. The javascript is for switching the style and the cookie is to store the user setting for return visits.


database driven CSS? - El Forum - 07-17-2008

[eluser]Bramme[/eluser]
That would work if you only need a few css files... What if every user can define it's own stylesheet colors and you have about 1000 users Confused


database driven CSS? - El Forum - 07-17-2008

[eluser]xwero[/eluser]
The user is able to do his own styling so it are the themes you provide as a developer and your own theme. If others can be chosen i think it will happen in a gallery.

I would create a physical css file per user instead of generating it from the database.