Re: Problem with functions and arrays...
| From: | Jason Pruim | Date: | Mon, 22 Nov 2010 20:33:29 +0000 |
| Subject: | Re: Problem with functions and arrays... | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309618@lists.php.net to get a copy of this message | ||
On Nov 21, 2010, at 4:57 PM, Tamara Temple wrote:
On Nov 20, 2010, at 5:31 PM, Jason Pruim wrote:It was there as a hold over from when I originally made the functions which just had to deal with getting variables from the $_POST global.<?PHP function ddbYear($name, $message, $_POST, $option){Maybe it's just me, but using the name of a global as a function parameter just seems like a bad idea. Yes, you can do it. Should you? I think not. Especially, as, you are passing it a scalar below and treating it here like the global array.
ini_set("display_errors", 1); error_reporting(-1); were both on and were not complaining about anything...//Make sure to post form start/stop OUTSIDE of this function... //It's not meant to be a one size fits all function! echo "NAME: " . $name . "<BR>"; echo "MESSAGE: " . $message . "<BR>"; echo "POST: " . $_POST . "<BR>"; echo "OPTION: " . $option . "<BR>"; $sticky = ''; if(isset($_POST['submit'])) {Check the error messages -- since you're passing in $startYear as a scalar below, you shouldn't be able to access $_POST as an associative array.
Turns out it was a conflict with the $_POST global.. Or my misunderstanding inside the functions... I changed that to another name and now it works fine... Thanks Tamara!$sticky = $_POST["{$name}"]; echo "STICKY: " . $sticky; } //echo "OPTION: "; //print_r($option); echo <<<HTML <select name="{$name}"> <option value="0">{$message}</option> HTML; foreach ($option as $key => $value){ if($key == $sticky) { echo '<option value="' . $key .'" selected>' . $value . '</option>'; }else{ echo '<option value="' . $key .'">' . $value . '</option>'; } } echo <<<HTML </select> HTML; unset($value); return; } ?> One for Month, Day & Year... All the same exact code... When I get brave I'll combine it into 1 functions :) Now... What it's trying to do.. It's on a "update" form on my website. Basically pulls the info from the database and displays it in the form again so it can be edited and resubmitted... As I'm sure you can tell from the function it checks to see if the a value has been selected in the drop down box and if it has then set the drop down box to that value. I call the function like this: <?PHP $startYear = date("Y", $row['startdate']); //Actual DBValue: 1265000400You're setting $startYear as a scalar value here.$optionYear = array("2010" => "2010", "2011" => "2011", "2012" => "2012", "2013" => "2013", "2014" => "2014"); ddbYear("startYear", "Select Year", $startYear, $optionYear);And passing it in to the $_POST variable in your function. Then you treat the $_POST variable as an associative array (seemingly much like the global $_POST array).?> The output I'm getting is: THESE ARE THE ACTUAL UNPROCESSED (OTHER THEN SEPARATING) VALUES FROM THE DATABASE startmonth: 2 startday: 1 startYear: 2010 endmonth: 11 endDay: 30 endYear: 1999 THESE ARE THE VALUES INSIDE THE FUNCTION NAME: startYear MESSAGE: Select Year POST: 2010 OPTION: Array STICKY: 2 Now... The problem is that $sticky get set to "2" instead of "2010"... But I can't figure out why... Anyone have any ideas? And just incase I didn't provide enough info here's a link that shows it happening: HTTP://jason.pruimphotography.com/dev/cms2/events/update_form.php?id=62Again, I will reiterate that taking the name of a global variable and using it as a parameter in a function is a bad idea. It can be done, and my in some cases have some uses, but I don't think the way you're using it is a good idea, and looks wrong to me as well.