Shadowing Posted May 14, 2012 Share Posted May 14, 2012 hey guys having troubles figuring out my problem here Just a standard populating drop down box I must be over looking something here. I should have two values from the away which I do print_r checks out fine on $mods. so nothing wrong with the array. Notice: Undefined index: name_list When I see this error I think name_list isnt defined. Only one of the two names display in the drop down box the other one displays out side of it. The error started after adding a 2nd name to the drop down box. $result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); $mods = array(); while($raw = mysql_fetch_assoc( $result )) { $mods[] = $raw; } echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">'; foreach($mods as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST ['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options></select></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/ Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 hey guys having troubles figuring out my problem here Just a standard populating drop down box I must be over looking something here. I should have two values from the away which I do print_r checks out fine on $mods. so nothing wrong with the array. Notice: Undefined index: name_list When I see this error I think name_list isnt defined. Only one of the two names display in the drop down box the other one displays out side of it. The error started after adding a 2nd name to the drop down box. $result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); $mods = array(); while($raw = mysql_fetch_assoc( $result )) { $mods[] = $raw; } echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">'; foreach($mods as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST ['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options></select></td>'; ... print_r( $_POST ); Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345221 Share on other sites More sharing options...
xyph Posted May 14, 2012 Share Posted May 14, 2012 You don't close your foreach before you close your </select> Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345222 Share on other sites More sharing options...
Shadowing Posted May 14, 2012 Author Share Posted May 14, 2012 thanks xyph that fixed the problem of the 2nd name not being in the drop down box. the error notice still exist though. hmm I did print_r($_POST['name_list']); and got the same error Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345229 Share on other sites More sharing options...
Shadowing Posted May 14, 2012 Author Share Posted May 14, 2012 here is the new code now after the change to the foreach bracket $result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); $mods = array(); while($raw = mysql_fetch_assoc( $result )) { $mods[] = $raw; } echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">'; foreach($mods as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; } echo '</select></td><td class="submit"><input type="submit" name="remove" id="remove" value="Remove"></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345236 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 thanks xyph that fixed the problem of the 2nd name not being in the drop down box. the error notice still exist though. hmm I did print_r($_POST['name_list']); and got the same error It's because you haven't posted the form yet, so the data for name_list does not exist in the php $_POST array. Therefore you are getting an error telling you that the index(['name_list']) does not exist in the array. It's not going to mess up execution of your script and those errors should not be displayed anyway in a production environment. But for now you can suppress it like so: $result = mysql_query("SELECT name FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); $mods = array(); while($raw = mysql_fetch_assoc( $result )) { $mods[] = $raw; } echo '<td colspan="3"><select name="name_list" class="textbox" id="name_list">'; foreach($mods as $key => $value) { echo '<option value="' . $value['name'] . '" ' . ($value['name'] == @$_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['name'] . '</options>'; } echo '</select></td><td class="submit"><input type="submit" name="remove" id="remove" value="Remove"></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345237 Share on other sites More sharing options...
Shadowing Posted May 14, 2012 Author Share Posted May 14, 2012 wierd i dont have this issue with other drop down boxes. Im not a big fan of using @ I over looked the bracket being before the end select tag when comparing it to the other drop down boxes i have. but the other ones i have are exactly the same now some times if i do have a undefine variable i'll do $name_list = !isset($name_list) ? '' : $name_list; hmm Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345240 Share on other sites More sharing options...
Shadowing Posted May 14, 2012 Author Share Posted May 14, 2012 another thing if i remove the name i added so its only populating one value. the error goes away Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345243 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 wierd i dont have this issue with other drop down boxes. Im not a big fan of using @ I over looked the bracket being before the end select tag when comparing it to the other drop down boxes i have. but the other ones i have are exactly the same now some times if i do have a undefine variable i'll do $name_list = !isset($name_list) ? '' : $name_list; hmm I generally use the @ to save bytes, especially for something as simple as checking the value of a variable that may or may not exist. I also understand how you feel about not wanting to suppress errors, should they exist. Just a personal preference really. That's weird that the error is not showing up when it's only populating from 1 result, it doesn't seem like that should make any difference in the post array. Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345244 Share on other sites More sharing options...
Shadowing Posted May 14, 2012 Author Share Posted May 14, 2012 I see whats going on now For starters i coded a long time with out using all errors, which is bad cause its real good for seeing mistakes. my other drop down boxes are using a php variable in place of name_list on the orginal example. echo '<option value="' . $value['name'] . '" ' . ($value['name'] == $current['name_list'] ? 'selected="selected"' : '') I see what you are saying now how the variable doesnt exist cause the html doesnt exist i dont understand why it would matter if im displaying one result or more. what the heck lol thanks alot for the help. much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345245 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 Happy to. Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345254 Share on other sites More sharing options...
xyph Posted May 14, 2012 Share Posted May 14, 2012 The @ error suppressor is never the 'right' way to handle an error. The 'right' way to do this is (isset($_POST['name_list']) && $value['name'] == $_POST['name_list'] ? 'selected="selected"' : '') Check if the variable exists before trying to use it. Quote Link to comment https://forums.phpfreaks.com/topic/262491-undefine-index-issue/#findComment-1345390 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.