futrose Posted August 3, 2011 Share Posted August 3, 2011 I have been trying to fix this code snippet because I need to do something different if my variable is empty. $result = mysqli_query($link, "Select eldercatid from eldercategory where elderid = '$id'"); if (!$result) { $error = 'Error fetching elder details ' . mysqli_error($link); include '../../includes/error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $selected[] = array('eldercatid' => $row['eldercatid']); } here is the code being used on the page using the variable foreach ($selected as $s); if ($ec['id'] == $s['eldercatid']) { echo 'selected = "yes"'; } I need my $selected variable to be equal to anything if $row['eldercatid'] is blank or empty because on the next page I have a call to use $selected but I am getting an error that says undefined variable: selected. I have tried all kinds of lines using if empty and if ($row['eldercatid'] != '') among other stuff but haven't had any luck. I know this probably pretty basic stuff, I just don't know it well enough yet. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/ Share on other sites More sharing options...
phpSensei Posted August 3, 2011 Share Posted August 3, 2011 It means you havn't defined $selected as an array outside the while loop. Also your going about this the wrong way, you can simply check for a match inside the while loop instead of putting inside an array, or you can even do a simple Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251047 Share on other sites More sharing options...
futrose Posted August 3, 2011 Author Share Posted August 3, 2011 a simple what? You left me hanging. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251049 Share on other sites More sharing options...
phpSensei Posted August 3, 2011 Share Posted August 3, 2011 sorry haha, You can do a simple mysql query to check the column field with $selected, and do a mysql_num_rows to check for the number of fields returned. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251051 Share on other sites More sharing options...
futrose Posted August 3, 2011 Author Share Posted August 3, 2011 can you give me an example...? I understand the logic behind your answer but I'm not sure what the code would be to do it. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251055 Share on other sites More sharing options...
phpSensei Posted August 3, 2011 Share Posted August 3, 2011 <?php $result = mysqli_query("SELECT * FROM `eldercategory` WHERE `elderid` = '$id'"); while($row = mysql_fetch_array($result)){ if($ec['id'] == $row['elderid']){ // selected does equal to the $ec['id'] }else{ // does not equal } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251058 Share on other sites More sharing options...
trq Posted August 3, 2011 Share Posted August 3, 2011 That was a terrible example. <?php $id = mysql_real_escape_string($ec['id']); if ($result = mysqli_query("SELECT elderid FROM eldercategory WHERE elderid = '$id'")) { if (mysql_num_rows($result)) { // match found } else { // no match found } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251060 Share on other sites More sharing options...
phpSensei Posted August 3, 2011 Share Posted August 3, 2011 Thorpe, how can you quickly assume $ec['id'] is directly from an HTTP VAR and not inside a while loop for another query? I simply used he own code so that he may change it himself if that was the case. I understand the second code he posted was outside the while loop, but I just thought he went about it the wrong way. If i had made a terrible example, thank you for posting a better one, I always appreciate that and so do the members. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251063 Share on other sites More sharing options...
futrose Posted August 3, 2011 Author Share Posted August 3, 2011 I think I may have been a little confusing. Let me explain better. I have 2 files, index.php and elders_modify.php Here is the code from index.php as I have it right now. $result = mysqli_query($link, "Select * from eldercategory where elderid = '$id'"); if (!$result) { $error = 'Error fetching elder details ' . mysqli_error($link); include '../../includes/error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { if($id != $row['elderid']) { $selected = '0'; } else { $selected[] = array('eldercatid' => $row['eldercatid']); } } the $id is pulled when an admin clicks on a staff member to edit this code tells the elders_modify.php file which categories in the drop down menu to mark as "selected". The code works fine when a staff member has a category assigned to them already. If a staff member does not have a category assigned yet than $selected is empty because $eldercatid is empty. here is the code from elders_modify.php ... <td>Staff Category:</td><td><select name="eldercatid[]" multiple> <?php foreach ($eldercat as $ec): ?> <option value="<?php echo $ec['id']; ?>" <?php if ($action == 'elder_edit') { foreach ($selected as $s); if ($ec['id'] == $s['eldercatid']) { echo 'selected = "yes"'; } } echo '>'; echo $ec['category']; ?></option> <?php endforeach; ?> </select> all I need to do is give $selected a value to carry over from index.php to elder_modify.php when a staff member has no category assigned yet. For some reason my $selected = '0'; isn't recognized. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251070 Share on other sites More sharing options...
futrose Posted August 3, 2011 Author Share Posted August 3, 2011 I got it to work... thanks for your help guys. I appreciated the examples. I just had to tweak them a bit but all is good for now. Maybe one day I'll have answers to post instead of just questions. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251076 Share on other sites More sharing options...
trq Posted August 3, 2011 Share Posted August 3, 2011 Thorpe, how can you quickly assume $ec['id'] is directly from an HTTP VAR and not inside a while loop for another query? It wasn't necessarily the usage of mysql_real_escape_string that I was harping on about. More the fact that you can (and should) do the actual comparison in your query. Quote Link to comment https://forums.phpfreaks.com/topic/243662-how-do-i-sayif-my-variable-is-blank-or-empty-do-xyz/#findComment-1251077 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.