Page 1 of 1
PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 10:25 am
by knight42
Hey everyone! Long time reader, first time poster! So please stay with me this may sound like a muddle, im starting to learn PHP but im pretty basic at the moment.
I have a PHP function that calls for the file names in a directory. That all works perfectly fine. The code is below:
Code: Select all
<?php
$string="";
$fileCount=0;
$filePath=$PATH.(TEMPLATEPATH . '/'); # Specify the path you want to look in.
$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) {
if (eregi("\.php",$file)) { # Look at only files with a .php extension
$string .= "$file,<br />";
$fileCount++;
}
}
if ($fileCount > 0) {
echo sprintf($string,$fileCount);
}
?>
Below that, I have some code that pulls in an array to use as a select list (this is for a wordpress theme options page) which is this:
Code: Select all
<?php foreach ($file['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
I have been trying all day without any success to bring the two together. I want a user to be able to drop files into a folder and then have those files listed in a drop down menu so they can select one.
I can get the two features working side by side, but cannot bring them together. Im tearing my hair out here with this and I havent got much hair to start with!
Could anyone possibly help, im really stuck on this one.
Thanks if anyone can help! Feel free to say if I havent explained myself very well.
Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 10:57 am
by andyhoneycutt
First let me say welcome! I'm curious what isn't working within your solution. You state that "...I can get the two features working side by side, but cannot bring them together." What exactly does this mean? side-by-side to me is the same as them working together, so I'm a mite bit confuzzled!
-Andy
Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 1:31 pm
by knight42
Hi Andy, thanks for the welcome.
Sorry for the confusion, have just read that back myself and it didnt make much sense. What I meant was, I have them both working on their own separate things on a page, there is no join. On the HTML page, I can list all of the pages and seperatly can make a select box work with differnt contents, but cannot join the two.
I hope I can fix it, so the list is IN the select box as options.
Hope thats clearer, its been a long day!

Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 1:36 pm
by andyhoneycutt
From the looks of it, you're attempting to access $file as an array. In your top code block you are explicitly defining it as a string via the readdir() function. Could you post the entire contents of your script (or at least the code in-between these blocks)? Please also use the code=php tag to enclose it.
Thanks much,
Andy
Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 4:25 pm
by knight42
Hi Andy and everyone else!
Thanks for helping out. There isnt that much code in between im afraid. Have listed below what I have:
Code: Select all
<?php
$string="";
$fileCount=0;
$filePath=$PATH.(TEMPLATEPATH . '/'); # Specify the path you want to look in.
$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) {
if (eregi("\.php",$file)) { # Look at only files with a .php extension
$string .= "$file,<br />";
$fileCount++;
}
}
if ($fileCount > 0) {
echo sprintf($string,$fileCount);
}
?>
<?php echo $value['name']; ?>
</span>
<select style="width:170px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($file['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
<br/>
<span style="font-family:Arial, sans-serif; font-size:11px; font-weight:bold; color:#444; display:block; padding:5px 0px;">
<?php echo $value['desc']; ?>
</span>
</div>
<?php
[/code=php]
I thought I would also try and describe what im doing here, im quiet new to this, so this is code I have found around and for want of a better word am "throwing together" to learn. This is all from a open-source Wordpress theme and im trying to add some more options to it.
1. I have a functions file, which has arrays in it like below. This houses all the options and prints out in the backend of Wordpress so a user can select something. In the one below they can choose how they want their post titles to be decorated.
[code] array( "name" => "Post Title Decoration ",
"category" => "body-font-links",
"id" => $shortname."_post_title_decoration",
"type" => "select-left",
"std" => "underline",
"options" => array( "none", "underline", "overline", "line-through", "blink"),
"info" => "content or excerpt"),[/code]
2. In a seperate section of the same file, we have the handling instructions for that array. I have select, text etc. Its the select in interested in, which is layed out below.
[code]case 'select':
?>
<div style="width:250px; height:80px; float:left; background-color: #fff; padding:5px; margin:0px 0px 10px; border:1px solid #000; overflow:hidden;">
<span style="font-family:Arial, sans-serif; font-size:16px; font-weight:bold; color:#444; display:block; padding:5px 0px;">
<?php echo $value['name']; ?>
</span>
<?php if ($value['image'] != "") {?>
<div style="width:220px; padding:10px 0px; overflow:hidden;">
<img style="padding:5px; background:#FFF; border:1px solid #ddd;" src="<?php bloginfo('template_url');?>/images/<?php echo $value['image'];?>" alt="image" />
</div>
<?php } ?>
<select style="width:170px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"><?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
<br/>
<span style="font-family:Arial, sans-serif; font-size:11px; font-weight:bold; color:#444; display:block; padding:5px 0px;">
<?php echo $value['desc']; ?>
</span>
</div>
<?php
break;
3. So now the complicated part! I want to change the handling in 2, so that it does two things. A, Looks in a specific folder and lists all the .php files in that folder inside a drop down selection. B, Allows a user to select one of those (so that I can echo the set value into the index page and display that selected PHP file there.
Phew!!! I hope that made sense!!! Its basicly so a user can select from multiple layout files and choose the one which suits their needs best.
Thanks if you can help!!!
Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 4:33 pm
by andyhoneycutt
Ok, I'm curious as to where you're defining $file as an array -- You are looping through $file['options'] as $option, but nowhere do I see it declared...
Re: PHP call files into a selectable list problems
Posted: Fri Jul 10, 2009 6:26 pm
by knight42
hmm, I think thats the bit im having a problem with. As I say, im really new at PHP and feel at the moment im biting off something I cant chew. Am I even close to what I want to do? Or should I start reading some books ASAP? hehe

Re: PHP call files into a selectable list problems
Posted: Sun Jul 12, 2009 12:56 pm
by andyhoneycutt
Well, you appear to be on the right track at least in terms of the process flow. Where I see a problem is that you're trying to use an array with a key that just doesn't exist. You're on the right track. Here's my suggestion: While you're looping through the names of directories and files in your upper loop, add those results to the $file[$key] array. E.g.:
Code: Select all
$fileArray = array();
while ($file = readdir($dir)) {
if (eregi("\.php",$file)) { # Look at only files with a .php extension
$string .= "$file,<br />";
$fileCount++;
$fileArray[] = $file;
}
}