Jump to content

dynamic styles.php help please


ricky spires

Recommended Posts

hello.

 

I have set up a back end systm for adding styles to my web site but i can but mary up the backend with the front end?

 

in the back end you can add a type, a category, a sub category and a value with is all added in to a database.

 

if i put the code on my index page:

 

<?php
if(isset($_GET['book_id'])){
   $book_id = $_GET['book_id'];

   $categories = Properties::cat_by_book($book_id);
   foreach($categories as $category){

       $propCat_id = $category->cat_id;
       $catNames = Cats::find_by_id($propCat_id);
       $cID = $catNames->id;
       echo $catNames->name.'{ ';

       $books = Properties::sub_by_cat($propCat_id);
       foreach($books as $book){
           $subCats_id = $book->subCat_id;
           $subNames = Sub_Cats::find_by_id($subCats_id);
           echo $subNames->name.': ';


           $bookProps = Properties::find_by_book($book_id);

           foreach($bookProps as $bookProp){

               $bp_id = $bookProp->id;
               $bp_client_id = $bookProp->client_id;
               $bp_book_id = $bookProp->book_id;
               $bp_page_id = $bookProp->page_id;
               $bp_area_type = $bookProp->area_type;
               $bp_type_id = $bookProp->type_id;
               $bp_cat_id = $bookProp->cat_id;
               $bp_subCat_id = $bookProp->subCat_id;
               $bp_media_id = $bookProp->media_id;
               $bp_content_id = $bookProp->content_id;
               $bp_value = $bookProp->value;
               $bp_format = $bookProp->format;


               if($book_id == $bp_book_id && $propCat_id == $bp_cat_id && $subCats_id == $bp_subCat_id){

                   if(!$bp_value == "" && $bp_media_id == "0" && $bp_content_id == "0"){
                   echo $bp_value.'; ';

                   }elseif($bp_media_id != "0" && $bp_value == ""){
                   echo'media;' ;

                   }elseif($bp_content_id != "0" && $bp_value == ""){
                   echo'content; ';
                   }
               }
           }
       }
       echo'}<br/>';
   }
}
?>

 

