Jump to content

Undefined offset in while-loops


Pecatto

Recommended Posts

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! 

 

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.