dmccabe Posted March 10, 2008 Share Posted March 10, 2008 I am just playing php, html and css and want to create page where the user can select different css styles to apply to the site. I have already made the site and created 2 different css files an have even managed to make it so the user can select css 1 or css 2, click submit and it applies it to the site. However what would be better is if it could read the list of css files from the root directory and populate the drop down list. I did find an example of a php script to list files, but couldn't work out what exactly it was doing so would prefer to learn it properly. Any takers to give me a well coded example? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 10, 2008 Author Share Posted March 10, 2008 Update...lol (it's always the same I look at a problem for hours then just after posting I work it out lol). Anyways, using the example I found on the web for getting the list of files, I did this: <? if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="'.$file.'">'.$file.'</a>'; echo "<option value=\"$file\">$file</option>"; } } closedir($handle); } ?> Which does almost what I want, except it lists all the files not just the .css files. Now my question is how do you do a wilcard match in php ? so I can check if $file = *.css or the equivalent. Quote Link to comment Share on other sites More sharing options...
raydawg Posted March 10, 2008 Share Posted March 10, 2008 http://us.php.net/manual/en/ref.pcre.php Quote Link to comment Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 Try... if ($file != "." && $file != ".." && substr($file, -4) == ".css") Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2008 Share Posted March 10, 2008 <?php foreach (glob($_SERVER['DOCUMENT_ROOT'] . "/*.css") as $file) { echo "<option value=\"$file\">$file</option>"; } ?> Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 10, 2008 Author Share Posted March 10, 2008 Thanks for the replies guys. Charlie, yours did the trick nicely, but I think thorpe's would be the correct way if trying to keep to good coding standards. That said I am very new to PHP, html and CSS so would appreciate it if you could explain your code a little Thorpe? Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2008 Share Posted March 10, 2008 Its quite simple. glob returns an array of files matching the pattern described in the first argument. You then use a foreach to loop through this array. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted March 11, 2008 Share Posted March 11, 2008 I just took your code and added something. I didn't want to rewrite your entire code for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.