Weird problem with If vs ternary operator unexpected results |
[eluser]edjon2000[/eluser]
Hi all This is a peculiar problem I have come across, where some code should work, but does not. First of all I am using CI 2.1.0 and have been using this to create a recruitment website for a client. I have recently been asked to provide a Mass Email type feature on this site so his recruitment clients can all be informed of new prospects when they become available. In order to keep things simple for him, it tends to make the coding more difficult for me lol (always the case). So, in summary, the sequence of actions required to send these emails work like this:- From admin page mass emails section, choose "select clients", this generates a page with a search form that has the client-specific search fields and some instructions, details are entered into this form (e.g. Location: Manchester) and submitted, this in turn generates a table of results detailing the clients that match the search criteria along with an additional field which is a simple dropdown menu with the options Yes or No(default is yes) to further fine-tune the returned results(i.e. to determine whether or not to include a specific client within the returned results on the email list or not. As there is a form field(the dropdown list) the whole table is enclosed in form tags and once that is submitted it passes an array of results via $_POST back to the controller which, in turn is converted to a simple $post array using Code: $post = $_POST So part of the code I used in the model looks like this:- Code: /** I apologise for the long initial post but hopefully it will make any follow-up posts a bit clearer Jon
[eluser]Samus[/eluser]
I know CI user guide recommends we use '===' rather than '==', but within my apps i've always found that '==' never gives me problems. Try it out and see if that fixes your problem EDIT: Upon further research i've found this article: http://www.jonlee.ca/the-triple-equals-in-php/ Have a read, it makes good sense of when to use == vs ===
[eluser]edjon2000[/eluser]
Hi Samus thank you for for response, I already tried out "==" and that did not work either What actually happens is that I get 0 results. I bet it is a simple syntax error, but, I cannot see it :-S Jon
[eluser]Samus[/eluser]
[quote author="edjon2000" date="1333839345"]Hi Samus thank you for for response, I already tried out "==" and that did not work either What actually happens is that I get 0 results. Jon[/quote] My bad then. You could probably enable profiler to help in your debugging and check if anything's actually coming in the post. Just an idea..
[eluser]edjon2000[/eluser]
Hey Samus, No need to say "my bad" I appreciate any responses from the community It's just a strange problem that shouldn't really happen, this site started off as a simple project and has now become massive but, at the end of the day, I want to be able to make websites that are user friendly and I have found that CI deals with a lot of things in the background. Jon
[eluser]InsiteFX[/eluser]
That's because === only checks for true boolean TRUE or FALSE unlike == that check for equal to! change your code to == and it should work!
[eluser]Aken[/eluser]
There's nothing wrong with your ternary functions. Which means there's something wrong with the data. Try to var_dump your $post array in the function and see what's ACTUALLY being passed. Because if you're expecting one thing and getting another, then you might think something else is the problem.
[eluser]edjon2000[/eluser]
Hi InsiteFX, [quote author="InsiteFX" date="1333855295"]That's because === only checks for true boolean TRUE or FALSE unlike == that check for equal to! change your code to == and it should work! [/quote] Yes thats quite right "==" means "equal to" and "===" means "exactly equal to" the ternary operator is essentially a simple logical switch Code: $phone = ($post['first_contact_phone'] === 'All Phone Numbers') ? '' : $post['first_contact_phone']; @Aken for this example I have only filled in the phone number field Code: <pre>Contents of the $post array BEFORE processing => array(6) { This is using the if sections of code rather than the ternary equations and it appears to work as expected. This is using the ternary equations with "==" Code: <pre>Contents of the $post array BEFORE processing => array(6) { Jon
[eluser]edjon2000[/eluser]
Just for an example I switched the equation about as follows:- This is the original equation Code: $name = ($post['name'] == 'All Companies') ? '' : $post['name']; Code: $name = ($post['name'] !== 'All Companies') ? $post['name'] : ''; As there is a bit of a time constraint on this particular project I will use the if versions for now - I know that they aren't pretty but they do work. I mentioned earlier that the results of these equations contribute to the WHERE clause of the database query, so, to give you an idea of where I was aiming to go, here is the full model method Code: /** I would like to thank you all for your help, I really appreciate it, and, I am sure, I will have a few more dumb questions further down the line Jon.
[eluser]Aken[/eluser]
[quote author="edjon2000" date="1333870400"]This is using the ternary equations with "==" Code: <pre>Contents of the $post array BEFORE processing => array(6) { Jon[/quote] They're not returning false, they're working just as they should. The $post array is not changing because you are not modifying the $post array when using the ternary operations. You are creating and assigning values to new variables. |
Welcome Guest, Not a member yet? Register Sign In |