0

I'm trying to echo a php variable within my styles.php. This variable, which I defined in a separate (and included) php file, contains several values from a database :

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
     $SQL = "SELECT NumDep, DENS FROM france WHERE DENS BETWEEN '0' AND '45' ";
     $result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) )
{
$dens = $db_field['NumDep'] . ",<BR>";
echo $dens;

When I echo the variable $dens in my index.php file, the 20 values are showing normally. But when I echo the $dens variable in my styles.php file, only the last value is printed.

<?php
echo $dens; ?> <?php echo "
{
   fill: #FFC285;
   fill-opacity:1;
   stroke:white;
   stroke-opacity:1;
   stroke-width:0.5;
   stroke-miterlimit: 3.97446823;
   stroke-dasharray: none;
}

What I want is to print the 20 values, in the same way they are printed in the index.php file.

Can anyone help me? (Alternative solutions for printing these values from the database into the styles.php are welcome!)

Thanks in advance. Jonas

4
  • As an aside: I hope you are using PHP to pre-process this CSS rather than executing it on every request. That is a huge was of resources. Commented Mar 10, 2014 at 23:55
  • $dens = $db_field['NumDep'] . ",<BR>"; - you're putting HTML in a CSS file. Did you mean to use \n rather than <br>? Also, please consider switching to MySQLi or PDO for your queries, as the mysql_ functions are now deprecated and considered insecure. Commented Mar 10, 2014 at 23:56
  • 2
    That's happening because in your index.php file you're outputting it during a loop whereas in your styles.php file you aren't, so it's just outputting the last result. I hate to think that you're going to print that CSS style for each database row, you should be assigning a class to the elements with those names and defining a single CSS class style. Commented Mar 10, 2014 at 23:56
  • @Marty I don't understand exactly what you mean. I think my css is generated by each request... is there a better way? Commented Mar 11, 2014 at 11:17

4 Answers 4

1

The echo in your index.php is in a while loop, so it is printing multiple times. By the time you hit styles.php, the while loop is done, so the value of $dens is whatever it was at the end of the loop.

You need to use a while loop in your CSS for it to work as intended.

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

4 Comments

Note that I don't think this is a very good idea. That's just why you're having the issue that you are having.
Thanks a lot. Actually, I didn't find a better way to echo a range of IDs in my css, as these IDs should be easy to change (with the command BETWEEN to select specific fields in my database). Maybe there is a better way?
I don't recall finding a much better way in my PHP days, but .css.php files just make me shudder. If you can, I would try to refactor so you can dynamically assign classes (in a normal php file) that do what you want, instead of ever going the .js.php or .css.php route.
Ok I'll explore this. Thanks for the advice.
0

Instead

$dens = $db_field['NumDep'] . "\n";

use

$dens .= $db_field['NumDep'] . "\n";

1 Comment

This will also work (assuming the while loop remains in index.php and was not there just to debug).
0

Because you're looping through when doing the echo it will show every result. You need to mimic this in your CSS either with a while in the same way you have for the echo, or create an array of values in the while loop then loop through this later in your CSS.

Or you can append to $dens with .= instead of just = in your while loop

Comments

0

After getting more involved in programming, I came to the conclusion that a possible answer to my question would have been: use d3.js instead of making things complicated with "dynamic CSS"!

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.