Page 1 of 2

Foreach array confusion.

Posted: Sun Oct 01, 2006 12:17 am
by akimm
So I have this arrray which will glob() through my directory, and print all contents with .php extensions. basically I want to use this as an admin. panel, all files in this directory will have admin. properties.

Code: Select all

<select name="admin">
<?php
$files = glob("admin/*.php");
foreach ($files as $key => $value){
?>
<form method=post action=<?php $_SERVER['PHP_SELF'];?>>
<B>Choose from the list below</B><br><SELECT name=link size=16>
<OPTION VALUE ="<?php echo $key;?>"><?php echo $key;?>></OPTION>
<?php
}
	?>
	</select>
I also tried

Code: Select all

<select name="admin">
<?php
$files = glob("admin/*.php");
foreach ($files as $key){
?>
<form method=post action=<?php $_SERVER['PHP_SELF'];?>>
<B>Choose from the list below</B><br><SELECT name=link size=16>
<OPTION VALUE ="<?php echo $key;?>"><?php echo $files;?>></OPTION>
<?php
}
	?>
	</select>
And most possibilities in-between.

Re: Foreach array confusion.

Posted: Sun Oct 01, 2006 2:08 am
by Christopher
I am guessing that the generated HTML is messed up. You have a select outside and inside the loop and the form inside. Maybe something like this:

Code: Select all

<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<b>Choose from the list below</b>
<br/>
    <select name="link">
<?php
$files = glob("admin/*.php");
foreach ($files as $key => $value) {
?>
        <option value="<?php echo $key;?>"><?php echo $key;?>></option>
<?php
}
	?>
    </select>
</form>

Posted: Sun Oct 01, 2006 2:20 am
by akimm
Thanks buddy, I'll be checking that out within the next few minutes. I was wondering where that went wrong hehe. most of my codes do go wrong at least at first...

Posted: Sun Oct 01, 2006 2:26 am
by akimm
Nope, same result.

It basically prints a blank select menu, not sure why yet though..

--thanks though.

Sean!

Posted: Sun Oct 01, 2006 2:50 am
by Christopher
If it prints a blank select then the "$files = glob("admin/*.php");" is not returning anything to $files. Probably a path or permission problem.

Posted: Sun Oct 01, 2006 2:57 am
by akimm
I would agree, but I can print from that directory no prob outside of this select script.

I have tried to glob them and make just a list of the uri's but I would like to make it in this format, because, it looks attractive, at least more-so than in another.

If you would like, I will print the contents of admin/ with a diff script, just to show you there is stuff there, and i do indeed have control over it, permissions wise everything is good : )

Posted: Sun Oct 01, 2006 3:04 am
by akimm
I know exactly what is wrong now. I made a slight oversight, one you would not have been able to forsee.

When writing this code, I first had it in my guestbook1 directory, I then modified the code to make it a dropdown, and in doing so also moved it into the admin directory hehe. So when you call for something in the directory we didn't need to specify admin/

Thank you for the help.

Sean!

Posted: Sun Oct 01, 2006 3:08 am
by akimm
Hmm, after testing it still doesn't act right..

I've tried with this, this file is in the same directory as the files i'm looking for.

Code: Select all

<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<b>Choose from the list below</b>
<br/>
    <select name="link">
<?php
$files = glob("/*.php");
foreach ($files as $key => $value) {
?>
        <option value="<?php echo $value;?>"><?php echo $key;?>></option>
<?php
}
        ?>
    </select>
</form>
I've also tried this

Code: Select all

<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<b>Choose from the list below</b>
<br/>
    <select name="link">
<?php
$files = glob("*.php");
foreach ($files as $key => $value) {
?>
        <option value="<?php echo $value;?>"><?php echo $key;?>></option>
<?php
}
        ?>
    </select>
</form>
Also to no avail.

Posted: Sun Oct 01, 2006 3:15 am
by Christopher
Well ... "/*.php" is the root directory and "*.php" is the current directory. It sounds like it isn't in either of those two (or "admin/*.php"). Keep trying.

Posted: Sun Oct 01, 2006 3:18 am
by akimm
Ok nevermind, I now have this solved.

Thanks for the help!

Code: Select all

<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<b>Choose from the list below</b>
<br/>
    <select name="link">
<?php
$files = glob("/*.php");
foreach ($files as $key => $value) {
?>
        <option value="<?php echo $value;?>"><?php echo $value;?></option>
<?php
}
        ?>
    </select>
</form>

Posted: Sun Oct 01, 2006 3:37 am
by akimm
I have a similar, but new problem. Basically now that we got control of that loop, I want it to create links to each of the files.
I have this so far, the only instance its worked is when I wrote the submit within the loop, which caused the one file, the first of the loop to print in the option, then the rest printed outside, yes as links and they did work, but not as expected, or needed. I only need them to work within the select box.

Code: Select all

<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<b>Choose from the list below</b>
<br/>
    <select name="link">
<?php
#if  arborint view this, I moved the file back guestbook1/ so to make it easier  
$files = glob("admin/*.php");
foreach ($files as $key => $value) {
?>
        <option value="<?php echo "<a href=" .  $key . ">" . $key . "</a>";?>"><?php echo "<a href=" .  $value . ">" . $value . "</a>"; ?></option>
<?php
}
        ?>
</select>
<input type="submit" value="go to!" name="submit">
  
</form>

Posted: Sun Oct 01, 2006 3:50 am
by miro_igov
Why your select tag is closed after the submit button 8O

Posted: Sun Oct 01, 2006 3:53 am
by akimm
Good point, I fixed that. However the php still is not doing what I need, any idea why?

Thanks!

Posted: Sun Oct 01, 2006 4:43 am
by volka
You have a/href elements in your option elements? Why?

This script will output "html" like
<option value="<a href=0>0</a>"><a href=admin/a.php>admin/a.php</a></option>
<option value="<a href=1>1</a>"><a href=admin/b.php>admin/b.php</a></option>
and that a bit ...strange.

Posted: Sun Oct 01, 2006 4:46 am
by akimm
:oops:

I am trying to make the select menu allow the different selections to be different links. It is the begining of a admin panel.