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: ?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. 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 . 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. 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 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: 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 Nov 20, 2010, at 5:31 PM, Jason Pruim wrote: ?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. //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. $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: 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
RE: [PHP] problem calling functions
2007. 10. 19, péntek keltezéssel 11.33-kor Jay Blanchard ezt írta: [snip] ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? [/snip] I don't think you can put a function name in a variable and call it like $function($var). why not? http://www.php.net/manual/en/functions.variable-functions.php greets Zoltán Németh You'd be better of with a case statement in one function and call the proper solution (quick syntax, may need a little fixing; function my_solution($function, $var){ switch $function{ case function1: ...do stuff... break; case function1: etc. } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
On 19/10/2007, afan pasalic [EMAIL PROTECTED] wrote: Robin Vickery wrote: On 19/10/2007, afan pasalic [EMAIL PROTECTED] wrote: hi I have a problem with calling functions: ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? suggestions? suggestions for what? What is your problem? If you set $function to 'solution1' and run your code, it will indeed execute solution1(). -robin the problem is that this code doesn't work. and I was asking where is the problem, or can I do it this way at all. Firstly, the code you posted does indeed work fine. Secondly, you need to learn to ask questions properly: What do you expect the code to do? What is it actually doing? Are there any error messages, if so what are they? -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
Jay Blanchard wrote: I don't think you can put a function name in a variable and call it like $function($var). Yes you can - it's basically the same as variable variables. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
yup! it works perfect. obviously, it's MY fault. :-) thanks stut -afan Stut wrote: afan pasalic wrote: actually, what example you are talking about? I got jay's example only? http://dev.stut.net/php/varfunc.php -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
afan pasalic wrote: actually, what example you are talking about? I got jay's example only? http://dev.stut.net/php/varfunc.php -Stut -- http://stut.net/ Stut wrote: afan pasalic wrote: why then the code doesn't work? the error I'm getting is: Fatal error: Call to undefined function solution1() in ... even the function itself is just above the line that calls function? I can only guess that you're not showing us the code you're actually running. See the example I put in another reply - that works and is based on the code you sent. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
why then the code doesn't work? the error I'm getting is: Fatal error: Call to undefined function solution1() in ... even the function itself is just above the line that calls function? -afan Stut wrote: Jay Blanchard wrote: I don't think you can put a function name in a variable and call it like $function($var). Yes you can - it's basically the same as variable variables. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
Jay Blanchard wrote: [snip] [snip] ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? [/snip] I don't think you can put a function name in a variable and call it like $function($var). You'd be better of with a case statement in one function and call the proper solution (quick syntax, may need a little fixing; function my_solution($function, $var){ switch $function{ case function1: ...do stuff... break; case function1: etc. } } [/snip] And call it like this; my_solution('function1', $var); actually, I did a little bit different: switch($function) { case 'solution1': solution1($var1); break; case 'solution2': solution2($var2); break; case 'solution3': solution3($var3); break; } ;-) -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
Robin Vickery wrote: On 19/10/2007, afan pasalic [EMAIL PROTECTED] wrote: hi I have a problem with calling functions: ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? suggestions? suggestions for what? What is your problem? If you set $function to 'solution1' and run your code, it will indeed execute solution1(). -robin the problem is that this code doesn't work. and I was asking where is the problem, or can I do it this way at all. :-) -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] problem calling functions
[snip] [snip] ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? [/snip] I don't think you can put a function name in a variable and call it like $function($var). You'd be better of with a case statement in one function and call the proper solution (quick syntax, may need a little fixing; function my_solution($function, $var){ switch $function{ case function1: ...do stuff... break; case function1: etc. } } [/snip] And call it like this; my_solution('function1', $var); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
On 19/10/2007, afan pasalic [EMAIL PROTECTED] wrote: hi I have a problem with calling functions: ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? suggestions? suggestions for what? What is your problem? If you set $function to 'solution1' and run your code, it will indeed execute solution1(). -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
Stut wrote: afan pasalic wrote: why then the code doesn't work? the error I'm getting is: Fatal error: Call to undefined function solution1() in ... even the function itself is just above the line that calls function? I can only guess that you're not showing us the code you're actually running. See the example I put in another reply - that works and is based on the code you sent. -Stut true. it's not original code than simplified one. let me try with one more time with your solution. -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] problem calling functions
[snip] ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? [/snip] I don't think you can put a function name in a variable and call it like $function($var). You'd be better of with a case statement in one function and call the proper solution (quick syntax, may need a little fixing; function my_solution($function, $var){ switch $function{ case function1: ...do stuff... break; case function1: etc. } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
2007. 10. 19, péntek keltezéssel 11.15-kor afan pasalic ezt írta: hi I have a problem with calling functions: ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? what's wrong with this? greets Zoltán Németh suggestions? thanks. -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
Jay Blanchard wrote: [snip] ?php function solution1($var1){ // some code } function solution2($var2){ // some code } function solution3($var3){ // some code } if ($function == 'solution1' or $function == 'solution2' or $function == 'solution3') { $my_solution = $function($var); # this supposed to call one of solution functions, right? } ? [/snip] I don't think you can put a function name in a variable and call it like $function($var). You'd be better of with a case statement in one function and call the proper solution (quick syntax, may need a little fixing; function my_solution($function, $var){ switch $function{ case function1: ...do stuff... break; case function1: etc. } } that's exactly what I'm doing now. though, I was thinking if it's possible. :-( thanks jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
afan pasalic wrote: why then the code doesn't work? the error I'm getting is: Fatal error: Call to undefined function solution1() in ... even the function itself is just above the line that calls function? I can only guess that you're not showing us the code you're actually running. See the example I put in another reply - that works and is based on the code you sent. -Stut -- http://stut.net/ Stut wrote: Jay Blanchard wrote: I don't think you can put a function name in a variable and call it like $function($var). Yes you can - it's basically the same as variable variables. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem calling functions
actually, what example you are talking about? I got jay's example only? Stut wrote: afan pasalic wrote: why then the code doesn't work? the error I'm getting is: Fatal error: Call to undefined function solution1() in ... even the function itself is just above the line that calls function? I can only guess that you're not showing us the code you're actually running. See the example I put in another reply - that works and is based on the code you sent. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions
Please show a bit more of the actual code - Original Message - From: Beauford.2002 [EMAIL PROTECTED] To: PHP General [EMAIL PROTECTED] Sent: Friday, December 20, 2002 9:55 AM Subject: [PHP] Problem with functions Hi, I keep getting errors in my script that says 'x' function is undefined or 'y' function is undefined. I defined it as 'function test ($a, $b)' and call it using 'test($a, $b)' From my knowledge this is correct, but it just simply doesn't work. Anyone have any ideas on this? TIA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problem with functions
Hi, I keep getting errors in my script that says 'x' function is undefined or 'y' function is undefined. I defined it as 'function test ($a, $b)' and call it using 'test($a, $b)' You need to post code :-) Try this: ?php function test ($a, $b) { echo I am the test functionbr /; echo a is $abr /; echo b is $bbr /; } test(1, 2); ? Cheers Jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions
Jon, You may have answered my question, but I'm still confused. I see from your example that the actual function comes before the function is called in the script, and when I changed mine to that format it worked. Now the confusion. I have another script which is the opposite of your example, and works fine. So why would one work one way but not the other. I have included the exact format I have for the one that doesn't work, and below that the actual code of the one that does work. In most languages I have used, this is the correct format - it makes for easier reading. ?PHP (this is the one that does not work) include (errormessages.php); $error = ; if (!$name) { errormessage(); } elseif ($anothercondition) { gotofunction(); } elseif ($anothercondition2) { gotofunction2(); } elseif ($anothercondition3) { gotofunction3(); } function errormessage() { code ... code ... } function gotofunction() { code ... code ... } function gotofunction2() { code ... code ... } function gotofunction3() { code ... code ... } ? ?PHP $ipexists = checkforduplicates(); switch (true) { case (!$name): showentries(); break; case (!$ipexists): writerecord($date, $name, $email, $comment); break; case ($ipexists): duplicatemessage(); break; } function checkforduplicates() { code ...; } showentries() { code ...; } writerecord($date, $name, $email, $comment) { code ...; } duplicatemessage() { code ...; } ? - Original Message - From: Jon Haworth [EMAIL PROTECTED] To: 'Beauford.2002' [EMAIL PROTECTED]; PHP General [EMAIL PROTECTED] Sent: Friday, December 20, 2002 11:05 AM Subject: RE: [PHP] Problem with functions Hi, I keep getting errors in my script that says 'x' function is undefined or 'y' function is undefined. I defined it as 'function test ($a, $b)' and call it using 'test($a, $b)' You need to post code :-) Try this: ?php function test ($a, $b) { echo I am the test functionbr /; echo a is $abr /; echo b is $bbr /; } test(1, 2); ? Cheers Jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions
On Saturday 21 December 2002 03:27, Beauford.2002 wrote: Jon, You may have answered my question, but I'm still confused. I see from your example that the actual function comes before the function is called in the script, and when I changed mine to that format it worked. Now the confusion. I have another script which is the opposite of your example, and works fine. So why would one work one way but not the other. I have included the exact format I have for the one that doesn't work, and below that the actual code of the one that does work. In most languages I have used, this is the correct format - it makes for easier reading. PHP looks at the whole file before doing anything. So it doesn't matter where in the code you define your function. I usually put all my functions at the end so they don't get in the way of the main loop. You must have some other problem. Crank up the error reporting and look at the error log. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* It's clever, but is it art? */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problem with functions
? $username = victor; function test() { echo $GLOBALS[$username]; ? Carlos Alberto Pinto Hurtado IT ICA (57 1) 2322181 (57 1) 2324698 Movil.(57 3) 310 6184251 -Mensaje original- De: Victor Halla [mailto:[EMAIL PROTECTED]] Enviado el: Wednesday, December 04, 2002 6:16 PM Para: [EMAIL PROTECTED] Asunto: [PHP] Problem with functions Hi, I have de following code for example ?php $username = victor; function test() { echo $username; } ? If I call the funcion test(), echo print nothing what's is going on ? []´s [EMAIL PROTECTED] __ Victor Halla ICQ#: 114575440 Current ICQ status: + More ways to contact me __ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions
Hi, Thursday, December 5, 2002, 9:15:53 AM, you wrote: VH Hi, VH I have de following code for example VH ?php VH $username = victor; VH function test() { VH echo $username; VH } ? VH If I call the funcion test(), echo print nothing what's is going on ? VH []´s VH [EMAIL PROTECTED] VH __ Victor VH Halla ICQ#: 114575440 Current ICQ status: + More ways to contact me VH __ You need to tell the function to use the global variable like so: function test() { global $username; echo $username; } -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with functions
Thanks !!! Victor Tom Rogers [EMAIL PROTECTED] escreveu na mensagem [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi, Thursday, December 5, 2002, 9:15:53 AM, you wrote: VH Hi, VH I have de following code for example VH ?php VH $username = victor; VH function test() { VH echo $username; VH } ? VH If I call the funcion test(), echo print nothing what's is going on ? VH []´s VH [EMAIL PROTECTED] VH __ Victor VH Halla ICQ#: 114575440 Current ICQ status: + More ways to contact me VH __ You need to tell the function to use the global variable like so: function test() { global $username; echo $username; } -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php