![]() |
Best way to check if variable is empty
I have a variable called $messagesToUpdate that holds any and all Messages that have been checked so that an action can be taken on them.
This variable is populated using the implode function if values were submitted in one of two $_POST arrays. An example of what this variable would hold if the User selected some Messages to take actions on, would be this... Code:
$messagesToUpdate = 52, 51, 49, 39, 38, 10, 8, 6, 5, 2, 1So on to my question... If this variable has at least one value, then I want to run an UPDATE query, but if it is blank then the query should not be run. What is the best way to check if this variable is blank? I was starting to use this, but am not sure if it covers all cases... Code:
if (!empty($messagesToUpdate)){Suggestions?? Thanks, Debbie |
Maybe this is better...
Code:
if (isset($messagesToUpdate) && $messageToUpdate){Debbie |
PHP Code:
|
For this you need to describe empty in your definition of it. From what I gather, you do not consider a space delimited string to be empty (and nor should you as it is not empty), so you would use the empty() function for that. Empty is true so long as the item it is given equates to false, so that will be false, 0, 0.0, "", "0", array(), null and uninstantiated object properties.
Assuming that 0 is never valid and you cannot possibly end up with spaces than empty is sufficient. Or you can never convert it to a string and retain it as an array and use the empty or count on the array instead. Arrays are far more useful than a string anyways. |
Quote:
In my HTML section, I have this code, which populates the selectedMsgArray with an entry for each Private Message that is checked... PHP Code:
Then at the top of my script - in the section handles the submitted Form - I have this snippet... PHP Code:
Above you can see the code I commented out, and what I have now, which seems to make sense. Follow me? Debbie |
I'm not saying it won't work, I'm saying you're doing more work than you need.
There is no reason to convert to a string prior to checking the count. I'm not sure why you are passing a msgArray on top of a selectedMsgArray; if you are updating all do you really need the msgArray? Maybe its for a pagination option so you only update a page at a time (which is fine)? In any case, keep it as an array until you need to actually use it. PHP Code:
|
Quote:
However... Quote:
I want it so if the User checks the Top Check-Box, then whatever "action" they choose will apply to all Messages. In order to make that happen, I need to do this... PHP Code:
That works great if you want to apply some "action" to everything, but I also need the ability for a User to cherry-pick the 1st, 3rd, 5th, and 6th Messages and have the "action" apply to just those selected ones. For that I chose to store things in a separate array. probably not the most efficient, but I have been screwing with this script for the last 3 weeks, and I need to get it done, so that is what I went with. Feel free to offer a better approach - without JS - if you want! Thanks, Debbie |
There's hardly anything wrong with looping and a prepared statement. At least this way you only send the data chunk between the dbms and the PHP instead of the whole statements, so that's okay. It is admittedly more complex to use an IN with a prepared statement if you don't know the exact number of options:
PHP Code:
Still not seeing the need for msgArray. If you are referring to everything, than what reason would you need to send this information back to the server for? The server came up with the data in the first place, so would it not be easier to update on the same criteria that the select was given if it detects that the selectAll checkbox has been selected? Always assume that there is no JS available. JS should be massaged to talk to PHP, not the other way around. Edit: BTW, using PDO would actually be easier for an IN clause. The PDO library binds on a call by call basis while the mysqli only allows a single call to bind. |
Quote:
Quote:
|
| All times are GMT +1. The time now is 06:11 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.