[PHP] Re: Switch Statement
On 9/28/2013 10:33 PM, Ethan Rosenberg wrote: Dear List - I have a working program. I made one change in a switch statement, and it does not work. I'm probably missing something fundamental. Here are some code SNIPPETS... [please note that all my debug statements are at the left margin] Setup... ?php session_start(); session_name(STORE); set_time_limit(2400); ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(-2); ini_set('error_reporting', 'E_ALL | E_STRICT'); ini_set('html_errors', 'On'); ini_set('log_errors', 'On'); require '/home/ethan/P/wk.inc'; //password file $db = Store; $cxn =mysqli_connect($host,$user,$password,$db); if (!$cxn) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); }// no error if($_REQUEST['welcome_already_seen']!= already_seen) show_welcome(); //end setup function show_welcome() //this is the input screen { snip echo input type='hidden' name='welcome_already_seen' value='already_seen'; echo input type='hidden' name='next_step' value='step20' /; snip } //end input screen //Switch statement echo 'before'; print_r($_POST); //post#1 switch ( $_POST['next_step'] ) { case 'step20': { pint_r($_POST);//post#2 echo 'step20'; if(!empty($_POST['Cust_Num'])) good(); if(empty($_POST['Cust_Num'])) bad(); break; } //end step20 snip } //end switch post#1 beforeArray ( [Cust_Num] = 123 [Fname] = [Lname] = [Street] = [City] = [state] = NY [Zip] = 10952 [PH1] = [PH2] = [PH3] = [Date] = [welcome_already_seen] = already_seen [next_step] = step20 ) Cust_Num state and Zip are as entered. The switch statement is never entered, since post#2 is never displayed, and neither good() or bad() functions are entered. TIA Ethan Once again you are posting code that has no chance of running. And since you are DISABLING error reporting with that -2 value you won't even know you have bad code. Try again. Post#2 will never display since you aren't printing it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch Statement
?php session_start(); session_name(STORE); set_time_limit(2400); ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(-2); ini_set('error_reporting', 'E_ALL | E_STRICT'); ini_set('html_errors', 'On'); ini_set('log_errors', 'On'); Ethan,Ethan,Ethan - what is all this stuff you have at the top??? Do you know how any of this is supposed to be written? You can not put Constants in quotes - they become just plain strings then, not Constants with the predefined values you (and the functions) are expecting. For example, 'on' is NOT the same as the use of the word : on. And your error_reporting setting (which you are attempting to do TWICE) is actually causing your script to NOT show any errors, which is preventing you from seeing that your script dies at the misspelled print statement and never gets to the pair of if statements that should call your good and bad functions. Hate to do this to you, but you've been attempting to pick up PHP for two years now almost and from this latest post you appear to not have learned anything. And WHY would you EVER want to have a time limit of 2400 seconds??? And stop burying functions in the middle of your straight line code. It's ridiculous and makes reading your scripts a royal PIA. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch Statement
On 09/28/2013 11:59 PM, Jim Giner wrote: ?php session_start(); session_name(STORE); set_time_limit(2400); ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(-2); ini_set('error_reporting', 'E_ALL | E_STRICT'); ini_set('html_errors', 'On'); ini_set('log_errors', 'On'); Ethan,Ethan,Ethan - what is all this stuff you have at the top??? Do you know how any of this is supposed to be written? You can not put Constants in quotes - they become just plain strings then, not Constants with the predefined values you (and the functions) are expecting. For example, 'on' is NOT the same as the use of the word : on. And your error_reporting setting (which you are attempting to do TWICE) is actually causing your script to NOT show any errors, which is preventing you from seeing that your script dies at the misspelled print statement and never gets to the pair of if statements that should call your good and bad functions. Hate to do this to you, but you've been attempting to pick up PHP for two years now almost and from this latest post you appear to not have learned anything. And WHY would you EVER want to have a time limit of 2400 seconds??? And stop burying functions in the middle of your straight line code. It's ridiculous and makes reading your scripts a royal PIA. Jim - Thanks. Changed error_reporting to -1. No error messages. No change in output. Ethan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch Statement
On 9/29/2013 1:29 AM, Ethan Rosenberg wrote: On 09/28/2013 11:59 PM, Jim Giner wrote: ?php session_start(); session_name(STORE); set_time_limit(2400); ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(-2); ini_set('error_reporting', 'E_ALL | E_STRICT'); ini_set('html_errors', 'On'); ini_set('log_errors', 'On'); Ethan,Ethan,Ethan - what is all this stuff you have at the top??? Do you know how any of this is supposed to be written? You can not put Constants in quotes - they become just plain strings then, not Constants with the predefined values you (and the functions) are expecting. For example, 'on' is NOT the same as the use of the word : on. And your error_reporting setting (which you are attempting to do TWICE) is actually causing your script to NOT show any errors, which is preventing you from seeing that your script dies at the misspelled print statement and never gets to the pair of if statements that should call your good and bad functions. Hate to do this to you, but you've been attempting to pick up PHP for two years now almost and from this latest post you appear to not have learned anything. And WHY would you EVER want to have a time limit of 2400 seconds??? And stop burying functions in the middle of your straight line code. It's ridiculous and makes reading your scripts a royal PIA. Jim - Thanks. Changed error_reporting to -1. No error messages. No change in output. Ethan CORRECT ALL THE WRONG SHIT AND YOULL GET ERROR MESSAGES!!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch Statement
On 9/29/2013 1:38 AM, Jim Giner wrote: session_start(); session_name(STORE); set_time_limit(2400); ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(-2); ini_set('error_reporting', 'E_ALL | E_STRICT'); ini_set('html_errors', 'On'); ini_set('log_errors', 'On'); This is what you should have in place of all of the above: session_start(); error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); set_time_limit(2);// if you use more than 2 secs you have a problem -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: UNLESS Statement Equivalent
-Original Message- From: Maciek Sokolewicz [mailto:tula...@gmail.com] On Behalf Of Maciek Sokolewicz Sent: 11 March 2013 22:44 unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600) ) ) { Whilst this is true as far as it goes, I would suggest applying deMorgan's laws to simplify slightly to if ( ($current_page != $saved_page) or ($current_ip != $saved_ip) or ($current_dt = ($saved_dt + 3600) ) ) Also, the keyword versions of the Boolean operators are very low priority, which is why you need all those extra parentheses -- if you switch to the symbolic versions, you can leave all the internal parentheses out to give: if ($current_page != $saved_page || $current_ip != $saved_ip || $current_dt = $saved_dt + 3600) Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Portland PD507, City Campus, Leeds Metropolitan University, Portland Way, LEEDS, LS1 3HE, United Kingdom E: m.f...@leedsmet.ac.uk T: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: UNLESS Statement Equivalent
2013/3/12 Ford, Mike m.f...@leedsmet.ac.uk -Original Message- From: Maciek Sokolewicz [mailto:tula...@gmail.com] On Behalf Of Maciek Sokolewicz Sent: 11 March 2013 22:44 unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600) ) ) { Whilst this is true as far as it goes, I would suggest applying deMorgan's laws to simplify slightly to if ( ($current_page != $saved_page) or ($current_ip != $saved_ip) or ($current_dt = ($saved_dt + 3600) ) ) If it is simpler depends, because not ($everything $must $be $true) is sometimes more logical :) Also, the keyword versions of the Boolean operators are very low priority, which is why you need all those extra parentheses -- if you switch to the symbolic versions, you can leave all the internal parentheses out to give: if ($current_page != $saved_page || $current_ip != $saved_ip || $current_dt = $saved_dt + 3600) Just to throw that in if (array($current_page, $current_ip, $current_dt) != array($saved_page, $saved_ip, $saved_dt + 3600)) This at least includes the information, that you have to sets of values, that should be equal :) Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Portland PD507, City Campus, Leeds Metropolitan University, Portland Way, LEEDS, LS1 3HE, United Kingdom E: m.f...@leedsmet.ac.uk T: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch
RE: [PHP] Re: UNLESS Statement Equivalent
-Original Message- From: Ford, Mike [mailto:m.f...@leedsmet.ac.uk] Sent: 12 March 2013 10:51 AM To: PHP General Subject: RE: [PHP] Re: UNLESS Statement Equivalent unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600) ) ) { Whilst this is true as far as it goes, I would suggest applying deMorgan's laws to simplify slightly to if ( ($current_page != $saved_page) or ($current_ip != $saved_ip) or ($current_dt = ($saved_dt + 3600) ) ) Also, the keyword versions of the Boolean operators are very low priority, which is why you need all those extra parentheses -- if you switch to the symbolic versions, you can leave all the internal parentheses out to give: if ($current_page != $saved_page || $current_ip != $saved_ip || $current_dt = $saved_dt + 3600) Cheers! Mike -- Mike, I presume you're saying the precedence of the Boolean keyword operators is lower than the Boolean symbol operators, but if so then wouldn't there be less need for the parentheses? I prefer using the Boolean keyword operators for both the readability and because of the lower precedence. Unless I've been wrong all this while and the keyword precedence is actually higher? Cheers Arno -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: UNLESS Statement Equivalent
-Original Message- From: Ford, Mike [mailto:m.f...@leedsmet.ac.uk] Sent: 12 March 2013 10:51 AM To: PHP General Subject: RE: [PHP] Re: UNLESS Statement Equivalent unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600) ) ) { Whilst this is true as far as it goes, I would suggest applying deMorgan's laws to simplify slightly to if ( ($current_page != $saved_page) or ($current_ip != $saved_ip) or ($current_dt = ($saved_dt + 3600) ) ) Also, the keyword versions of the Boolean operators are very low priority, which is why you need all those extra parentheses -- if you switch to the symbolic versions, you can leave all the internal parentheses out to give: if ($current_page != $saved_page || $current_ip != $saved_ip || $current_dt = $saved_dt + 3600) Cheers! Mike -- Mike, I presume you're saying the precedence of the Boolean keyword operators is lower than the Boolean symbol operators, but if so then wouldn't there be less need for the parentheses? I prefer using the Boolean keyword operators for both the readability and because of the lower precedence. Unless I've been wrong all this while and the keyword precedence is actually higher? Cheers Arno -- I checked the manual, the Boolean symbol operators have a higher precedence than ternary and assignment, while the Boolean keyword operators have the lowest precedence. So using Boolean keyword operators should obviate internal parentheses. Cheers Arno -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: UNLESS Statement Equivalent
-Original Message- From: Arno Kuhl [mailto:a...@dotcontent.net] Sent: 12 March 2013 13:04 Mike, I presume you're saying the precedence of the Boolean keyword operators is lower than the Boolean symbol operators, but if so then wouldn't there be less need for the parentheses? I prefer using the Boolean keyword operators for both the readability and because of the lower precedence. Unless I've been wrong all this while and the keyword precedence is actually higher? You are absolutely right, of course -- I failed to engage brain before typing words! Both sets of Boolean operators have lower priority than the comparison operators, so none of the parentheses are needed in either of those cases -- it's the assigning operators (and ?:) that come between the symbolic and keyword Booleans, so that $a = $b = true$c means something different from $a = $b=true and $c Thanks for picking me up on that one! Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Portland PD507, City Campus, Leeds Metropolitan University, Portland Way, LEEDS, LS1 3HE, United Kingdom E: m.f...@leedsmet.ac.uk T: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: UNLESS Statement Equivalent
On 11-3-2013 22:32, Angela Barone wrote: I'm looking for an 'unless' statement, but as far as I can tell, PHP doesn't have one. Hopefully someone can help me rewrite my statement. In English, I want to say: always do something UNLESS these 3 conditions are met. The best I've been able to come up with in PHP is this: if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600)) ) { return; } else { $query = UPDATE `table` SET `hits` = '$count', `agent` = '$agent', `ts` = '$date_time' WHERE `page` = '$page'; $result = mysql_query($query) or die ('Error! -- ' . mysql_error()); } However, I've read where this is not really acceptable. Can someone help me eliminate the 'else' portion of this if statement? Thank you, Angela P.S. I realize the above isn't complete code but it should still be clear. If not, let me know. Logically, unless means the same as if not. As long as you condense your entire expression to a single boolean result, the only thing you would need to do is negate it to create your unless-statement. In code, this would mean: unless ( $a ) = if ( !$a ) or to show it in a more clear way: unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt ($saved_dt + 3600) ) ) { -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch - Case Statement Questions
On Nov 19, 2012, at 2:46 PM, Stuart Dallas stu...@3ft9.com wrote: On 19 Nov 2012, at 19:35, Tim Streater t...@clothears.org.uk wrote: On 18 Nov 2012 at 14:44, Jim Giner jim.gi...@albanyhandball.com wrote: Just so. Perhaps those who are not grasping the point could re-read their copy of The Elements of Programming Style by Kernighan and Plauger where this sort of issue is covered. And of course, nothing is allowed to have changed since 1978! Using a switch in that manner is normal for me. Should I change my style simply because you don't agree with it? If so, why don't you change your style because I don't agree with that? Code is art; there is no right way to do it. Can code be shown to be more efficient, elegant, faster, cleaner? Yes. Right or wrong? No. +1 for me as well. Publish data -- January 11, 1976??? This may be your father's book (or grand-father in my case). So, don't confuse me with old facts -- buy new ones. Just look to other languages (such as JAVA) for variations in switch. I think php got it right. After all, while(), if(), do/while() and even for() work around the concept of true -- why not switch? Cheers, tedd PS: We've had this conversation many years ago (but not as far back as 1976). _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re:Switch - Case Statement Questions
Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} Does anyone know the answer why using the Switch it always execute the first case ? Many thanks Omar -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] re:Switch - Case Statement Questions
At 07:10 AM 11/16/2012, Omar Muhsin wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} For the switch statement to work, you need to use switch (true) $boxes = 1; switch (true) { case ($count 14): $boxes = 3; break; case ($count 7 $count = 14): $boxes = 2; break; case ($count = 7): default : $boxes = 1; break; } BTW, you don't need the break statement at the end of the last case and you don't really need the case ($count = 7) since that condition is what remains anyway. Ken -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] re:Switch - Case Statement Questions
On Fri, 16 Nov 2012, Ken Robinson wrote: At 07:10 AM 11/16/2012, Omar Muhsin wrote: Hello, I was just wondering after writting the code in version 2 here below, it turns out in testing that it actually PHP is not validating the expressions instead always I get the first case. 1.Using nested if statement {THE INTENDED BEHAVIOR}: if ($count 14) $boxes = 3; elseif($count 7 $count = 14) $boxes = 2; else $boxes = 1; 2. Using Switch {ALWAYS FIRST CASE!!!} //$boxes = 1; //switch ($count) { //case ($count 14): //$boxes = 3; //break; //case ($count 7 $count = 14): //$boxes = 2; //break; //case ($count = 7): //default : //$boxes = 1; //break; //} For the switch statement to work, you need to use switch (true) $boxes = 1; switch (true) { case ($count 14): $boxes = 3; break; case ($count 7 $count = 14): $boxes = 2; break; case ($count = 7): default : $boxes = 1; break; } I understand you use `switch` there for answering the question 'which of the n following questions is true'. But how do you evaluate `$count`? iñ BTW, you don't need the break statement at the end of the last case and you don't really need the case ($count = 7) since that condition is what remains anyway. Ken -- 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] mysql case statement
On Sun, Jun 27, 2010 at 4:08 AM, Tanel Tammik keevit...@gmail.com wrote: Hello, how to select only if value is present? $query = $db-query(select menus.id, menus.name, case when panels.id is not null then '1' end as hiddenpanel from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); i would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding value in HIDDENPANELS table! Br Tanel That's what a LEFT JOIN does - it returns all rows from the LEFT table that match the criteria in the WHERE clause, and then returns any rows from the RIGHT table that happen do match. If you only want rows that exist in both tables, change the join from LEFT (OUTER) JOIN to INNER JOIN. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
On Monday 28 June 2010 09:49:55 Andrew Ballard wrote: On Sun, Jun 27, 2010 at 4:08 AM, Tanel Tammik keevit...@gmail.com wrote: Hello, how to select only if value is present? $query = $db-query(select menus.id, menus.name, case when panels.id is not null then '1' end as hiddenpanel from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); i would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding value in HIDDENPANELS table! Br Tanel That's what a LEFT JOIN does - it returns all rows from the LEFT table that match the criteria in the WHERE clause, and then returns any rows from the RIGHT table that happen do match. If you only want rows that exist in both tables, change the join from LEFT (OUTER) JOIN to INNER JOIN. Tanel, we both learned something. I didn't fully understand join myself yet, but I think I do now. but let me ask this if the join wasn't there would an if statement like I mentioned have worked? Blessings, David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
On Sunday 27 June 2010 22:12:41 Brandon Rampersad wrote: no At least smack me and give us an explanation. :-) -- Blessings, David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
On Mon, Jun 28, 2010 at 10:27 AM, David McGlone da...@dmcentral.net wrote: Tanel, we both learned something. I didn't fully understand join myself yet, but I think I do now. but let me ask this if the join wasn't there would an if statement like I mentioned have worked? Blessings, David M. I think you are confusing a few things. You can't really rely on testing empty(DB_HIDDENPANELS) because the value of the constant DB_HIDDENPANELS is most likely a string that was set with an earlier call to define. The OP could have tested for the column value hiddenpanel using an if (...) test as you suggeted. However, given that the OP stated he would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS, the INNER JOIN will do that at the database query level, so an if (...) then test in PHP isn't really necessary. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
On Monday 28 June 2010 11:14:53 Andrew Ballard wrote: On Mon, Jun 28, 2010 at 10:27 AM, David McGlone da...@dmcentral.net wrote: Tanel, we both learned something. I didn't fully understand join myself yet, but I think I do now. but let me ask this if the join wasn't there would an if statement like I mentioned have worked? Blessings, David M. I think you are confusing a few things. You can't really rely on testing empty(DB_HIDDENPANELS) because the value of the constant DB_HIDDENPANELS is most likely a string that was set with an earlier call to define. The OP could have tested for the column value hiddenpanel using an if (...) test as you suggeted. However, given that the OP stated he would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS, the INNER JOIN will do that at the database query level, so an if (...) then test in PHP isn't really necessary. That is exactly what I was wondering. I haven't got to joins yet, and didn't understand them at all until Richard explained a few minutes ago. I knew my suggestion was a complete shot in the dark and I did feel stupid posting it, but I'm glad now, because I learned something invaluable. Thanks to this list, someday, which I hope is sooner than later, I'll be able to help somebody with complete confidence. -- Blessings, David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
On Sunday 27 June 2010 04:08:24 Tanel Tammik wrote: Hello, how to select only if value is present? $query = $db-query(select menus.id, menus.name, case when panels.id is not null then '1' end as hiddenpanel from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); i would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding value in HIDDENPANELS table! I would use an if statement since you only need to determine true or false. Something like: $query = $db-query(select menus.id, menus.name, from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); if (empty(DB_HIDDENPANELS)) { echo ; } else { echo hiddenpanel; } But I would wait for others to chime in on this one, because I'm very far from an expert, there's also got to be a much better efficient way to write the if statement above, but it's what I would do in a case like this until I found a better way. -- Blessings, David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql case statement
no On Sun, Jun 27, 2010 at 8:29 PM, David McGlone da...@dmcentral.net wrote: On Sunday 27 June 2010 04:08:24 Tanel Tammik wrote: Hello, how to select only if value is present? $query = $db-query(select menus.id, menus.name, case when panels.id is not null then '1' end as hiddenpanel from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); i would like to select hiddenpanel only if there is a corresponding value in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding value in HIDDENPANELS table! I would use an if statement since you only need to determine true or false. Something like: $query = $db-query(select menus.id, menus.name, from . \DB_MENUS . as menus left join . \DB_HIDDENPANELS . as panels on (menus.id = panels.menu_id) where menus.id=' . (int)$id . ' ); if (empty(DB_HIDDENPANELS)) { echo ; } else { echo hiddenpanel; } But I would wait for others to chime in on this one, because I'm very far from an expert, there's also got to be a much better efficient way to write the if statement above, but it's what I would do in a case like this until I found a better way. -- Blessings, David M. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- A Brandon_R Production
[PHP] complex if statement for field validation
Hello, I've got a form with three fields that are not required for proper completion of it, ending month, day, year fields. If a user enters nothing no problem, but if those form fields are entered I need them validated. They have to be in the correct format -MM-DD date format and that value also has to be greater than the starting date validated previously. I've got some not working code. Thanks. Dave. // if an ending date field was entered validate that // also must be greater than the starting date values if(!empty($_POST['month1']) !empty($_POST['day1']) !empty($_POST['year1']) { $month1=$_POST['month1']; $day1=$_POST['day1']; $year1=$_POST['year1']; $date_value1=$year1-$month1-$day1; } if(!checkdate($month1,$day1,$year1)) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } } if(!checkdate($month1,$day1,$year1)) $date_value { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] complex if statement for field validation
On Mon, 2010-06-07 at 16:51 -0400, David Mehler wrote: Hello, I've got a form with three fields that are not required for proper completion of it, ending month, day, year fields. If a user enters nothing no problem, but if those form fields are entered I need them validated. They have to be in the correct format -MM-DD date format and that value also has to be greater than the starting date validated previously. I've got some not working code. Thanks. Dave. // if an ending date field was entered validate that // also must be greater than the starting date values if(!empty($_POST['month1']) !empty($_POST['day1']) !empty($_POST['year1']) { $month1=$_POST['month1']; $day1=$_POST['day1']; $year1=$_POST['year1']; $date_value1=$year1-$month1-$day1; } if(!checkdate($month1,$day1,$year1)) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } } if(!checkdate($month1,$day1,$year1)) $date_value { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } strtotime() can create a timestamp from a date string that you can use to compare two dates, and the strings can be put together with the values from the variables. I'm assuming that your form is using a combination of select lists to create the date, so you should be able to just grab the integer values to test they are within valid ranges for a date. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] complex if statement for field validation
David Mehler wrote: Hello, I've got a form with three fields that are not required for proper completion of it, ending month, day, year fields. If a user enters nothing no problem, but if those form fields are entered I need them validated. They have to be in the correct format -MM-DD date format and that value also has to be greater than the starting date validated previously. I've got some not working code. Thanks. Dave. // if an ending date field was entered validate that // also must be greater than the starting date values if(!empty($_POST['month1']) !empty($_POST['day1']) !empty($_POST['year1']) { $month1=$_POST['month1']; $day1=$_POST['day1']; $year1=$_POST['year1']; $date_value1=$year1-$month1-$day1; } if(!checkdate($month1,$day1,$year1)) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } } if(!checkdate($month1,$day1,$year1)) $date_value { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } Well, I don't know if they are cut/paste errors, but you have a few syntax errors in the above code... # Initialize your date container variables $date_value_ts = $date_value1_ts = null; # Setup start date stuff if ( !empty($_POST['month']) !empty($_POST['day']) !empty($_POST['year']) ) { $month = (int)$_POST['month']; $day= (int)$_POST['day']; $year = (int)$_POST['year']; $date_value = {$year}-{$month}-{$day}; #if ( !checkdate($month, $day, $year) ) if ( ($date_value_ts = strtotime($date_value) ) === FALSE ) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } } Test for if ( !empty($_POST['month1']) !empty($_POST['day1']) !empty($_POST['year1']) ) { $month1 = (int)$_POST['month1']; $day1 = (int)$_POST['day1']; $year1 = (int)$_POST['year1']; $date_value1= {$year1}-{$month1}-{$day1}; #if ( !checkdate($month1, $day1, $year1) ) if ( ($date_value1_ts = strtotime($date_value1) ) === FALSE ) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } } # Compare the two dates. Make sure end date is after start date if ( !is_null($date_value1_ts) ( (int)$date_value_ts (int)$date_value1_ts ) ) { echo Invalid Date.\n; } else { echo Entered Date is correct.\n; } -- Jim Lucas A: Maybe because some people are too annoyed by top-posting. Q: Why do I not get an answer to my question(s)? A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
2010/4/16 Adam Richardson simples...@gmail.com: On Thu, Apr 15, 2010 at 5:55 PM, Jason Pruim li...@pruimphotography.comwrote: On Apr 15, 2010, at 8:55 AM, tedd wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Hey tedd I just recently started using netbeans and it looks like it may fit the bill... it's simple enough to understand but can be extended if you want to. It also runs better on my Mac then Eclipse ever did. Just something that might be worth checking out :) +1 Netbeans +3 Netbeans Because + it's able to manage huge projects and huge files, + has the best code completion and + I'm not missing any features but one. ;-) It also runs great on linux. IMHO it beats the Zend IDE, Eclipse as well as Komodo Edit /ide. Regards -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Jan G.B. wrote: 2010/4/16 Adam Richardson simples...@gmail.com: On Thu, Apr 15, 2010 at 5:55 PM, Jason Pruim li...@pruimphotography.comwrote: On Apr 15, 2010, at 8:55 AM, tedd wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Hey tedd I just recently started using netbeans and it looks like it may fit the bill... it's simple enough to understand but can be extended if you want to. It also runs better on my Mac then Eclipse ever did. Just something that might be worth checking out :) +1 Netbeans +3 Netbeans Because + it's able to manage huge projects and huge files, + has the best code completion and + I'm not missing any features but one. ;-) It also runs great on linux. IMHO it beats the Zend IDE, Eclipse as well as Komodo Edit /ide. +googol Joe It runs in a terminal! Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
At 9:06 AM -0400 4/16/10, Robert Cummings wrote: It runs in a terminal! Cheers, Rob. That's not good. Airport security frowns on that. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
tedd wrote: At 9:06 AM -0400 4/16/10, Robert Cummings wrote: It runs in a terminal! Cheers, Rob. That's not good. Airport security frowns on that. Cheers, tedd Mac users might get a little upset if Airport security doesn't allow them to use their terminals to run -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
On Thu, Apr 15, 2010 at 8:55 AM, tedd tedd.sperl...@gmail.com wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Sorry, should have copied the list try netbeans http://netbeans.org/kb/articles/mac.html -- Bastien Cat, the other other white meat -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
On Thu, 2010-04-15 at 08:55 -0400, tedd wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com Have you tried Coder on the Mac? Most developers I know who use Macs (it's not the oxymoron it sounds! :p ) tend to use Coder. It's pretty good, and while I've not used it myself, I've seen enough to like it. In a perfect world my editor would be somewhere between Coder and Kate (the KDE Advanced Text Editor) with the best bits of both thrown in. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Basic switch statement
On Apr 15, 2010, at 8:55 AM, tedd wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Hey tedd I just recently started using netbeans and it looks like it may fit the bill... it's simple enough to understand but can be extended if you want to. It also runs better on my Mac then Eclipse ever did. Just something that might be worth checking out :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
On Apr 15, 2010, at 9:24 AM, Ashley Sheridan wrote: On Thu, 2010-04-15 at 08:55 -0400, tedd wrote: Have you tried Coder on the Mac? Most developers I know who use Macs (it's not the oxymoron it sounds! :p ) Most Mac people would say the morons use Windows ;) But that's another story for another list! :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
On Thu, Apr 15, 2010 at 5:55 PM, Jason Pruim li...@pruimphotography.comwrote: On Apr 15, 2010, at 8:55 AM, tedd wrote: At 4:13 PM -0400 4/14/10, Al wrote: Incidentally, about formatting scripts, one of the reasons I like phpEdit is that it has a terrific code beautifier. You can set it for phpDoc or Pear rendering. And, it auto indents, etc. as you enter stuff. Al... Unfortunately, there is no phpEdit version for the Mac. Currently, I use GoLive (without all the WYSIWYG bloatware), but it limitations are showing. I like Eclipse, but the learning curve is high and has more features than I need. Hey tedd I just recently started using netbeans and it looks like it may fit the bill... it's simple enough to understand but can be extended if you want to. It also runs better on my Mac then Eclipse ever did. Just something that might be worth checking out :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php +1 Netbeans -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
Re: [PHP] Basic switch statement
At 10:04 PM +0100 4/13/10, Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Fail on that last one. -1 is not equivalent to FALSE :B well that's one job I'm not getting :p cheers for the picking that one up Rob And that's the reason why I hate test like that! The short tricky logic questions that interviewer's use to be clever are just nonsense. I'll take someone who can solve a problem over one who can argue true/false logic tables every time. Not meaning that people who come natural to that sort of logic solving ability do not make good programmers (because they can -- like Rob), but rather people who fail those types of logic questions do NOT also fail to be good programmers (like me). It's similar to the Tortoise and Hare thing -- it really doesn't make any difference who gets there first for both can run the distance. I don't think those types of logic puzzles do much to measure anything other than people's ability to solve logic puzzles. IMO, it's interviewers leap of faith to think logic puzzles are a good indicator of programming prowess. To many of us, programming is just doing over until it works. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
At 5:06 PM -0400 4/13/10, Robert Cummings wrote: Nathan Rixham wrote: well that's one job I'm not getting :p Well you DID get 66.7%. I've met coders that would stare at the answer and still not understand :D Cheers, Rob. Well.. count me among those staring. I just don't get those type of things until I see them actually work. My logic works the other way -- when presented with a logic problem, I come up with a solution that works the way I think and I always to solve the problem presented. Perhaps my solution isn't as clever nor as cryptic as others, but it's always easier to read and understand. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
tedd wrote: At 5:06 PM -0400 4/13/10, Robert Cummings wrote: Nathan Rixham wrote: well that's one job I'm not getting :p Well you DID get 66.7%. I've met coders that would stare at the answer and still not understand :D Cheers, Rob. Well.. count me among those staring. I just don't get those type of things until I see them actually work. My logic works the other way -- when presented with a logic problem, I come up with a solution that works the way I think and I always to solve the problem presented. Perhaps my solution isn't as clever nor as cryptic as others, but it's always easier to read and understand. Maybe you haven't debugged enough other people code in your time as a developer... there's a certain knack to weeding out logic failures :) It also helps to use proper indentation and bracing format as illustrated by all my code examples so that you can more easily see where problems lie :D Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Basic switch statement
I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } I even put the following code before the switch statement just to make sure I'm not crazy: $val = 0; if($val 0) { echo If greater than 0; } else { echo If not greater than 0; } and it falls through to the else as it should. I've tried putting single and double quotes around the case variables but it always prints out the first value. I've recoded to use a series of if statements but why isn't the switch working? I've read through the 'loose comparison' section, but nothing appears to apply there. Sorry for the basic question. Steve Reilly
Re: [PHP] Basic switch statement
On Tue, 2010-04-13 at 12:04 -0400, steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } I even put the following code before the switch statement just to make sure I'm not crazy: $val = 0; if($val 0) { echo If greater than 0; } else { echo If not greater than 0; } and it falls through to the else as it should. I've tried putting single and double quotes around the case variables but it always prints out the first value. I've recoded to use a series of if statements but why isn't the switch working? I've read through the 'loose comparison' section, but nothing appears to apply there. Sorry for the basic question. Steve Reilly Change the first line of the switch to switch(true) and it will be functioning as you want. Normally, a switch has this form: switch($val) { case 1: { // statements break; } case 10: { // statements break; } default: { // statements } } But PHP does allow you to use variable cases (as you have in your example) if the value in the switch is a boolean (true or false). It can be a little confusing if you're new to PHP (or programming in general) but you'll get used to it after using it a few times. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Basic switch statement
steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Okay, I understand now, true/false is better, now it makes sense why it's always hitting the case it hits. Thank you Ashley.
Re: [PHP] Basic switch statement
At 12:04 PM -0400 4/13/10, steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. -snip- Sorry for the basic question. Steve Reilly Steve: Please review: http://php1.net/c/switch/ Note: 1. The value of the switch statement in this case is true (it could also be '1'). This is different than what's found in most other case/switch control structures in other languages. 2. Generally you should use functions to process and return values and then decide as to what to do with the results, such as print(). Putting print in functions is fine if the function is supposed to print something. 3. Try to make your variables and function names simple and syntactical -- it will help you later when your code becomes more complex. Good luck with trying to make a better living programming than driving a truck. If you want to say thanks for my effort, please answer me this -- with regard to the class you are talking: What is the name of the class? How many credits is it? What's the name of the textbook you are using? And, if there is a syllabus, can I have a copy? Thanks, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Robert Cummings wrote: steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. lovely Regards! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. Fail on that last one. -1 is not equivalent to FALSE :B Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. Fail on that last one. -1 is not equivalent to FALSE :B well that's one job I'm not getting :p cheers for the picking that one up Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming, drive a truck in the day, now taking night courses to get a better job for my family. Please bear with me if this is a dumb question, I don't have much experience. I'm taking a night class in HTML and PHP and can't figure out a problem and can't find the answer in the book for the course (Beginning PHP5 by Wrox Press), on the switch manual page on php.net, or in any postings to this mailing list. I'm trying to pass a value to a simple integer to a function, and then use that value in a switch statement. The problem I'm having is that regardless of the value of 'val', the first case statement always executes. Even if I put '$val = 0' right before the case statement, the first case statement executes. The syntax looks correct based on the php.net man page for switch and from the user examples. It also matches the example in the book. function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; $diff_obj = 1; break; case($val 0 ): echo Less than 0; $diff_obj = -1; break; default: echo Equal to 0; $diff_obj = 0; } print(Here's \$diff_obj2 in the function: . $diff_obj); return $diff_obj; } You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. Fail on that last one. -1 is not equivalent to FALSE :B well that's one job I'm not getting :p Well you DID get 66.7%. I've met coders that would stare at the answer and still not understand :D Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. Fail on that last one. -1 is not equivalent to FALSE :B well that's one job I'm not getting :p Well you DID get 66.7%. I've met coders that would stare at the answer and still not understand :D the travesty is that I spent most of yesterday on trains brushing up on / studying formal logic! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: 0 Q: Is 0 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is right) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: YES so no matter what value you set $val to; it's always true. Fail on that last one. -1 is not equivalent to FALSE :B well that's one job I'm not getting :p Well you DID get 66.7%. I've met coders that would stare at the answer and still not understand :D the travesty is that I spent most of yesterday on trains brushing up on / studying formal logic! Muphry's Law!!! http://en.wikipedia.org/wiki/Muphry's_law *heheh* Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Basic switch statement
Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: steve_r wrote: I'm new to programming function check_it2($val) { echo gettype($val); switch($val) { case($val 0 ): echo Switch greater than 0; You're a tad confused :) Q: What is the result of $val 0? A: false. Q: What is the value of $val? A: -1 Q: Is -1 equivalent to false? A: Yes! Use an if statement for this kind of logic. This is a fantastic example of false logic and an easy pitfall. in fact this would make a great interview question! to expand a little on the various scenarios (just for clarity, Rob is wrong) $val = 1; 1 0 equates to TRUE is 1 equivalent to TRUE : YES $val = 0; 0 0 equates to FALSE is 0 equivalent to FALSE : YES $val = -1; -1 0 equates to FALSE is -1 equivalent to FALSE: NO Fail on that last one. -1 is equivalent to FALSE :B well that's one job you're not getting :p Well I DID get 66.7%. I've met coders that would stare at the answer and still not understand :D the travesty is that you didn't spend most of yesterday on trains brushing up on / studying formal logic! Muphry's Law!!! http://en.wikipedia.org/wiki/Muphry's_law *heheh* in situations like this one might consider changing the conversation previous! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] help with statement
I'm trying to take a paragraph then break it into linebreaks and grab each line separately but it's not working, I can get the paragraph but my lines are not I get the object id #6 or #7 everytime Here's my code // grab all the paragraphs on the page $xpath = new DOMXPath($dom); //$graphs = $xpath-evaluate(/html/body//p); $graphs=$dom-getElementsByTagName(p); $lines=$dom-getElementsByTagName(br); // Set $i =5 because first 5 paragraphs are not inspections for ($i = 5; $i+1 $graphs-length; $i++) { $paragraph = $graphs-item($i); $text = $dom-saveXML($paragraph); $text = trim($text); /* //my experiment for getting line br for ($b = 1; $b+1 $lines-length; $b++) {*/ $title = $lines-item($i); $addie= $lines-item($i); if($TESTING) echo br /$i Graph: . $text . br /; echo br /$line: . $title . br /; echo br/$line: . $addie . br/; // } } ? Thanks Terion Happy Freecycling Free the List !! www.freecycle.org Over Moderation of Freecycle List Prevents Post Timeliness. Report Moderator Abuse Here: http://www.freecycle.org/faq/faq/contact-info Or Email Your Complaint to: f...@freecycle.org or i...@freecycle.org Twitter? http://twitter.com/terionmiller Facebook: a href=http://www.facebook.com/people/Terion-Miller/1542024891; title=Terion Miller's Facebook profile target=_TOPimg src= http://badge.facebook.com/badge/1542024891.237.919247960.png; border=0 alt=Terion Miller's Facebook profile/a
[PHP] long echo statement performance question
I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
... Wouldn't have thought so. But for readability, you may find this a little easier instead: ? {$var1} Blah {$var2} ... ?php -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.org (Updated January 31st) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
In my opinion, you would achieve better results using a template engine, like Smarty (http://www.smarty.net/). In addition, your code would be entirely separated from presentation in a elegant way. 2009/2/6 Frank Stanovcak blindspot...@comcast.net: I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Bruno Fajardo - Desenvolvimento bruno.faja...@dinamize.com - www.dinamize.com Dinamize RS - Porto Alegre-RS - CEP 90420-111 Fones (51) 3027 7158 / 8209 4181 - Fax (51) 3027 7150 Dinamize BA - Lauro de Freitas - Fone 71 3379.7830 Dinamize SC - Joinville - Fone 47 3025.1182 Dinamize DF - Asa Norte - Brasília - Fone 61 3274.1172 Dinamize SP - São Paulo - Fone 11 6824.6250 Dinamize PR - Curitiba - Fone 41 3306.4388 Dinamize RS - Caxias do Sul - Fone 54 3533.4333 Dinamize RJ - Rio de Janeiro - Fone 21 2169.6311 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
Wouldn't have thought so. But for readability, you may find this a little easier instead: Slight correction: ? ?=$var1? blah ?=var2? ?php -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.org (Updated January 31st) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
Richard Heyes rich...@php.net wrote in message news:af8726440902060918v6d2f1ee1ia3f839189874...@mail.gmail.com... Wouldn't have thought so. But for readability, you may find this a little easier instead: Slight correction: ? ?=$var1? blah ?=var2? ?php -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.org (Updated January 31st) Actually that's what I'm in the middle of undoing. The first revision was jumping in and out of PHP mode upwards of 60 times per page on the shorter pages. I've read a couple places that that can put a huge performace hit on things, so I was just trying to simplify the code a bit. Plus I don't have the fast tags, ?=, enabled *blush* -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
Bruno Fajardo bsfaja...@gmail.com wrote in message news:eeb6980b0902060915k2b34ec31nc6ad166e2c544...@mail.gmail.com... In my opinion, you would achieve better results using a template engine, like Smarty (http://www.smarty.net/). In addition, your code would be entirely separated from presentation in a elegant way. 2009/2/6 Frank Stanovcak blindspot...@comcast.net: I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Bruno Fajardo - Desenvolvimento bruno.faja...@dinamize.com - www.dinamize.com Dinamize RS - Porto Alegre-RS - CEP 90420-111 Fones (51) 3027 7158 / 8209 4181 - Fax (51) 3027 7150 I'm going to take a serious look at that for ver 2.0, but unfortunately live beta 1 rollout is monday, and I'm just cleaning house right now. Thanks for the advice though! Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
2009/2/6 Frank Stanovcak blindspot...@comcast.net: Richard Heyes rich...@php.net wrote in message news:af8726440902060918v6d2f1ee1ia3f839189874...@mail.gmail.com... Wouldn't have thought so. But for readability, you may find this a little easier instead: Slight correction: ? ?=$var1? blah ?=var2? ?php -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.org (Updated January 31st) Actually that's what I'm in the middle of undoing. The first revision was jumping in and out of PHP mode upwards of 60 times per page on the shorter pages. I've read a couple places that that can put a huge performace hit on things, so I was just trying to simplify the code a bit. Plus I don't have the fast tags, ?=, enabled *blush* They're called short tags, not fast tags. There is nothing faster about them beyond the typing effort required. This question, or rather variations of it, appear on this list at pretty regular intervals. A little while ago I wrote a script to test the speed of various output methods. http://stut.net/projects/phpspeed/?iterations=1 As you can see, the difference is so minimal that unless you're doing it hundreds of thousands of times per script it makes little difference how you do it. In short it's usually not worth optimising at this level since greater gains are certainly to be had by looking at your interaction with databases or other external resources. -Stuart -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
On Fri, Feb 6, 2009 at 12:15 PM, Bruno Fajardo bsfaja...@gmail.com wrote: In my opinion, you would achieve better results using a template engine, like Smarty (http://www.smarty.net/). In addition, your code would be entirely separated from presentation in a elegant way. 2009/2/6 Frank Stanovcak blindspot...@comcast.net: I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Bruno Fajardo - Desenvolvimento bruno.faja...@dinamize.com - www.dinamize.com Dinamize RS - Porto Alegre-RS - CEP 90420-111 Fones (51) 3027 7158 / 8209 4181 - Fax (51) 3027 7150 Dinamize BA - Lauro de Freitas - Fone 71 3379.7830 Dinamize SC - Joinville - Fone 47 3025.1182 Dinamize DF - Asa Norte - Brasília - Fone 61 3274.1172 Dinamize SP - São Paulo - Fone 11 6824.6250 Dinamize PR - Curitiba - Fone 41 3306.4388 Dinamize RS - Caxias do Sul - Fone 54 3533.4333 Dinamize RJ - Rio de Janeiro - Fone 21 2169.6311 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php In a thread about performance you suggest Smarty? Really? :D -- http://www.voom.me | EFnet: #voom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
Well, Smarty's caching layer is very fast. Maybe not as fast as an echo statement, but apparentely Frank was also interested in separate logic from presentation, and a series of echo's is not the best solution in my opinion. :-) But the best solution depends of the context of the application, i agree. 2009/2/6 Eric Butera eric.but...@gmail.com: On Fri, Feb 6, 2009 at 12:15 PM, Bruno Fajardo bsfaja...@gmail.com wrote: In my opinion, you would achieve better results using a template engine, like Smarty (http://www.smarty.net/). In addition, your code would be entirely separated from presentation in a elegant way. 2009/2/6 Frank Stanovcak blindspot...@comcast.net: I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank -- 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 In a thread about performance you suggest Smarty? Really? :D -- http://www.voom.me | EFnet: #voom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
On Fri, Feb 6, 2009 at 1:43 PM, Bruno Fajardo bsfaja...@gmail.com wrote: Well, Smarty's caching layer is very fast. Maybe not as fast as an echo statement, but apparentely Frank was also interested in separate logic from presentation, and a series of echo's is not the best solution in my opinion. :-) But the best solution depends of the context of the application, i agree. Right on. I was just playing anyways. I'm a strong supporter of things like Savant Zend_View. Use PHP, but for read only type of things with no logic. -- http://www.voom.me | EFnet: #voom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
I am so stick to Smarty that I never tried other solutions, like Savant. Thanks for the tip, I'll try it right away. Cheers. 2009/2/6 Eric Butera eric.but...@gmail.com: On Fri, Feb 6, 2009 at 1:43 PM, Bruno Fajardo bsfaja...@gmail.com wrote: Well, Smarty's caching layer is very fast. Maybe not as fast as an echo statement, but apparentely Frank was also interested in separate logic from presentation, and a series of echo's is not the best solution in my opinion. :-) But the best solution depends of the context of the application, i agree. Right on. I was just playing anyways. I'm a strong supporter of things like Savant Zend_View. Use PHP, but for read only type of things with no logic. -- http://www.voom.me | EFnet: #voom -- Bruno Fajardo - Desenvolvimento bruno.faja...@dinamize.com - www.dinamize.com Dinamize RS - Porto Alegre-RS - CEP 90420-111 Fones (51) 3027 7158 / 8209 4181 - Fax (51) 3027 7150 Dinamize BA - Lauro de Freitas - Fone 71 3379.7830 Dinamize SC - Joinville - Fone 47 3025.1182 Dinamize DF - Asa Norte - Brasília - Fone 61 3274.1172 Dinamize SP - São Paulo - Fone 11 6824.6250 Dinamize PR - Curitiba - Fone 41 3306.4388 Dinamize RS - Caxias do Sul - Fone 54 3533.4333 Dinamize RJ - Rio de Janeiro - Fone 21 2169.6311 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
On Fri, Feb 6, 2009 at 2:10 PM, Bruno Fajardo bsfaja...@gmail.com wrote: I am so stick to Smarty that I never tried other solutions, like Savant. Thanks for the tip, I'll try it right away. Cheers. 2009/2/6 Eric Butera eric.but...@gmail.com: On Fri, Feb 6, 2009 at 1:43 PM, Bruno Fajardo bsfaja...@gmail.com wrote: Well, Smarty's caching layer is very fast. Maybe not as fast as an echo statement, but apparentely Frank was also interested in separate logic from presentation, and a series of echo's is not the best solution in my opinion. :-) But the best solution depends of the context of the application, i agree. Right on. I was just playing anyways. I'm a strong supporter of things like Savant Zend_View. Use PHP, but for read only type of things with no logic. -- http://www.voom.me | EFnet: #voom -- Bruno Fajardo - Desenvolvimento bruno.faja...@dinamize.com - www.dinamize.com Dinamize RS - Porto Alegre-RS - CEP 90420-111 Fones (51) 3027 7158 / 8209 4181 - Fax (51) 3027 7150 Dinamize BA - Lauro de Freitas - Fone 71 3379.7830 Dinamize SC - Joinville - Fone 47 3025.1182 Dinamize DF - Asa Norte - Brasília - Fone 61 3274.1172 Dinamize SP - São Paulo - Fone 11 6824.6250 Dinamize PR - Curitiba - Fone 41 3306.4388 Dinamize RS - Caxias do Sul - Fone 54 3533.4333 Dinamize RJ - Rio de Janeiro - Fone 21 2169.6311 Keep in mind it won't keep people from running whatever code they want. It's more of a discipline you must enforce upon yourself and development team. It works out well for me though, but all requirements are different. -- http://www.voom.me | EFnet: #voom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
On Fri, Feb 06, 2009 at 01:09:13PM -0500, Eric Butera wrote: On Fri, Feb 6, 2009 at 12:15 PM, Bruno Fajardo bsfaja...@gmail.com wrote: In my opinion, you would achieve better results using a template engine, like Smarty (http://www.smarty.net/). In addition, your code would be entirely separated from presentation in a elegant way. snip In a thread about performance you suggest Smarty? Really? :D You know, I was gonna say something about that, but I figure I've complained enough on list. I agree, though. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
Stuart stut...@gmail.com wrote in message news:a5f019de0902060932k1ccf2948ua42f3cfa33694...@mail.gmail.com... 2009/2/6 Frank Stanovcak blindspot...@comcast.net: Richard Heyes rich...@php.net wrote in message news:af8726440902060918v6d2f1ee1ia3f839189874...@mail.gmail.com... Wouldn't have thought so. But for readability, you may find this a little easier instead: Slight correction: ? ?=$var1? blah ?=var2? ?php -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.org (Updated January 31st) Actually that's what I'm in the middle of undoing. The first revision was jumping in and out of PHP mode upwards of 60 times per page on the shorter pages. I've read a couple places that that can put a huge performace hit on things, so I was just trying to simplify the code a bit. Plus I don't have the fast tags, ?=, enabled *blush* They're called short tags, not fast tags. There is nothing faster about them beyond the typing effort required. This question, or rather variations of it, appear on this list at pretty regular intervals. A little while ago I wrote a script to test the speed of various output methods. http://stut.net/projects/phpspeed/?iterations=1 As you can see, the difference is so minimal that unless you're doing it hundreds of thousands of times per script it makes little difference how you do it. In short it's usually not worth optimising at this level since greater gains are certainly to be had by looking at your interaction with databases or other external resources. -Stuart -- http://stut.net/ Thanks Stuart! So for clarity's sake jumping into and out of PHP parsing mode to echo a var isn't going to do as great a performance hit as these naysayers would have me believe. Good. I like the color coding of my html in my editor that is lost when I have to quote it to get it to echo/print properly! Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
On Fri, 2009-02-06 at 17:32 +, Stuart wrote: They're called short tags, not fast tags. There is nothing faster about them beyond the typing effort required. Mostly true. However, the only thing that ?= ? does is an echo/print, so aside from saving you ?php echo $foo; ? all the time, you can't do ?= if ($foo) print 'blah'; ? Personally, I love them. http://stut.net/projects/phpspeed/?iterations=1 God bless you son for making this f'n amazing page! Ironically it doesn't have ?= $x ? in it though. HAHA! can you add that to your test suite too? D.Vin http://daevid.com
Re: [PHP] long echo statement performance question
On Friday 06 February 2009 19:12:08 Frank Stanovcak wrote: I'm in the process of seperating logic from display in a section of code, and wanted to make sure I wasn't treading on a performance landmine here, so I ask you wizened masters of the dark arts this... is there a serious performance hit, or reason not to use long, ie more than 30 - 40 lines, comma conjoined echo statments... echo 'blah', $var, 'blah', $var2,...ad nauseum ... to output mixed html and php var values? If so could you refer me to a work around, or better way? Frank Seperate logic. Not the template. ?=$variable? was good for templating. With opcode cache all your template was cached. And of course you should not use write functions in templates. and if you really want to do this with echo yada yada I suggest this one. $output = ''; do someting remove echo and put $output. and end of the script do echo $output. This was the best way. Of course you can use output buffering. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] long echo statement performance question
I have been reading this thread with interest ... and amusement. FWII for my web sites I have 1) Most PHP code in files outside of the document root 2) Site specific variables (constants, really) in an include.php file 3) All HTML is also in this include file and is the content a variable. 4) Nested in the HTML code are variables for the content, like the menu and body text 5) All presentation is done using CSS 6) Each page just has PHP code (mostly function calls) where the content variables are built 7) Finally the content variable is echoed. Stephen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch statement Question
tedd tedd.sperl...@gmail.com wrote in message news:p06240801c5aa0ed7d...@[192.168.1.101]... At 4:16 PM +0100 1/30/09, Jochem Maas wrote: tedd schreef: At 4:43 PM -0500 1/29/09, Frank Stanovcak wrote: yes...that is legal. as long as the statment resolves to a boolean it will work. It's not technically correct, but it does work. There you go again. What's technically correct? hiya tedd, you mean to ask not technically correct ... I plead that it's his statement that is not technically correct. 1. php does a soft comparison ('==' rather than '===') so the results of each case expression is auto-cast to a boolean (in the case of switch'ing on TRUE), ergo there is no 'as long', statements will always resolve to a boolean ... only they may not do it in a way that is readily understood by everyone. 2. the php engine specifically allows for 'complex expression' cases testing against a boolean switch value ... not best practice according to some but very much technically correct according to the php implementation. Good explanation -- I think the drum he's beating is that some of use the switch without actually using the main expression within the control, such as: switch($who_cares) { case $a = $b: // do something break; case $a 0: // do something else break; case $a 0: // do something elser break; } The control works. However to him, technically correct means: switch($a) { case 0: // do something break; case 1: // do something else break; case 2: // do something elser break; } That's what I think he's advocating. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com very true since this is the way it is taught in every book. Php, however, allows for other uses that weren't technically in the original plan for this command from other languages. The problem is I shave my head, so I have no hairs to split over this one. :) Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch statement Question
At 4:16 PM +0100 1/30/09, Jochem Maas wrote: tedd schreef: At 4:43 PM -0500 1/29/09, Frank Stanovcak wrote: yes...that is legal. as long as the statment resolves to a boolean it will work. It's not technically correct, but it does work. There you go again. What's technically correct? hiya tedd, you mean to ask not technically correct ... I plead that it's his statement that is not technically correct. 1. php does a soft comparison ('==' rather than '===') so the results of each case expression is auto-cast to a boolean (in the case of switch'ing on TRUE), ergo there is no 'as long', statements will always resolve to a boolean ... only they may not do it in a way that is readily understood by everyone. 2. the php engine specifically allows for 'complex expression' cases testing against a boolean switch value ... not best practice according to some but very much technically correct according to the php implementation. Good explanation -- I think the drum he's beating is that some of use the switch without actually using the main expression within the control, such as: switch($who_cares) { case $a = $b: // do something break; case $a 0: // do something else break; case $a 0: // do something elser break; } The control works. However to him, technically correct means: switch($a) { case 0: // do something break; case 1: // do something else break; case 2: // do something elser break; } That's what I think he's advocating. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch statement Question
At 4:43 PM -0500 1/29/09, Frank Stanovcak wrote: yes...that is legal. as long as the statment resolves to a boolean it will work. It's not technically correct, but it does work. There you go again. What's technically correct? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Switch statement Question
tedd schreef: At 4:43 PM -0500 1/29/09, Frank Stanovcak wrote: yes...that is legal. as long as the statment resolves to a boolean it will work. It's not technically correct, but it does work. There you go again. What's technically correct? hiya tedd, you mean to ask not technically correct ... I plead that it's his statement that is not technically correct. 1. php does a soft comparison ('==' rather than '===') so the results of each case expression is auto-cast to a boolean (in the case of switch'ing on TRUE), ergo there is no 'as long', statements will always resolve to a boolean ... only they may not do it in a way that is readily understood by everyone. 2. the php engine specifically allows for 'complex expression' cases testing against a boolean switch value ... not best practice according to some but very much technically correct according to the php implementation. Cheers, tedd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Switch statement Question
Alice Wei aj...@alumni.iu.edu wrote in message news:snt101-w587cd616331fc59b84834af0...@phx.gbl... Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? Thanks in advance. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail You mean as in... switch($string){ case : code break; case $string == 'some value': code break; case in_array($string, $somearray): code break; default: code }; yes...that is legal. as long as the statment resolves to a boolean it will work. It's not technically correct, but it does work. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Cannot create statement handler
I was wondering if anyone sees something I am bypassing: I have this sample code that works in another server: ?php function getClientFullName($dbh,$id){ // die(var_dump($dbh)); $sql = SELECT * FROM Clients WHERE Id=.$id; // die(print $sql); $sthr = $dbh-query($sql); // die(var_dump($sthr)); $res = $sthr-fetch(PDO::FETCH_ASSOC); return $res['Name']; } try { $dbh = new PDO('mysql:host=localhost;port=3306;dbname=ins', 'root', '', array(PDO::ATTR_PERSISTENT = false)); $sql = SELECT * FROM Contracts; $sth = $dbh-query($sql); print pre; while($res = $sth-fetch(PDO::FETCH_ASSOC)) { $name = getClientFullName($dbh,$res['ClientId']); print $name.br; } } catch (Exception $e) { print $e-getMessage(); } ? but in my case I can't make it work on my own server. I have removed both PHP and apache and compiled everything for source just to make sure that I will avoid possible problems using rpm. It seems that if I dump $sthr it returns false. Although if I print the query and use it directly it works fine. Apache 2.2.10 PHP 5.2.6 Linux EL5 I've posted this some days ago but none had any idea about this. Do think this could be a bug? -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Newbie select statement questions 'WHERE'
NEWBIE! I have some GET data coming from a previous search form. How do I add the WHERE part ? orig: $query_get1 = SELECT p_First, p_id, p_Last, p_Lvl, p_Sel FROM contacts; --W / WHERE...??? $query_get1 = SELECT p_First, p_id, p_Last, p_Lvl, p_Sel FROM contacts WHERE p_First like $_GET['p_First'] or p_Last like $_GET['p_Last']; I tried various things that make errors: where p_First like '%$_GET['p_First ']%'; where p_First like .$_GET['p_First '].; Etc... How can I make this work? -- Thanks - RevDave Cool @ hosting4days . com [db-lists] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie select statement questions 'WHERE'
On Wed, 2008-04-23 at 19:07 -0700, revDAVE wrote: NEWBIE! I have some GET data coming from a previous search form. How do I add the WHERE part ? orig: $query_get1 = SELECT p_First, p_id, p_Last, p_Lvl, p_Sel FROM contacts; --W / WHERE...??? $query_get1 = SELECT p_First, p_id, p_Last, p_Lvl, p_Sel FROM contacts WHERE p_First like $_GET['p_First'] or p_Last like $_GET['p_Last']; I tried various things that make errors: where p_First like '%$_GET['p_First ']%'; where p_First like .$_GET['p_First '].; Etc... How can I make this work? ?php $escape = 'mysql_real_escape_string'; $query_get1 = SELECT .p_First, .p_id, .p_Last, .p_Lvl, .p_Sel .FROM .contacts .WHERE .p_First LIKE '.$escape( $_GET['p_First'] ).' .OR .p_Last LIKE '.$escape( $_GET['p_Last'] ).' ; ? But really... you're not doing any partial matching so don't bother with 'LIKE'. Just do the following: ?php $query_get1 = SELECT .p_First, .p_id, .p_Last, .p_Lvl, .p_Sel .FROM .contacts .WHERE .p_First = '.$escape( $_GET['p_First'] ).' .OR .p_Last = '.$escape( $_GET['p_Last'] ).' ; ? Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie ' If Statement' Question
Thank you everybody for these great tips! -- Thanks - RevDave Cool7 @ hosting4days . com [db-lists] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Newbie ' If Statement' Question
Hello Folks, I would like to be able to wrap a 'form' inside a php 'if statement' - so that the form will appear if the 'if condition' is met. - most likely I cannot have a ?php tag inside another one - and am sure I'm doing other things wrong also... - now I get the error - Parse error: syntax error, unexpected T_STRING in... Q: What is the best way to accomplish this goal? --- ?php if ($emp_row-getField('testfield') !=) { print form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active/ input type=hidden name=The_Date value=/ input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? input type=submit name=edit_submit value=Activate /form ;}else {print Non Print;} ? -- Thanks - RevDave Cool7 @ hosting4days . com [db-lists] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Newbie ' If Statement' Question
On Fri, Mar 14, 2008 at 7:56 PM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hello Folks, I would like to be able to wrap a 'form' inside a php 'if statement' - so that the form will appear if the 'if condition' is met. - most likely I cannot have a ?php tag inside another one - and am sure I'm doing other things wrong also... - now I get the error - Parse error: syntax error, unexpected T_STRING in... Q: What is the best way to accomplish this goal? --- ?php if ($emp_row-getField('testfield') !=) { print form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active/ input type=hidden name=The_Date value=/ input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? input type=submit name=edit_submit value=Activate /form ;}else {print Non Print;} ? something like this, ?php if($emp_row-getField('testfield') != '') { ? form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active / input type=hidden name=The_Date value= / input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? / input type=submit name=edit_submit value=Activate / /form ?php } ? // warning: typed directly into browser w/ no testing ;) -nathan
Re: [PHP] Newbie ' If Statement' Question
On Fri, Mar 14, 2008 at 7:56 PM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hello Folks, I would like to be able to wrap a 'form' inside a php 'if statement' - so that the form will appear if the 'if condition' is met. - most likely I cannot have a ?php tag inside another one - and am sure I'm doing other things wrong also... - now I get the error - Parse error: syntax error, unexpected T_STRING in... Q: What is the best way to accomplish this goal? --- ?php if ($emp_row-getField('testfield') !=) { print form name=edit method=post action=emp_edit_result2.php input type=hidden name=Status value=Active/ input type=hidden name=The_Date value=/ input type=hidden name=-recid value=?php echo $emp_row-getRecordId(); ? input type=submit name=edit_submit value=Activate /form ;}else {print Non Print;} ? -- Thanks - RevDave Cool7 @ hosting4days . com [db-lists] Your first mistake I'm finding is the use of quotes. You started the print with a , which means the print ends at name=, and it's expecting more PHP code, rather than a HTML string. The first fix would be to either escape all quotes in the HTML with a slash (\) before it, or to change your opening and closing quotes on the print to a single quote ('). Second, yes, you cannot enclose PHP tags inside PHP tags. To do this, change that portion to end the print, attach the two portions with a period (.) and start up with the php function, then a period, and back to the print. Here's a fix that should work. I haven't tested it though, so I may have missed one or two quotes. ?php if ($emp_row-getField('testfield') !=) { print form name=\edit\ method=\post\ action=\emp_edit_result2.php\ input type=\hidden\ name=\Status\ value=\Active\/ input type=\hidden\ name=\The_Date\ value=\\/ input type=\hidden\ name=\-recid\ value=\$emp_row-getRecordId().\ input type=\submit\ name=\edit_submit\ value=\Activate\ /form ;}else {print Non Print;} ?
Re: [PHP] Re: If statement duplicating mysql records?
On Jun 19, 2007, at 5:10 PM, Roberto Mansfield wrote: Jason Pruim wrote: Okay, so I have a question... Probably pretty easy, but why would my if statement show more records then what are in the database? if($row[5] =='Level4'){ // White Highlight }// End of Level 4 else {// Green Highlight }// End of Unclassified If I have a record that matches Level4 it will display both with the $Level4 color and the $unclassified color. Ideally it would only show up under the $Level4 category if it's Level4... If that makes sense. I don't think IF is the culprit. I'm guessing you have a bad join in your sql query so you are getting back more records than you expect. Have you run the query directly in to see what your results are? Hi Roberto, Thanks for the reply! The query I'm using to grab the records from MySQL is a very simple one: $sql = SELECT * FROM tasks WHERE completed='0' order by id; I don't totally understand yet why it happened, but Jim's suggestion worked great. Thanks though! Roberto -- 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
[PHP] Re: If statement duplicating mysql records?
Jason Pruim wrote: Okay, so I have a question... Probably pretty easy, but why would my if statement show more records then what are in the database? if($row[5] =='Level4'){ // White Highlight }// End of Level 4 else {// Green Highlight }// End of Unclassified If I have a record that matches Level4 it will display both with the $Level4 color and the $unclassified color. Ideally it would only show up under the $Level4 category if it's Level4... If that makes sense. I don't think IF is the culprit. I'm guessing you have a bad join in your sql query so you are getting back more records than you expect. Have you run the query directly in to see what your results are? Roberto -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach statement
It will probably work, and you could find out for sure by just trying it. It might be better to construct a single query using things like: $company_ids = implode(', ', $_POST['reporton_company']); $query .= WHERE company_id IN ($company_ids) ; This presumes you have already validated the company_ids. However, if the number of companies, periods, and questions is SMALL, the difference between one big query and a dozen little queries is pretty minimal, and if you find the nested loops easier to maintain, go for it. On Mon, July 31, 2006 5:02 am, Chris Grigor wrote: Have been wondering if this is possible Basically I have 3 posted arrays, $_POST['reporton_company'] (this can be various company id's. ie 3,6,7) $_POST['report_period'] (this can be various periods but a max of 4 submitted. ie 3,4,5) $_POST['questions_groups'] (this can be various - starting from 1- whatever (usually a max of 10 or 11). ie 1,2,3,4,5,6,7,8,9,10) So the select should work as 1. for each company listed go through the loop 2. for each report period listed go through loop for each company 3. for each questions group go through the loop for each report period and each company.. So I came up with this - will it work?? foreach($_POST['reporton_company'] as $cmp_ind =$arrayd_cmp_id) { foreach($_POST['report_period'] as $rep_ind =$arrayd_per_id) { foreach($_POST['questions_groups'] as $group_ind = $arrayd_group_no) { mysql_select_db($database_name, $dname); $query_get_list_of_answers = SELECT * FROM answers LEFT JOIN (questions, period) ON (questions.id=answers.ans_l_question_idAND period.per_id=ans_l_period_id) where ans_l_company_id = '$arrayd_cmp_id' AND per_id = '$arrayd_per_id' AND group_no = '$arrayd_group_no';; $get_list_of_answers = mysql_query($query_get_list_of_answers, $antiva) or die(mysql_error()); $row_get_list_of_answers = mysql_fetch_assoc($get_list_of_answers); $totalRows_get_list_of_answers = mysql_num_rows($get_list_of_answers); } } } Anyone suggest an easier way? Cheers Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Nested foreach statement
Have been wondering if this is possible Basically I have 3 posted arrays, $_POST['reporton_company'] (this can be various company id's. ie 3,6,7) $_POST['report_period'] (this can be various periods but a max of 4 submitted. ie 3,4,5) $_POST['questions_groups'] (this can be various - starting from 1- whatever (usually a max of 10 or 11). ie 1,2,3,4,5,6,7,8,9,10) So the select should work as 1. for each company listed go through the loop 2. for each report period listed go through loop for each company 3. for each questions group go through the loop for each report period and each company.. So I came up with this - will it work?? foreach($_POST['reporton_company'] as $cmp_ind =$arrayd_cmp_id) { foreach($_POST['report_period'] as $rep_ind =$arrayd_per_id) { foreach($_POST['questions_groups'] as $group_ind = $arrayd_group_no) { mysql_select_db($database_name, $dname); $query_get_list_of_answers = SELECT * FROM answers LEFT JOIN (questions, period) ON (questions.id=answers.ans_l_question_id AND period.per_id=ans_l_period_id) where ans_l_company_id = '$arrayd_cmp_id' AND per_id = '$arrayd_per_id' AND group_no = '$arrayd_group_no';; $get_list_of_answers = mysql_query($query_get_list_of_answers, $antiva) or die(mysql_error()); $row_get_list_of_answers = mysql_fetch_assoc($get_list_of_answers); $totalRows_get_list_of_answers = mysql_num_rows($get_list_of_answers); } } } Anyone suggest an easier way? Cheers Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach statement
foreach($_POST['reporton_company'] as $cmp_ind =$arrayd_cmp_id) { foreach($_POST['report_period'] as $rep_ind =$arrayd_per_id) { foreach($_POST['questions_groups'] as $group_ind = $arrayd_group_no) { mysql_select_db($database_name, $dname); Why do that here? That's going to do it for each element in the arrays, lots of overhead! Move that outside the first loop. I'd probably leave it as it is and make sure your data is what you expect, ie use mysql_real_escape_string in appropriate places. You could clean up one loop by doing this: $query_get_list_of_answers = SELECT * FROM answers LEFT JOIN (questions, period) ON (questions.id=answers.ans_l_question_id AND period.per_id=ans_l_period_id) where ans_l_company_id = '$arrayd_cmp_id' AND per_id = '$arrayd_per_id' AND group_no IN ( . implode(',', $_POST['questions_groups']) . ); but if I can enter dodgy values in the questions_groups form field, you're hosed. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: If statement question
Alex Major wrote: Hi list. Basically, I'm still learning new things about php and I was wondering if things inside an if statement get 'looked at' by a script if the condition is false. For example, would this mysql query get executed if $number = 0 ? If ($number == 1) { mysql_query($blah) } I know that's not really valid php, but hope it gets my point across. I was just wondering from an optimisation perspective, as I don't want sql commands being executed when they don't need to be (unnecessary server usage). Thanks in advance for your responses. Alex. Stuff inside an if statement will be compiled (So it has to be free of syntax errors and such), but it won't be executed unless the condition is true. Regards, Adam Zey. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] mysql select statement in php having three conditions
hi, can someone tell me how to do this: i have to retrive data from a mysql table let's sayTABLE . i have to check that the rows i retrive meet this condition: field1='$variable',field2 is false and field3 is also false. as you can see field2 and field3 are bool type. field1 is varchar. i did this query SELECT * FROM TABLE WHERE field1='$variable' AND field2='0' AND field3='0'; another one tried is SELECT * FROM TABLE WHERE field1='$variable' AND field2=false AND field3=false; But none was a success. it didn't matter whether i used '0' or 0 and false or 'false'. thanks in advance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] mysql select statement in php having three conditions
[snip] can someone tell me how to do this: i have to retrive data from a mysql table let's sayTABLE . i have to check that the rows i retrive meet this condition: field1='$variable',field2 is false and field3 is also false. as you can see field2 and field3 are bool type. field1 is varchar. i did this query SELECT * FROM TABLE WHERE field1='$variable' AND field2='0' AND field3='0'; another one tried is SELECT * FROM TABLE WHERE field1='$variable' AND field2=false AND field3=false; But none was a success. it didn't matter whether i used '0' or 0 and false or 'false'. thanks in advance. [/snip] That is likely because the data is either blank or NULL , try; SELECT * FROM TABLE WHERE field1='$variable' AND field2 IS NULL AND field3= IS NULL ; or SELECT * FROM TABLE WHERE field1='$variable' AND field2='' AND field3=''; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Unnecessary if statement? Programming technique
Hi everyone Quick question: If I have such a loop: ? for($i=0;$i100;$i++) { if($i==100) { // do something special for this occurence } // do something standard } ? In this case it seems such a waste that the if() statement is done 99 times when it's not needed. Is there any obvious trick that I am missing? I'm not sure how taxing a simple if() statement is on a server, maybe it's negligible, or is it something to worry about? Something which I'd prefer NOT to do: ? for($i=0;$i100;$i++) { // do something standard } // do something special for $i = 100 for($i=101;$i100;$i++) { // do something standard } ? as I would have have to either keep two copies of the code or write a function just for this purpose, which hardly seems worth it. Thanks to anyone who takes the time to think about my question and/or respond. Best wishes, Steve McGill -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Unnecessary if statement? Programming technique
Hi, Why not for ($i = 0; $i 100/100; ++$i) { for ($j = 0; $j 100; ++$j) { // do something standard } // do something special for this occurence } Jared -Original Message- From: Steve McGill [mailto:[EMAIL PROTECTED] Sent: 06 December 2005 10:18 To: php-general@lists.php.net Subject: [PHP] Unnecessary if statement? Programming technique Hi everyone Quick question: If I have such a loop: ? for($i=0;$i100;$i++) { if($i==100) { } // do something standard } ? In this case it seems such a waste that the if() statement is done 99 times when it's not needed. Is there any obvious trick that I am missing? I'm not sure how taxing a simple if() statement is on a server, maybe it's negligible, or is it something to worry about? Something which I'd prefer NOT to do: ? for($i=0;$i100;$i++) { // do something standard } // do something special for $i = 100 for($i=101;$i100;$i++) { // do something standard } ? as I would have have to either keep two copies of the code or write a function just for this purpose, which hardly seems worth it. Thanks to anyone who takes the time to think about my question and/or respond. Best wishes, Steve McGill -- 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] Unnecessary if statement? Programming technique
Jared Williams wrote: Why not for ($i = 0; $i 100/100; ++$i) This involves dividing 100 by 100 for each iteration of the loop. It would be better to test against 1. There is also the unwanted side-effect of executing the code on each hundredth iteration, which is unwanted (as far as I understand the problem). :) It would be interesting if Steve could divulge the greater problem that he is seeking a solution to. Cheers, David -- David Grant http://www.grant.org.uk/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unnecessary if statement? Programming technique
Hi, David is right about the unwanted side-effect. Thanks for the idea though. Unfortunately the 'greater problem' is not so great, I've just been doing this for a while now and find myself programming loops like these so often and I've never got round to testing if a simple IF statement is a major drain on the CPU. Somehow I doubt it. I got this reply from someone direct to my mail address, which seems to sum it up: -- In truth you are not evaluating the whole if block just the condition and since its such a simple condition I can't see how it would be at all taxing on the server. In your specific case I can't think of a better way to do it either. -- I'll try and think of a better example: ? $bool = true; // this is set dynamically and not known in advance while(true) { if($bool) { // this condition tested in every single loop // do first code } else { // do second code } } ? and I am wondering if the compiler is smart enough to turn this into: ? $bool = true; // this is set dynamically and not known in advance if($bool) { // this condition only tested once while(true) { // do first code } } else { while(true) { // do second code } } ? I realise this might be hard to follow without giving specific examples and code. In this case, the coding style of the 2nd example seems far better, but sometimes the 2 blocks of code are practically identical and it's a programmer's nightmare to have the blocks of code in 2 places and to remember to keep them both updated. I'm also assuming that using function calls is also much slower than evaluating a very simple IF statement. Thanks for your interest. Best wishes, Steve David Grant [EMAIL PROTECTED] schreef in bericht news:[EMAIL PROTECTED] Jared Williams wrote: Why not for ($i = 0; $i 100/100; ++$i) This involves dividing 100 by 100 for each iteration of the loop. It would be better to test against 1. There is also the unwanted side-effect of executing the code on each hundredth iteration, which is unwanted (as far as I understand the problem). :) It would be interesting if Steve could divulge the greater problem that he is seeking a solution to. Cheers, David -- David Grant http://www.grant.org.uk/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unnecessary if statement? Programming technique
Hi, I imagine this kind of thing is not especially taxing on the processor, especially if the condition is a fairly simple comparison. That said, I have very little understanding aside from my own limited experience of what runs slowly! If you're worried about code maintenance, then move the code out to a function and pass the parts that vary as parameters. Cheers, David Grant Steve McGill wrote: Hi, David is right about the unwanted side-effect. Thanks for the idea though. Unfortunately the 'greater problem' is not so great, I've just been doing this for a while now and find myself programming loops like these so often and I've never got round to testing if a simple IF statement is a major drain on the CPU. Somehow I doubt it. I got this reply from someone direct to my mail address, which seems to sum it up: -- In truth you are not evaluating the whole if block just the condition and since its such a simple condition I can't see how it would be at all taxing on the server. In your specific case I can't think of a better way to do it either. -- I'll try and think of a better example: ? $bool = true; // this is set dynamically and not known in advance while(true) { if($bool) { // this condition tested in every single loop // do first code } else { // do second code } } ? and I am wondering if the compiler is smart enough to turn this into: ? $bool = true; // this is set dynamically and not known in advance if($bool) { // this condition only tested once while(true) { // do first code } } else { while(true) { // do second code } } ? I realise this might be hard to follow without giving specific examples and code. In this case, the coding style of the 2nd example seems far better, but sometimes the 2 blocks of code are practically identical and it's a programmer's nightmare to have the blocks of code in 2 places and to remember to keep them both updated. I'm also assuming that using function calls is also much slower than evaluating a very simple IF statement. Thanks for your interest. Best wishes, Steve David Grant [EMAIL PROTECTED] schreef in bericht news:[EMAIL PROTECTED] Jared Williams wrote: Why not for ($i = 0; $i 100/100; ++$i) This involves dividing 100 by 100 for each iteration of the loop. It would be better to test against 1. There is also the unwanted side-effect of executing the code on each hundredth iteration, which is unwanted (as far as I understand the problem). :) It would be interesting if Steve could divulge the greater problem that he is seeking a solution to. Cheers, David -- David Grant http://www.grant.org.uk/ -- David Grant http://www.grant.org.uk/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unnecessary if statement? Programming technique
This is probably going to sound strange, but I like to try to think outside the box (buzzphrase!) and hit things at odd angles. Would someone care to test (or already know) the performance difference between a for loop and a foreach loop? Or the performance difference over many iterations of if (something == somethingelse) versus if (true)? Two examples to test: ?php $arr = array_fill(0, 99, 'echo \test standard\;'); $arr[100] = 'echo \test special\;'; foreach ($arr as $value) { eval($value); } ? or ?php $arr = array_fill(0, 99, true); $arr[100] = false; foreach ($arr as $value) { if ($value) { // do standard } else { // do special } } ? I'd be curious to see the benchmarks. I wonder if a if() versus doing the value check in a for statement are different speed-wise.. I'm wondering how much eval() slows it down.. and I'm wondering if if (value == value2) is slower than if (true). Probably other offbeat ways of doing something like this too. -TG = = = Original message = = = Hi everyone Quick question: If I have such a loop: ? for($i=0;$i100;$i++) if($i==100) // do something special for this occurence // do something standard ? In this case it seems such a waste that the if() statement is done 99 times when it's not needed. Is there any obvious trick that I am missing? I'm not sure how taxing a simple if() statement is on a server, maybe it's negligible, or is it something to worry about? Something which I'd prefer NOT to do: ? for($i=0;$i100;$i++) // do something standard // do something special for $i = 100 for($i=101;$i100;$i++) // do something standard ? as I would have have to either keep two copies of the code or write a function just for this purpose, which hardly seems worth it. Thanks to anyone who takes the time to think about my question and/or respond. Best wishes, Steve McGill ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Single SQL-statement in PHP to order...
Hi there! I guess this is off-topic? but I want to know if this is possible. (If there's anyone out there that have a solution, I would appreciate it though) I want to know if there is anyway of showing tables in PHP in an order like this in a single SQL-statement... Table is like this: IDCategory 7 15 4 3 I want to order the table like this: First everything that is IDCategory 4, then the rest of the posts (doesn't matter which order) SELECT *, 1 AS sequence FROM Table WHERE IDCategory = 4 UNION SELECT *, 2 AS sequence FROM Table WHERE IDCategory != 4 ORDER BY sequence ASC Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Single SQL-statement in PHP to order...
Hi there! I guess this is off-topic? but I want to know if this is possible. (If there's anyone out there that have a solution, I would appreciate it though) I want to know if there is anyway of showing tables in PHP in an order like this in a single SQL-statement... Table is like this: IDCategory 7 15 4 3 I want to order the table like this: First everything that is IDCategory 4, then the rest of the posts (doesn't matter which order) /G @varupiraten.se -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Single SQL-statement in PHP to order...
select ... from ... ORDER BY (IDCategory = 4) DESC, IDCategory = 4 will return 1 or 0, for 4 or not 4. 1 is bigger than 0, in most countries. :-) On Mon, April 18, 2005 2:50 pm, Gustav Wiberg said: Hi there! I guess this is off-topic? but I want to know if this is possible. (If there's anyone out there that have a solution, I would appreciate it though) I want to know if there is anyway of showing tables in PHP in an order like this in a single SQL-statement... Table is like this: IDCategory 7 15 4 3 I want to order the table like this: First everything that is IDCategory 4, then the rest of the posts (doesn't matter which order) /G @varupiraten.se -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: SQL statement - date
Jacques wrote: How should I formulate my sql statement to create a result set of members who registered between now and 7 days ago? I have tried the following and it is obviously incorrect: $sqlnewmembers = Select uid, uprofilename from tblusers where udatereg between (date() and date() - 7); Regards Jacques this should work (please adjust date format to your needs): BETWEEN date('Y-m-d') AND date('Y-m-d',mktime(0, 0, 0, date(m), date(d)-7,date(Y))); Regards Kyriacos -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: SQL statement - please help
Try a MySQL list. Or possibly even php-db. -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHPsubmitform=Find+search+plugins signature.asc Description: OpenPGP digital signature
Re: [PHP] in_array w/statement
reason it seems to always be true, ... something i'm doing wrong? btw, i cannot add the in_array to the statement because if the $buddylist is empty it will generate errors because of the empty implode. you could add the is_array() check. $buddylist = preg_split('/( )+/', trim($userinfo['buddylist']), -1, PREG_SPLIT_NO_EMPTY); if($buddylist) { $buddy = in_array($uname['uid'], array(implode(',', $buddylist))); } not sure I understand this, implode returns a string, then you are putting that string into the array() function. I dont know what kind of data you would get back from that. $buddylist should already be an array. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php