Re: [PHP] Problem with functions and arrays...
On Nov 21, 2010, at 4:57 PM, Tamara Temple wrote: On Nov 20, 2010, at 5:31 PM, Jason Pruim wrote: 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. 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. //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 . ""; echo "MESSAGE: " . $message . ""; echo "POST: " . $_POST . ""; echo "OPTION: " . $option . ""; $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. ini_set("display_errors", 1); error_reporting(-1); were both on and were not complaining about anything... $sticky = $_POST["{$name}"]; echo "STICKY: " . $sticky; } //echo "OPTION: "; //print_r($option); echo << {$message} HTML; foreach ($option as $key => $value){ if($key == $sticky) { echo '' . $value . ''; }else{ echo '' . $value . ''; } } echo << 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: $startYear = date("Y", $row['startdate']); //Actual DBValue: 1265000400 You'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=62 Again, 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. 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! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions and arrays...
On 20 November 2010 23:31, Jason Pruim wrote: > Hey Everyone! > > So I came across a problem that I don't know how to fix... I have searched > and thought and just not having anything click as to where I am messing > up... > > I have a few functions as follows: > > function ddbYear($name, $message, $_POST, $option){ > > //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 . ""; > echo "MESSAGE: " . $message . ""; > > echo "POST: " . $_POST . ""; > echo "OPTION: " . $option . ""; > > > $sticky = ''; > if(isset($_POST['submit'])) { > $sticky = $_POST["{$name}"]; > echo "STICKY: " . $sticky; > } > //echo "OPTION: "; > //print_r($option); > > echo << > {$message} > > HTML; > > foreach ($option as $key => $value){ > > if($key == $sticky) { > echo '' . $value . ''; > }else{ > echo '' . $value . ''; > } > > } > > echo << > > 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: > $startYear = date("Y", $row['startdate']); //Actual DBValue: 1265000400 > $optionYear = array("2010" => "2010", "2011" => "2011", "2012" => "2012", > "2013" => "2013", "2014" => "2014"); > > ddbYear("startYear", "Select Year", $startYear, $optionYear); > > > ?> > > 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=62 > > Thanks for looking and for your answers in advance! :) > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > It is extremely counter-intuitive to have parameters named after superglobal variables. Rather than doing nothing, any parameter passed to it will be assigned to the super global $_POST array. There is no "pass be reference" &$_POST which would be one way to alter a variable that exists outside of the scope of the function. If your intention _IS_ to assign to the $_POST super-global, then just assign it. Don't pass it. Using any super global as a parameter is almost always NOT going to have the intent required. Things get really quite odd if the parameter is $GLOBALS. The output here is ... array(1) { ["Name"]=> string(7) "Richard" } Notice: Undefined variable: Name in Z:\testsg.php on line 12 43 So, the link between the global variables and $GLOBALS super global is now broken. $Name isn't in global scope and $Age isn't in $GLOBALS. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions and arrays...
On Nov 20, 2010, at 5:31 PM, Jason Pruim wrote: 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. //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 . ""; echo "MESSAGE: " . $message . ""; echo "POST: " . $_POST . ""; echo "OPTION: " . $option . ""; $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. $sticky = $_POST["{$name}"]; echo "STICKY: " . $sticky; } //echo "OPTION: "; //print_r($option); echo << {$message} HTML; foreach ($option as $key => $value){ if($key == $sticky) { echo '' . $value . ''; }else{ echo '' . $value . ''; } } echo << 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: $startYear = date("Y", $row['startdate']); //Actual DBValue: 1265000400 You'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=62 Again, 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php