Tboy_forum Posted May 22, 2008 Share Posted May 22, 2008 Hi all Yep, I'm a noob so go lightly please ;D Can someone please assist me with finding the conditional problem here. Its part functioning ??? I.O.W. - I'm not sure where the conditional problem lies. It executes the first part of the code. That is, it does assign values to $year, $month and $day but the condition is wrong - it executes whether the string length is 6 or 5 characters :'( ANY help is appreciated if (trim(strlen($user['user_birthday'])) != 6 ) { $year = substr($user['user_birthday'], 4, 4); $month = substr($user['user_birthday'], 0, 2); $day = substr($user['user_birthday'], 2, 2); } else if (trim(strlen($user['user_birthday'])) != 5 ) { if ($user['user_birthday'] AND $user['user_birthday'] != 999999) { if (trim(strlen($user['user_birthday'])) != 6 ) { $year = substr($user['user_birthday'], 4, 4); $month = substr($user['user_birthday'], 0, 2); $day = substr($user['user_birthday'], 2, 2); } else if (trim(strlen($user['user_birthday'])) != 5 ) { $year = substr($user['user_birthday'], 3, 4); $month = substr($user['user_birthday'], 0, 1); $day = substr($user['user_birthday'], 1, 2); } $try->set_value('nonmandatory', 'birthday', "{$month}-{$day}-{$year}"); $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}"); } Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/ Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 Um, first thing that jumps out at me is else if should be elseif Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547609 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 Already tried that without success Not sure if the problem lies here != 6 - its reading an integer field Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547615 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 oh yeah...duh...trim before strlen: if (strlen(trim($user['user_birthday'])) != 6 ) Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547620 Share on other sites More sharing options...
soycharliente Posted May 22, 2008 Share Posted May 22, 2008 Um, first thing that jumps out at me is else if should be elseif I use "else if" all the time with no problems. Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547622 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 really? didn't know that way worked... look at that...straight from the PHP site: "In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word)." Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547624 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 Tried swopping strlen and trim - still no go :'( Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547625 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 The code is difficult to understand with the break in the middle. What are the input and output values? What is the different between the real and expected output? Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547632 Share on other sites More sharing options...
soycharliente Posted May 22, 2008 Share Posted May 22, 2008 really? didn't know that way worked... look at that...straight from the PHP site: "In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word)." LOL. I didn't know that elseif worked. We both learned something. Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547633 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 The input data are dates stored as follows: either 1061962 or 11281978 Data stored in a MySql database. As can be seen, the length of the info is either 5 characters or 6 Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547639 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 the != needs to be == try this code: <?php if ($user['user_birthday'] && $user['user_birthday'] != 999999) { $bday = trim($user['user_birthday']); if (strlen($bday) == 6 ) { $year = substr($bday, 4, 4); $month = substr($bday, 0, 2); $day = substr($bday, 2, 2); } else if (strlen($bday) == 5 ) { $year = substr($user['user_birthday'], 3, 4); $month = substr($user['user_birthday'], 0, 1); $day = substr($user['user_birthday'], 1, 2); } else { die("Invalid Bday"); } $try->set_value('nonmandatory', 'birthday', "{$month}-{$day}-{$year}"); $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547641 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 Okay - now its dying ;D Obviously, the $bday string is not receiving a valid value to test against. Damn, wish I had a debugger for my 'localhost' Any other ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547648 Share on other sites More sharing options...
Darklink Posted May 22, 2008 Share Posted May 22, 2008 Don't use trim. You shouldn't need it. And don't use AND. Use && instead. Try this: <?php if ( strlen($user['user_birthday']) != 6 ) { $year = substr($user['user_birthday'], 4, 4); $month = substr($user['user_birthday'], 0, 2); $day = substr($user['user_birthday'], 2, 2); } elseif ( strlen($user['user_birthday']) != 5 ) { if ($user['user_birthday'] && $user['user_birthday'] != 999999) { if ( strlen($user['user_birthday']) != 6 ) { $year = substr($user['user_birthday'], 4, 4); $month = substr($user['user_birthday'], 0, 2); $day = substr($user['user_birthday'], 2, 2); } elseif ( strlen($user['user_birthday']) != 5 ) { $year = substr($user['user_birthday'], 3, 4); $month = substr($user['user_birthday'], 0, 1); $day = substr($user['user_birthday'], 1, 2); } $try->set_value('nonmandatory', 'birthday', "{$month}-{$day}-{$year}"); $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547654 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 Thanks for the assistance I am getting the following results 5 digit dates are returning: 10-61-962 instead of 01-06-1962. So it would seem the condition for 5 digits is not being met??? And 6 digit dates are returning: 11-28-1978 - which is correct. Its only the 5 digit dates which are giving me nightmares Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547664 Share on other sites More sharing options...
Darklink Posted May 22, 2008 Share Posted May 22, 2008 Hmm. You might have to be a bit more specific than saying "IF the length of this IS NOT 5 THEN" I'm not sure how you are trying to get it to work exactly. Just be more specific with your conditionals. Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547674 Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 omg...how did i miss that before...the lengths are 7 and 8, not 5 and 6 (at least with your examples) <?php if ($user['user_birthday'] && $user['user_birthday'] != 999999) { if (strlen($user['user_birthday']) == 8 ) { $year = substr($user['user_birthday'], 4, 4); $month = substr($user['user_birthday'], 0, 2); $day = substr($user['user_birthday'], 2, 2); } else if (strlen($user['user_birthday']) == 7 ) { $year = substr($user['user_birthday'], 3, 4); $month = substr($user['user_birthday'], 0, 1); $day = substr($user['user_birthday'], 1, 2); } else { die("Invalid Bday"); } $try->set_value('nonmandatory', 'birthday', "{$month}-{$day}-{$year}"); $try->set_value('nonmandatory', 'birthday_search', "{$year}-{$month}-{$day}"); } ?> 1061962 => 1-06-1962 11281978 => 11-28-1978 Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547676 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 The origin info is either: 1061962 - 5 digits outputs 1-06-1962 if checking for 5 digits is first 12251965 - 6 digits outputs 1-12-8197 and 6 digits is second What I have found is that whatever test condition is first, that is then the resulting output. I.O.W. the second condition is not being executed??? Either the one condition is correct on the output - never both Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547678 Share on other sites More sharing options...
Tboy_forum Posted May 22, 2008 Author Share Posted May 22, 2008 omg...how did i miss that before...the lengths are 7 and 8, not 5 and 6 (at least with your examples) You sir, are a genius ;D ;D ;D ;D I must have been dreaming!! I look at the problem and don't even see it - thanks a STACK for spotting that one - I thought I was going nuts Many thanks for all the assistance here, I really do appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/106819-solved-conditional-testing/#findComment-547681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.