While Loop Creating CSS Classes

He has: 578 posts

Joined: Jun 2004

<?php
$menu
[0] = 'front';
$menu[1] = 'work';
$menu[2] = 'philosophy';
$menu[3] = 'contact';

$number = 4;
$x = 0;

while (
$x < $number) {
echo \
"
.header a.
$menu[$x], .header a.$menu[$x]:visited {
    width: 100px;
    height: 130px;
    background: url('imgs/menu/
$menu[$x].png);
    display: block;
}

.header a.
$menu[$x]:hover, .header a.$menu[$x]:visited:hover {
    background: url(imgs/menu/
$menu[$x]_hover.png')
}
\";
++
$x;
}
?>

Shouldn't this create a class for each of the menu items? I've got a little thing at the top that supposedly makes it parse PHP, but still look like css (

<?php
Header
("Content-type: text/css");
?>
).

You can see what it does at http://madclassifieds.000k.net/

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

2 things:
1: it's usually written $x++; but I think ++$x; "adds it sooner" or something to that effect, again, I don't think it matters, but it might.

2: You have the file named main.css, which will not be read as php, a php file will be read as CSS, but not vice versa. You have the header() set in the file which I think is needed for Firefox, but yeah. You need to make a .htaccess either making things named .css processable by php, OR rename it main.php and set the .htaccess to read it as text/css and php, though I don't know if thats totally possible, heh.

Just rename the file .php, and it should work.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

One you reaname it main.php, you should be ok, however there are two other things to look at, both of which didn't prevent your script from running in ZDE.

When using an array inside quotes, instead of just doing $array['element'] you should use {$array['element']} I thought this was required, but eiter it isn't now or it is becasue you used a variable for the element name it worked without them Wink

Also, you do not have your ending ?> tag at the end of the file.

Both are just for better coding practices.

-Greg

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

It only becomes an issue to not use {} when you have things that can be valid parts of variable names ie letters and underscores. I think it's also required with some multilevel arrays like $array[$key1][$subkey].

He has: 578 posts

Joined: Jun 2004

Did it work in your browser? I still have nothing, even when I set a class without the PHP.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.