rockstarrem Posted August 6, 2010 Share Posted August 6, 2010 Hey guys, Really dumb question, but I want to check if a bunch of variables are all not empty. Then, if they aren't, I want to execute a mySQL query. Right now I have this which I know is wrong because it doesn't work the right way: if($album != null || $artist != null || $year != null || $genre != null) { // stuff here... } Obviously I want all of those variables to be checked if they are empty or not. I did a quick Google and found that checking if the variables are null wouldn't be the correct way of solving this situation because an "empty string" is still not null? Anyway, any help would be appreciated. I'm pretty sure it's really simple . Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/ Share on other sites More sharing options...
sspoke Posted August 6, 2010 Share Posted August 6, 2010 check all at once. replace $_REQUEST WITH $_POST OR $_GET or leave $_REQUEST to check both if you are unsure. foreach($_REQUEST as $name => $value) { if($value == "") { echo $name." is empty"; } } this supports POSTS/GETS i'm not sure what you are using. you can ofcourse use strlen() to get length of string if it's 0 in size then it's empty.. or even the function empty() although if you are submitting a number 0 is considered empty too.. so up 2 u. Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1095857 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2010 Share Posted August 6, 2010 This assumes that the OP is looking the check variables from a form or the URL, which wasn't stated. If that's not the case you can do something like this: <?php $vars = array('var1','var2','var3','var4'); $var1 = 'x'; $var3 = ''; $var2 = 'x'; $var4 = 'x'; $filled = false; foreach ($vars as $var) { if (isset($$var) && strlen($$var) > 0) { $filled = true; } else { $filled = false; } } if (!$filled) { echo "all the variables are not filled\n"; } else { echo "all the variables are filled\n"; } ?> Just put all the variables you want to test in the array (without the $). Ken Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1095863 Share on other sites More sharing options...
rockstarrem Posted August 6, 2010 Author Share Posted August 6, 2010 Hey guys, Thanks for the help! It does exactly what I want it to. Just another question though, say I want it to return something else other than "variable is empty.", like, instead of cd_genre is empty, I want it to say CD Genre is empty. How would I do this? Again, thanks for the help . Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096027 Share on other sites More sharing options...
wildteen88 Posted August 6, 2010 Share Posted August 6, 2010 If you're using kens code then modify the array to be // variable => label $vars = array( 'var1' => 'Variable 1', 'var2' => 'Variable 2', 'var3' => 'Variable 3'); Basically you're setting the variable as the key, then assigning the label as the value. Now change the loop to $errors =array(); foreach ($vars as $var => $label) { if (isset($$var) && strlen($$var) == 0) { $errors[$var] = $label . 'is empty'; } } When displaying the errors you can either display them individually , for example next to the form field if(isset($error['variable_here'])) echo $error['variable_here']; Or display them in one go if(is_array($errors) && count($errors) > 0) { echo 'Please fix the following errors: '; echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'; } Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096029 Share on other sites More sharing options...
rockstarrem Posted August 6, 2010 Author Share Posted August 6, 2010 I'm not using Ken's code because I was unsure if it would work with user input or not. I'm using sspoke's code. I was also wondering if I could put all the variables that I'm using in an array instead of checking $_REQUEST, like this: $variables = array($cd_album, $cd_artist, $cd_year, $cd_genre); That way I could take out variables in the future that I don't want to be checked on that page. But I'm still wondering about my last question too because I'm using sspoke's code. Thanks for all the help . Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096033 Share on other sites More sharing options...
wildteen88 Posted August 6, 2010 Share Posted August 6, 2010 This is what ken's code does. Add the full variable name but without the dollar sign eg do $vars = array( "_POST['cd_name']" => 'CD Name', 'var1' => 'Variable 1', 'var2' => 'Variable 2', 'var3' => 'Variable 3'); You can add any variable to the array, just ommit the dollar sign. Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096048 Share on other sites More sharing options...
rockstarrem Posted August 6, 2010 Author Share Posted August 6, 2010 I switched to Ken's code and did as you told me and now whenever I submit anything, even when I do fill out the form with content it still says that all variables are empty. I should also note that before when I had it working it submitted the form four times, which is the number of variables I'm using. I'm guessing that's from the foreach? Here's the full code: <?php $album = $_POST["cd_album"]; $artist = $_POST["cd_artist"]; $year = $_POST["cd_year"]; $genre = $_POST["cd_genre"]; require_once("mysqli.connect.php"); $vars = array( 'album' => 'CD Album', 'artist' => 'CD Artist', 'year' => 'CD Year', 'genre' => 'CD Genre' ); $filled = false; foreach ($vars as $var) { if (isset($$var) && strlen($$var) > 0) { $filled = true; //Prepare formatting for mySQL. $album = $mysqli->real_escape_string($album); $artist = $mysqli->real_escape_string($artist); $year = $mysqli->real_escape_string($year); $genre = $mysqli->real_escape_string($genre); $query = "INSERT INTO cd (album, artist, year, genre) VALUES ('$album', '$artist', '$year', '$genre')"; $mysqli->query($query, MYSQLI_STORE_RESULT); printf("Added: (%s) %s - %s GENRE: %s", $year, $album, $artist, $genre); } else { $filled = false; } } if (!$filled) { echo "all the variables are not filled\n"; } else { echo "all the variables are filled\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096052 Share on other sites More sharing options...
wildteen88 Posted August 6, 2010 Share Posted August 6, 2010 Oops sorry. I was referring to the modifications I made to kens code. Your code should be like <?php require_once("mysqli.connect.php"); $album = $mysqli->real_escape_string($_POST["cd_album"]); $artist = $mysqli->real_escape_string($_POST["cd_artist"]); $year = $mysqli->real_escape_string($_POST["cd_year"]); $genre = $mysqli->real_escape_string($_POST["cd_genre"]); $vars = array( 'album' => 'CD Album', 'artist' => 'CD Artist', 'year' => 'CD Year', 'genre' => 'CD Genre' ); $errors = array(); foreach ($vars as $var => $label) { if (isset($$var) && strlen($$var) == 0) { $errors[$var] = $label . 'is empty'; } } if(is_array($errors) && count($errors) > 0) { echo 'Please fix the following errors: '; echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'; } else { $query = "INSERT INTO cd (album, artist, year, genre) VALUES ('$album', '$artist', '$year', '$genre')"; $mysqli->query($query, MYSQLI_STORE_RESULT); printf("Added: (%s) %s - %s GENRE: %s", $year, $album, $artist, $genre); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096057 Share on other sites More sharing options...
rockstarrem Posted August 6, 2010 Author Share Posted August 6, 2010 Thanks a lot! Works perfectly. Really appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/209955-check-that-multiple-variables-arent-empty/#findComment-1096058 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.