Foreach array confusion.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Foreach array confusion.

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Foreach array confusion.

Post 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>
(#10850)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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...
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Nope, same result.

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

--thanks though.

Sean!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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 : )
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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!
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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>
Last edited by akimm on Sun Oct 01, 2006 3:54 am, edited 1 time in total.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Why your select tag is closed after the submit button 8O
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Good point, I fixed that. However the php still is not doing what I need, any idea why?

Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
Post Reply