eek! it's not doing somethin correctly! (converting an array

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

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

eek! it's not doing somethin correctly! (converting an array

Post by m3rajk »

Code: Select all

<?php
morphiglobe: function array_to_options($array, $method, $feild_name){
  echo "in ato, feild name $feild_name; method $method; array $array  $method[$feild_name] "; # debug
  $optlist=array(); # make an empty array
  foreach($array as $key=>$value){
    if($method[$feild_name]==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist[]="<option value="$key" selected>$value</option>;
    }else{
      $optlist[]="<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
?>
the output i'm getting from the test file....
in ato, feild name month; method $_POST; array Array
$
Array
the test file has an extra line to print out the final version of the array.
test file (give a number between 00 and 12) http://24.91.157.113/findyourdesire/php ... sttest.php
test file

Code: Select all

[joshua@Ashes joshua]$ cat fyd/phpTest/optlisttest.php
<?php
include("/home/joshua/includes/fyd.incs.php"); # includes file
$month=$_POST['month'];
if(!isset($month)){
  echo <<<END
    <html><head></head><body>
    <form action="$_SERVER[PHP_SELF]" method="post">
    <input type="text" name="month">
    <input type="submit" value="check">
    </form>
    </body></html>
END;
}else{
  $array=array('00'=>'Month', '01'=>'January', '02'=>'February', '03'=>'March', '04'=>'April', '05'=>'May', '06'=>'June', '07'=>'July', '08'=>'August', '09'=>'September', '10'=>'October', '11'=>'November', '12'=>'December');
  $method='$_POST';
  $feild_name='month';
  $list=array_to_options($array, $method, $feild_name);
  echo $list;
}
?>
on a somewhat related note, a diff file is haveing an issue with uploading files, anyone know how filetypes are set up? i can't find it in my book., i know it's part of the array but not how it reports them, so i don't know if the error is the file or the check i have
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

I think

{$$method}[$feildname]

might be what you are after. You are certainly looking at variable variables

$var = 'foo';
$$var = 'bar';
echo $foo;

so $$method would be the same as writing $_POST - you'd want that in braces though as it is an array {$$method} and needs to be evaluated prior to the index.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

the optlist creation is now.....

Code: Select all

<?php
function array_to_options($array, $method, $feild_name){
  echo "in ato, feild name $feild_name; method $method; array $array <br /> {$$method}[$feild_name] <br />"; # debug
  $optlist=array(); # make an empty array
  foreach($array as $key=>$value){
    if($method[$feild_name]==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist[]="<option value="$key" selected>$value</option>";
    }else{
      $optlist[]="<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
?>
and that gets....
in ato, feild name month; method $_POST; array Array
[month]
Array
in response. obviously i know now why the array is empty. gonna try without your extra $ and report back
reporting back...

Code: Select all

<?php
function array_to_options($array, $method, $feild_name){
  echo "in ato, feild name $feild_name; method $method; array $array <br /> {$$method}[$feild_name] <br />"; # debug
  $optlist=array(); # make an empty array
  foreach($array as $key=>$value){
    if($method[$feild_name]==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist[]="<option value="$key" selected>$value</option>";
    }else{
      $optlist[]="<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
?>
gets
in ato, feild name month; method $_POST; array Array
$_POST[month]
Array
slightly better... now how do i get it to put together the array based on it??? obviously i am getting close.

btw: i named the function and feilds like i did because i can see others wanting to use this and since there's nothing and i found this trick useful n making a page with options and memory, i figure i might as well send it into php.net since i need to make it anyway so i can call it from two pages... one the sign up and one searching since i use the same arrays and this is how i'm turning them into options
edit:
:oops: :oops: :oops: :oops: :oops:
i realized that i am trying to get a string. so i made some changes, but....
the default selected thing doesn't seem to work
new code:

Code: Select all

function array_to_options($array, $method, $feild_name){
  echo "in ato, feild name $feild_name; method $method; array $array <br /> {$method}[$feild_name] <br />"; # debug
  $optlist=''; # make an empty string
  foreach($array as $key=>$value){
    if($method[$feild_name]==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist=$optlist."<option value="$key" selected>$value</option>";
    }else{
      $optlist=$optlist."<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
and

Code: Select all

<?php
include("/home/joshua/includes/fyd.incs.php"); # includes file
$month=$_POST['month'];
if(!isset($month)){
  echo <<<END
    <html><head></head><body>
    <form action="$_SERVER[PHP_SELF]" method="post">
    <input type="text" name="month">
    <input type="submit" value="check">
    </form>
    </body></html>
END;
}else{
  $array=array('00'=>'Month', '01'=>'January', '02'=>'February', '03'=>'March', '04'=>'April', '05'=>'May', '06'=>'June', '07'=>'July', '08'=>'August', '09'=>'September', '10'=>'October', '11'=>'November', '12'=>'December');
  $method='$_POST';
  $feild_name="'month'";
  $list=array_to_options($array, $method, $feild_name);
  echo '<select name="test" size="1">'.$list.'</select>';
}
?>
generates
in ato, feild name 'month'; method $_POST; array Array <br /> $_POST['month'] <br /><select name="test" size="1"><option value="00">Month</option><option value="01">January</option><option value="02">February</option><option value="03">March</option><option value="04">April</option><option value="05">May</option><option value="06">June</option><option value="07">July</option><option value="08">August</option><option value="09">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select>
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

just overflew your last lines:

if $month is not set, you will not echo html header nor footer tags. dunno whether thats an error.

and how about $method='$_POST["something"]';
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

this site was down last night whne i figured it out. turned out that the === should be == since it was treating the input as a string, not a a number.

actually, i use this on a page that the first time i don't want anything set to be selected.what do you mean by html header and footer tags? do you mean <html> and </html>?

the first time the page is created i just want the list as it's appearing.

the final code i'm using is....

Code: Select all

<?php
function array_to_options($array, $selected){
  echo "in ato, selected: $selected; array: $array <br />"; # debug
  $optlist=''; # make an empty string
  foreach($array as $key=>$value){
    if($selected==$key){
 # if this is the first time it wont match, so we don't need to care if it's set
      $optlist=$optlist."<option value="$key" selected>$value</option>";
    }else{
      $optlist=$optlist."<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
?>
Post Reply