PHP dropdown menu option name help please

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
beazleybub
Forum Newbie
Posts: 5
Joined: Mon Dec 14, 2009 11:03 am

PHP dropdown menu option name help please

Post 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>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP dropdown menu option name help please

Post 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'.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
beazleybub
Forum Newbie
Posts: 5
Joined: Mon Dec 14, 2009 11:03 am

Re: PHP dropdown menu option name help please

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP dropdown menu option name help please

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
beazleybub
Forum Newbie
Posts: 5
Joined: Mon Dec 14, 2009 11:03 am

Re: PHP dropdown menu option name help please

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