eek!not handling the array right!

They have: 461 posts

Joined: Jul 2003

so i have an associative array that i need to make into an html string.... my way of doing it before was to pop in and out of php and using a foreach to loop through... that foreach loop is in the function i'm trying to make. (there's several of them in the sign up and i need to make them into options to use in the search as well.) now my issue is that the function doesn't work right... i have a test page that's just supposed to make a string out of an array that i'm using to test it...

here's the function (carriage retun in the debug string and for the comment added to stop horizontal scroll)

<?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 the test script is getting this printout
Quote: in ato, feild name month; method $_POST; array Array
$_POST[month]
Array

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

$optlist is an array, though, not a string variable?

If you want it to be a string variable, remove the []. ??

<?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

_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

______echo \"<option value=\\"
$key\\" selected>$value</option>\";

____}else{

______echo \"<option value=\\"
$key\\">$value</option>\";

____}

__}

}
?>

Or use =. to run them all together?

They have: 461 posts

Joined: Jul 2003

i realized that.[blush]
yes i am trying to get a string. =. doesn't seem to work. that made an error.
but $optlist=$optlist."$value";
does work.. to some extent.
the default selected thing doesn't seem to work
new code:

<?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=''; # 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
<?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  dropped from here & chopped up to stop scrolling */
$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
Quote: in ato, feild name 'month'; method $_POST; array Array $_POST['month'] MonthJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember

it should have gotten the value of 12 since that's what i typed in

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I have trouble with $variable and $_POST['variable'] -- they are the same thing. Perhaps set $thismonth=$_POST['month']?

They have: 461 posts

Joined: Jul 2003

now the test file looks like

<?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>';
}
?>
the function looks like
<?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;
}
?>
and the output was
in ato, selected: 12; array: Array <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>
'

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

They have: 461 posts

Joined: Jul 2003

figured it out.. the selected value is treated as a string, so i needed == instead of ===

since i know this is useful to turn associative arrays into option code, i think i'll send it to php.net and see if they wanna add it. i'm sure those doing forms with associative arrays that want memory have already written functions, but hey, who knows...one second thought i don't see how and most problaby already have that. so... unless you people want to encourage me i'm going ot give up on that

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Glad you figured it out! Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.