set var for each file in a dir?

liunx

Guest
Preamble:
first off, i must apologize for rarely if ever being of any help to others, particularly in re: PHP. i'm still just getting the hang of the simplest concepts in PHP (hey, look at me - i can work with variables and includes!), and am just not very comfortable with most of this stuff yet. strings, arrays, loops, dynamic variables, and setting cookies are things i've managed to do a couple of times now, but that's about it.

okay, on to my question...

goal:
what i'm trying to do is list the filenames in a directory, strip out the extensions (have gotten this far successfully), and then for each name make a variable that i can use elsewhere.

what it's for:
<!-- w --><a class="postlink" href="http://www.meat-thing.com/marsdev">www.meat-thing.com/marsdev</a><!-- w -->
i'd like to make it easier to add new skins to the system, preferably limiting the amount of work needed to simply dropping the files into their folders and letting mars s.t.s. do the rest.

implemented where, now?
rather than require the names of the skins to be hardcoded into every file, i'd like to have a loop that writes in the names of files in the CSS folder, minus their extensions.

for instance, in the switcher.php file, there's an array that has the names of the skins, which it uses to set a cookie. and then the viewable pages themselves have the dropdown box where users can select their preferred theme. there's also something to do with alternate stylesheets, but i'm thinking of nixing that thing.

what i have so far (not much!):<?
if ($handle = opendir("css")) {
echo "Directory handle: $handle\n<br />";
echo "Files:\n<br />";

// This is the correct way to loop over the directory.
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$skin = substr($file, 0, -8);
echo "$skin\n<br />";
}
}
closedir($handle);
}
?>when reading the directory you could store each name into an array using array_push


<?
$skins_array = array();
if ($handle = opendir("css")) {
echo "Directory handle: $handle\n<br />";
echo "Files:\n<br />";

// This is the correct way to loop over the directory.
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$skin = substr($file, 0, -8);
array_push($skins_array, $skin); //adds the name of the skin to the array
echo "$skin\n<br />";
}
}
closedir($handle);
}
//if you want to make a select input thing do:
echo "<select name=\"skin\">";
foreach ($skins_array as $s) {
echo "<option>$s</option>";
}
echo "</select>";

?>

is that what you were looking for?thanks, i'll try that!why use array_push??

$skins_array[]= $skin; //adds the name of the skin to the array

that works just as good.As usual with PHP there are 20 ways to accomplish the same task.

For info only, the differences between array_push and $arr[] are:

1) array_push will allow you to push an existing array as a new element; $array[] will not.
2) array_push expects the receiving array to exist (will not create one on the fly)
3) $array[] is slightly faster with large (and I mean large) arrays
4) $array[] will create an array if one does not already exist

Nine times out of 10 though they are equivalent in their funtionality. Programmer's personal preference as to which one should be used. :)<?
echo "$curseword1 $curseword2 $curseword3".", you guys are "."$curseword4 $curseword5"." awesome!!";
?>As all the $curseword data is related, you should store it in an array:<?
echo "$curseword[1] $curseword[2] $curseword[3]".", you guys are "."$curseword[4] $curseword[5]"." awesome!!";
?>hee hee hee :Dbtw, would anyone happen to know offhand how i could replace an underscore with a space? or is that pretty complicated? i tried finding something that might help me do that in the windows helpdoc, but didn't find it.

thanks, and sorry for the trouble!str_replace("_", " ", $var);$string = "blah_blah_blah";
$string = str_replace("_", " ", $string);
echo $string; //echoes blah blah blah


oops, beat me to itooh, sweet! again, you folks ROCK. thanks!
 
Back
Top