www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Load parts of CSS

  1. #1
    Join Date
    Dec 2011
    Posts
    163

    resolved [RESOLVED] Load parts of CSS

    I'm not exactly sure how to describe this, so I'll do my best.

    I have a themes page that I want to recode.

    I am using MySQL, in my themes table, each row has the following set of fields...id, name, css, active.

    The css field points to the css file to be loaded on the main pages.

    I have several rows of themes.

    I want to be able to pull things such as the background image, background color, border, etc. from each theme's css file and populate the theme's box(div tag).

    The code would look something like this once done...

    HTML Code:
    <div id="theme_dir">
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    	<div class="theme_box" style="border:5px solid rgba(0,0,0,1);background-image:url('image.jpg');">
        	<h1 style="font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;font_size:0.8em;font-color:rgba(255,255,255,1);">Theme Name</h1>
        </div>
    </div>
    of course each one being different...

  2. #2
    Join Date
    Sep 2007
    Posts
    315
    tema.php
    Code:
      
    <html>
    <head>
    </head>
    <body>
    <div id="theme_dir">
    <?php
    mysql_connect("localhost","mysql_user","mysql_password");
    mysql_select_db("temalar");
    $tbl = mysql_query("select * from tema");
    
    while($satir = mysql_fetch_array($tbl)) {
    $cssdosya= $satir["css"];
    $tema_name= $satir["ad"];
    $file = fopen($cssdosya, "r") or exit("Unable to open file!");
    $i =0;
    //Output a line of the file until the end is reached
    while(!feof($file))   {
    
    //$metin .= fgets($file,4);
     ///echo fgets($file). "<br>";
     
     if($i==0) {
     echo strtr( fgets($file)   ,array (
      '.kutu' => '<div class="',
      ' { ' => '" style="',
      '}' => '">') );
      echo '  >Tema ad: '.$tema_name.'';
    }
    if($i==1) { 
     echo strtr( fgets($file)   ,array (
      'h1' => '<h1',
      ' { ' => ' style="',
      '}' => '">Bura h1 </h1></div>') );
    }
    $i++;
      }
    fclose($file);
    }
    // http://www.webdeveloper.com/forum/showthread.php?291911-Load-parts-of-CSS
    ?> 
    </body>
    </html>
    tema1.css
    Code:
     
    .kutu { width:300px; height:200px; border:5px solid red; color:red; background-image:url(kutucuk.jpg); }
    h1 { font-size:20pt; color:red; }
    tema2.css
    Code:
      
    .kutu { width:300px; height:200px; border:5px solid blue; color:blue;font-size:12px;background-image:url(daire.jpg); }
    h1 { font-size:20pt; color:blue; }

  3. #3
    Join Date
    Dec 2011
    Posts
    163
    Could someone move this to PHP...

  4. #4
    Join Date
    Dec 2011
    Posts
    163
    Very nicely done! I took what you posted, changed what I needed...

    PHP Code:
     -- updated code
    <?php 
        $sql 
    "SELECT * FROM themes ORDER BY id ASC";
        
    $theme_set mysqli_query($connect$sql);
        
    confirm_query($theme_set);
        while (
    $theme mysqli_fetch_assoc($theme_set)) {
            
            
    $cssFile $theme['css'];
            
    $name $theme['name'];
            
    $themeID $theme['id'];
            
            
    $file fopen(ROOT."_system/css/themes/".$cssFile,"r") or exit("Could not open CSS file!");
            
    $i 0;
            echo 
    "<div class=\"activeBox\" style=\"background-color:".($theme['active'] == "rgba(0,255,0,0.8)" "none")."\">";
            while(!
    feof($file)) {
                switch(
    $i){
                    case 
    0
                        echo 
    "<div class=\"themeBox\" style=\"margin:0 20px;";
                        
    //echo strtr(fgets($file), array(array("#themeBox","{","}") => "")); 
                        
    echo str_replace(array("#themeBox","{","}"),""fgets($file));
                        echo 
    "\">\n"
                        break;
                    case 
    1
                        echo 
    "<h1 style=\"";
                        
    //echo strtr(fgets($file), array(array("#themeH1","{","}") => "")); 
                        
    echo str_replace(array("#themeH1","{","}"),""fgets($file));
                        echo 
    "\">".$name."</h1>\n";
                        echo 
    "</div>\n";
                        break;
                    default:
                        exit;
                }
                
    $i++;
                
    //if($i>2) endwhile;
            
    }
            
    fclose($file);
            echo 
    "<a href=\"theme_update.php?id=".urlencode($themeID)."\">- Edit -</a>";
            echo 
    "</div>";
        }
        
    mysqli_free_result($theme_set);
    ?>
    Now I'm having a problem where my while(!feof($file)) loop times out. I tried an if statement with endwhile, but threw an error in code.

    UPDATE: I fixed the timeout problem, but my link at the bottom is not showing up, and I'm only getting my first result.
    Last edited by Dragonfire2008; 03-19-2014 at 09:56 PM. Reason: update

  5. #5
    Join Date
    Dec 2011
    Posts
    163
    Ok, so I got it now...

    PHP Code:
    <?php 
        $sql 
    "SELECT * FROM themes ORDER BY id ASC";
        
    $theme_set mysqli_query($connect$sql);
        
    confirm_query($theme_set);
        while (
    $theme mysqli_fetch_assoc($theme_set)) {
            
            
    $cssFile $theme['css'];
            
    $name $theme['name'];
            
    $themeID $theme['id'];
            
            
    $file fopen(ROOT."_system/css/themes/".$cssFile,"r") or exit("Could not open CSS file!");
            
    $i 0;
            echo 
    "<div class=\"activeBox\" style=\"background-color:".($theme['active'] == "rgba(0,255,0,0.8);" "none;")."\">";
            while(!
    feof($file)) {
                if(
    $i == 0){
                    echo 
    "<div class=\"themeBox\" style=\"margin:0 20px;";
                    
    //echo strtr(fgets($file), array(array("#themeBox","{","}") => "")); 
                    
    echo str_replace(array("#themeBox","{","}"),""fgets($file));
                    echo 
    "\">\n";
                } else if(
    $i == 1){
                    echo 
    "<h1 style=\"";
                    
    //echo strtr(fgets($file), array(array("#themeH1","{","}") => "")); 
                    
    echo str_replace(array("#themeH1","{","}"),""fgets($file));
                    echo 
    "\">".$name."</h1>\n";
                    echo 
    "</div>\n";
                } else if(
    $i 1) {
                    break;
                }
                
                
    $i++;
            }
            
    fclose($file);
            echo 
    "<a href=\"theme_update.php?id=".urlencode($themeID)."\">- Edit -</a>";
            echo 
    "</div>";
        }
        
    mysqli_free_result($theme_set);
    ?>
    Switched out the switch for ifelse and added the break. Everything works fine.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles