0

I want to find an image source from a MySQL database and assign it to a css style. I just changed the .css extension to .php.

It does work with XAMPP and runs everything I want, but when I uploaded my code in cpanel it didn't work correctly.

I used this code in the css file:

Top of page:

<?php include '../Connections/Base.php'; ?>
 <?php header("Content-type: text/css; charset: UTF-8"); ?>

Some styles:

.box {
    overflow: hidden;
    display:block;
    border:4px solid #171717;
    position:absolute;
    cursor: pointer;
    float:left;
    display: inline-block;
    zoom: 1;
    background:url(<?php 
        $query = "SELECT imape_path FROM MyTable WHERE number='5' LIMIT 1";
        //echo $query;
        if ($result = $mysqli->query($query)) {
            while ($row = $result->fetch_assoc()) {
                echo $row['imape_path'];
            }
        }
 ?>) no-repeat;
}

How can I fix this problem?

7
  • You will need to change file type to .php and echo current css code inside that , though this method is not preferred. Commented Jun 14, 2015 at 12:16
  • if i change css codes to php echo , can HTML tags give data from php echo and convert it to css style? Commented Jun 14, 2015 at 12:18
  • Note that: my codes doing work with XAMPP but it have problem with CPanel Commented Jun 14, 2015 at 12:20
  • Could you plz post error if possible Commented Jun 14, 2015 at 12:26
  • It don't get any errors, just It don't set css style to HTML tags, Tables and divs in HTML will be tumultuous Commented Jun 14, 2015 at 12:30

2 Answers 2

2

Personally, I do not think this is the right problem to address.

The whole point in separating markup and styles is to simplify things. I see very little value in separating it so rigidly you have to mix css, php and sql instead (you just moved the same problem elsewhere).

My suggestion is to configure .box's background using inline css in the html page itself and inject the url as any other value:

<div class="box" style="background: url('<?php ... ?>')">...</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You could try keeping the .css extension on the file with PHP code, and setting a rule in .htaccess to parse the file as PHP (source).

<FilesMatch "^.*?style.*?$">
SetHandler php5-script
</FilesMatch>

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.