0

This is the problem I have: I am making a CSS editor for website administrators, but only for certain styles. What I need is a way to find a word or a phrase in the CSS file (the title of the style), and then change its content. So, in other words, if I have this code:

.test_style {
font-size:12px;
color:#ffffff;
}

and the administrator chooses the style

test_style

and writes this:

font-size:16px;
color:#000000;
font-weight:bold;
margin-top:5px;

the PHP script should find the corresponding style, and change the content to this:

.test_style {
font-size:16px;
color:#000000;
font-weight:bold;
margin-top:5px;
}

Does anyone have any ideas how I could do this?

EDIT: My idea is to let administrators choose a selector from a drop-down menu, write the code in a textarea, and then save the code they write into that specific style in the CSS file.

3
  • 1
    store css in db, then generate css files on the fly Commented Sep 10, 2012 at 13:16
  • The thing is-I already have a very large CSS file, which would be very hard to transfer to a database. I was thinking of a function which could go through a CSS file (like through a text file), find the corresponding style, then insert code between its { and }... Commented Sep 10, 2012 at 13:35
  • if you follow the link I posted in the answer below, you can create a file with .php extension instead of css, but instead of just css content you include sections -yes only sections- which users can edit, only this part can be saved in db while all other things can be directly edited from the file as if its css, so actually you are giving user control only to variable parts only Commented Sep 10, 2012 at 13:38

5 Answers 5

2

you can use the same idea on this site: http://www.barelyfitz.com/projects/csscolor/

I copied the link from: Using PHP as a CSS file?

all you have to do is use db to store actual css content in a text field

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx, I am going with this one.
2

If you are trusting them to actually write CSS anyhow, why not just pull the styles they can edit into another file, and import it into the mail CSS file. That way they won't see all the confusing parts, and you don't have to worry about processing the file each time they make a change.

1 Comment

Well, the thing is, I am trusting them to know some basic css coding, such as to change font size, color, weight... But I want to make sure what they write, is inserted into the right style. Also that would be an easy way for me to change certain styles, without having to search for them in the css file itself...
1

@Hawili has a great answer, but you can also store the individual rules somewhere else (flat files, couchDB stores, etc). The problem here is interface: do you want them to be able to add their own selectors? Do you want them to pick from a list of selectors that already exist? If they're choosing, why not have them also choose from a list of common declarations, or modify them parametrically?

The real question you should be asking isn't just how to implement this solution, it's that you should analyze the problem and see if this solution is the correct one. If it really is, you can also use regEx matches to find any code after the selector and modify that with the user's input. But I would recommend you store the discrete data points in some other way easier to modify and then save that output to your css file.

2 Comments

Well, the thing is, I am trusting administrators to know some basic css coding, such as to change font size, color, weight... But I want to make sure what they write, is inserted into the right style. Also that would be an easy way for me to change certain styles, without having to search for them in the css file itself...
If that's the case, I'd recommend what others have said, you should store this in a normalized database. I would follow @blasteralfred 's example, except make sure to provide a unique auto-incrementing ID to the table, for example RuleID. Alternately, you could use the selector as the unique identifier of each column and avoid 100% similar rules, but a lot of people will get on to you for using text IDs.
1

You can do this with a database. Create a database in mysql with a table. Make a column for selector and another column for styling so that the table will look like;

---------------------------------
| selector   | style            |
---------------------------------
|.test_style | font-size:12px;  |
|            | color:#ffffff;   |
---------------------------------

You may echo this data to your php file to display. When admin changes the style, you may update the style field, looking for .test_style as the key. You may try jQuery + AJAX to send and receive data avoiding a form or page reload.

Comments

0

Try to do something like this :

  <form method="POST" action="admin/settings/css-example">

  <textarea id="css-example" name="css-example" rows="10">
  <?php $template_file = "../css-example.css";
  $file_handle = fopen($template_file, "rb");
  echo fread($file_handle, filesize($template_file));
  fclose($file_handle);
  ?>
  </textarea>

  <div class="form-actions">
  <input type="hidden" name="section" value="css-example">
  <input type="submit" name="updatesettings" value="Update">
  </div>
  </form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.