Pecatto Posted July 22, 2015 Share Posted July 22, 2015 Hi together, I am quite new in PHP-Programming. Hope you can help me out. In general everything is working. I getting a couple of PHP-Notice: Undefined offset:24 for example. <select name="selectlist1" id=selectlistid1 size="1" > <option value="None">No Component</option> <?php $Counter1 =0; $Counter2 =0; while ($Counter1<$Counter3){ echo '<optgroup Label="'.$array1[$Counter1].'">'; while($array1[$Counter1] == $array3[$Counter2]){ echo '<option>' .$array2[$Counter2]. '</option>'; $Counter2++; } $Counter1++; echo '</optgroup>'; } ?> The failure is at the second while-loop. I separated each word to it's line and it's saying the failure ist the second counter of the second Array -> $Counter2 All Arrays + Counter 3 having values. I tried to insert "if ($Counter1==$Counter3){ break;}" but it doesnt work. (I put it between $Counter1++ and echo '</optgroup>'; Many Thanks in advance! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 22, 2015 Share Posted July 22, 2015 counter3? array3? Are these vars defined somewhere? Your code is really pretty meaningless to us since you don't fill us in on all the vars/values you are using. How about describing what you are trying to do? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 23, 2015 Share Posted July 23, 2015 (edited) It would be better if you did not store the option groups and option values in separate arrays. I would use the option group label as the key and assign the values for the group to it. Example this is how I would structure the array // options grouped in one array // the key is defined as the option group label // the options for the label are assigned to that key $options = array( 'Drinks' => array( 'Tea', 'Coffee', 'Orange Juice', 'Apple Juice' ), 'Food' => array( 'Toast', 'Ham Sandwich', 'Chicken Soup', ), 'Desert' => array( 'Ice Cream', 'Chocolate Moose', 'Jelly' ), ); Now creating the option groups and options becomes trivial using two foreach loops echo '<select name="menu">'; foreach($options as $optGroupLabel => $optgroupValues) { echo '<optgroup Label="'.$optGroupLabel.'">'; foreach($optgroupValues as $optionValue) { echo '<option>' .$optionValue. '</option>'; } echo '</optgroup>'; } echo '</select>'; Edited July 23, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 23, 2015 Share Posted July 23, 2015 Your problem is that the variables used to identify the index contain values that do not exist as indexes in those two arrays. Either the indexes in one or both of those arrays are not sequential or the max limit ($Counter3) is higher than the indexes in those two arrays. When dealing with arrays, you should almost always be using the foreach() loops to iterate through the values, as Ch0cu3r shows, rather than trying to programatically determine the indexes to use. I'll add some more suggestions: Give your variables meaningful names that give an indication as to what they contain. Otherwise, you spend too much time trying to figure out what is what - especially when you have to go back to some code in the future. Separate your logic (PHP) from your presentation (HTML). This makes your code much more maintainable and flexible. In this case, rather than stick PHP code to create the options between the <select> tags you could put the PHP code at the top of the page and store the output for the options in a variable. Then just include an echo statement in the HTML where the <select> tags are. Also, with all due respect to Ch0cu3r, I would suggest including line breaks (and even spacing) in the HTML output using \n and \t. It makes it much easier to debug issues in the HTML code. Example: <?php //Array of menun groups and options $menuOptionsAry = array( 'Drinks' => array( 'Tea', 'Coffee', 'Orange Juice', 'Apple Juice' ), 'Food' => array( 'Toast', 'Ham Sandwich', 'Chicken Soup', ), 'Desert' => array( 'Ice Cream', 'Chocolate Moose', 'Jelly' ), ); //Create the default value for the menu options list $menuOptions = "\t<option value='None'>No Component</option>\n"; //Create the dynamic values for the menu options list foreach($menuOptionsAry as $menuGroupLabel => $menuGroupValues) { $menuOptions .= "\t<optgroup label='{$menuGroupLabel}'>\n"; foreach($menuGroupValues as $menuValue) { $menuOptions .= "\t\t<option value='{$menuValue}'>{$menuValue}</option>'; } $menuOptions .= "\t</optgroup>\n"; } ?> <html> <head> <title>Sample Page</title> </head> <body> Select a menu item: <form> <select name="menu"> <?php echo $menuOptions; ?> </select> </form> </body> </html> 1 Quote Link to comment 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.