Page 1 of 1

PHP dropdown menu option name help please

Posted: Thu Apr 01, 2010 9:06 am
by beazleybub
Hi, I'm a noob when it comes to PHP and I'm learning but I'm stuck so please be kind to me.

I have a PHP dropdown menu that is populating using an array and it's working fine but I'd like to use a regular name as an option instead of the array name.

Here's the code. Could someone please show me how to display "test.php" instead of the path "pagedata/test.php". Thanks

Code: Select all

<?php
$dataFiles=array("pagedata/test.php","pagedata/data2.php","pagedata/data3.php","pagedata/data4.php","pagedata/data5.php","pagedata/data6.php","end_of_array");
for($i=0;$i<count($dataFiles)-1;$i++)
{
 if(!file_exists($dataFiles[$i]))
 {
  $fp=fopen($dataFiles[$i],"w");
  fwrite($fp," ");
  fclose($fp);
  }
 else
 {
  if(filesize($dataFiles[$i])<=0)
  {
   $fp=fopen($dataFiles[$i],"a");
   fwrite($fp," ");
   fclose($fp);
   }
  }
 }
$file=empty($_POST["source"])?"pagedata/test.php":$_POST["source"];//gg
if(!empty($_POST["current_file"]))
{
 $data=empty($_POST['data'])?" ":$_POST['data'];
 $file=$_POST["current_file"];
 $fh=fopen($file,"w");
 fwrite($fh,$data);
 fclose($fh);
 }
if(file_exists($file))
{
 $fh=fopen($file,'r');
 $data=stripslashes(fread($fh,filesize($file)));
 fclose($fh);
 }
?>
<div style="float:left;padding:5px 0 5px 0">
<form name="view_form" action="#" method="POST">
<select name="source" onchange="view_form.submit();">
<?php
  for($i=0;$i<count($dataFiles);$i++)
  {
   $selected="";
   if($dataFiles[$i]!="end_of_array")
   {
    if($file==$dataFiles[$i]){$selected="selected";}
    echo "<option value='$dataFiles[$i]' $selected>$dataFiles[$i]</option>";
    }
   }
  ?>
</select></form></div>					
<form action="#" method="post">
<input type="hidden" name="current_file" value="<?php echo $file; ?>"></form>

Re: PHP dropdown menu option name help please

Posted: Thu Apr 01, 2010 1:17 pm
by AbraCadaver
In this case, the easiest would be:

Code: Select all

echo "<option value='$dataFiles[$i]' $selected>" . basename($dataFiles[$i]) . "</option>";
But this only works because it's a file path. For other scenarios you could use an associative array:

Code: Select all

$dataFiles = array("pagedata/test.php" => "test.php"); // etc...
// more code...

foreach($dataFiles as $value => $text) {
    echo "<option value='$value' $selected>$text</option>";
}
I recommend always using foreach for looping arrays. Much easier, you don't have to count the values or maintain a counter. Also, I don't see any need for that 'end_of_array'.

Re: PHP dropdown menu option name help please

Posted: Sat Apr 03, 2010 12:47 am
by beazleybub
Thanks for your reply. There's a good reason the "end of array" is there. :) You're just not seeing all of the code. I have tried learning about associative arrays. Why don't you think the code I provided could use it? Would it really be that hard to add to my existing code?

Re: PHP dropdown menu option name help please

Posted: Sat Apr 03, 2010 11:40 am
by AbraCadaver
beazleybub wrote:Thanks for your reply. There's a good reason the "end of array" is there. :) You're just not seeing all of the code. I have tried learning about associative arrays. Why don't you think the code I provided could use it? Would it really be that hard to add to my existing code?
I didn't say that you're code couldn't use it. I gave an easy fix and also a different way of doing it.

Re: PHP dropdown menu option name help please

Posted: Sat Apr 03, 2010 3:56 pm
by beazleybub
Hi,

Your basename suggestion will work ok.

I was not implying you said an associative array would not work intentionally. When I answered your post I had just waken up and was a bit goofy.
I have seen associative array examples searching the web and investigated their mechanics. I'm just not sure how difficult it would be for me to work it into the code I already have.

Thanks for helping me out. Have a good Easter weekend.