the code above will echo out whatever styles i set in the back end. {note that i have not included code for media and content. only value inputs.

 

i this case im setting the background color so the code above echos out.

 

body{ background-color: red;}

 

 

 

that all seems fine but how do i now get that to change my styles.

 

i tried removing that code from the index page and putting it in to a styles.php page like this...

 

<link rel="stylesheet" type="text/css" media="screen" href="assets/css/styles.php">

 

<?php header("Content-type: text/css"); ?>
<?php require_once("includes/initialize.php"); ?>
<?php
if(isset($_GET['book_id'])){
$book_id = $_GET['book_id'];

$categories = Properties::cat_by_book($book_id);
foreach($categories as $category){

 $propCat_id = $category->cat_id;
 $catNames = Cats::find_by_id($propCat_id);
 $cID = $catNames->id;
 echo $catNames->name.'{ ';

 $books = Properties::sub_by_cat($propCat_id);
 foreach($books as $book){
  $subCats_id = $book->subCat_id;
  $subNames = Sub_Cats::find_by_id($subCats_id);
  echo $subNames->name.': ';


  $bookProps = Properties::find_by_book($book_id);

  foreach($bookProps as $bookProp){

   $bp_id = $bookProp->id;
   $bp_client_id = $bookProp->client_id;
   $bp_book_id = $bookProp->book_id;
   $bp_page_id = $bookProp->page_id;
   $bp_area_type = $bookProp->area_type;
   $bp_type_id = $bookProp->type_id;
   $bp_cat_id = $bookProp->cat_id;
   $bp_subCat_id = $bookProp->subCat_id;
   $bp_media_id = $bookProp->media_id;
   $bp_content_id = $bookProp->content_id;
   $bp_value = $bookProp->value;
   $bp_format = $bookProp->format;


   if($book_id == $bp_book_id && $propCat_id == $bp_cat_id && $subCats_id == $bp_subCat_id){

 if(!$bp_value == "" && $bp_media_id == "0" && $bp_content_id == "0"){
 echo $bp_value.'; ';

 }elseif($bp_media_id != "0" && $bp_value == ""){
 echo'media;' ;

 }elseif($bp_content_id != "0" && $bp_value == ""){
 echo'content; ';
 }
   }
  }
 }
 echo'}<br/>';
}
}
?>

 

 

but that does not work..

If i remove that code and replace it with

 

<?php
echo $background = "green";
?>
body {
   background-color: <?php echo $background; ?>;
}

 

i get a green background ...

 

so how can i get my

body{ background-color: red;}
to change the bg ??

 

thanks

 

ricky

Link to comment
https://forums.phpfreaks.com/topic/273183-dynamic-stylesphp-help-please/
Share on other sites

ok.

 

i have ran in to an issue.

 

its seems to olny allow 1 style at a time ?

 

i.e if it echos back

 

body{ background-color: red; }
#bookWrapper{ width: 1000px; background-color: #ccc; }

 

only the body{ background-color: red; } seems to work.

if i remove body{ background-color: red; } then #bookWrapper{ width: 1000px; background-color: #ccc; } will work.

 

any idea ?

 

here is my code.

 

on the page i have

 

<!-- Custom Template Styles -->
<style type="text/css">
<?php echo getStyles(); ?>
</style>

 

 

my function looks like this

 

function getStyles() {
if(isset($_GET['book_id'])){
$book_id = $_GET['book_id'];

$categories = Properties::cat_by_book($book_id);
foreach($categories as $category){

$propCat_id = $category->cat_id;
$catNames = Cats::find_by_id($propCat_id);
$cID = $catNames->id;
$style = $catNames->style_type;


$bProps = Properties::area_by_id($cID);
foreach($bProps as $bProp){
$b_area_type = $bProp->area_type;
}


if($b_area_type == "styles"){

if($style == "none"){
 echo $catNames->name.'{ ';
}elseif($style == "id"){
 echo '#'.$catNames->name.'{ ';
}elseif($style == "class"){
 echo '.'.$catNames->name.'{ ';
}

$books = Properties::sub_by_cat($propCat_id, $book_id);
foreach($books as $book){

 $subCats_id = $book->subCat_id;
 $subNames = Sub_Cats::find_by_id($subCats_id);
 echo $subNames->name.': ';


 $bookProps = Properties::find_by_book($book_id);

 foreach($bookProps as $bookProp){

 $bp_id = $bookProp->id;
 $bp_client_id = $bookProp->client_id;
 $bp_book_id = $bookProp->book_id;
 $bp_page_id = $bookProp->page_id;
 $bp_area_type = $bookProp->area_type;
 $bp_type_id = $bookProp->type_id;
 $bp_cat_id = $bookProp->cat_id;
 $bp_subCat_id = $bookProp->subCat_id;
 $bp_media_id = $bookProp->media_id;
 $bp_content_id = $bookProp->content_id;
 $bp_value = $bookProp->value;
 $bp_format = $bookProp->format;

 if($book_id == $bp_book_id && $propCat_id == $bp_cat_id && $subCats_id == $bp_subCat_id){

 if(!$bp_value == "" && $bp_media_id == "0" && $bp_content_id == "0"){
 echo $bp_value.'; ';

 }elseif($bp_media_id != "0" && $bp_value == ""){

 $media = Images::find_by_id($bp_media_id);
 $file = $media->filename;

 echo' url("assets/images/uploads/'.$file.'");' ;

 }elseif($bp_content_id != "0" && $bp_value == ""){
 echo'content; ';
 }
 }

 }
}
echo'}<br/>';
}//end if
}
}
}

 

i goes roughly like this:

 

- first i get the book id ($book_id)

- then i get all the properties for the book

- then if its a style

- get the style type (id, class or none)

- then echo echo $catNames->name.'{ '; gives the class or id name

- then echo $subNames->name.': '; gives the style name

- then echo $bp_value.'; '; gives the value name

or echo' url("assets/images/uploads/'.$file.'");' ; gives an image

 

 

thanks

 

 

-

Strange...

 

FIXED IT

 

all i did was to remove the <br/> from the bottom so instead of echoing

 

body{ background-color: red; }
#bookWrapper{ width: 1000px; background-color: #ccc; }

 

it gives me

 

body{ background-image: url("assets/images/uploads/retina_wood.png");}#bookWrapper{ width: 1000px; background-color: #ccc; }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.