Associative arrays?

I have a form that generates lists with varying number of list items. I’m new to php and testing various ideas.
I can generate the arrays from the form but the way I"m trying to echo them is including the list items from both list in each list. I would like to show the unique items from list 1 only in list 1 and so on. Here is how I’m caputing the form data and generating the output.

$lists = ($_POST['listname']);
$listitems = ($_POST['listitem']);
$array = array(($lists),
				($listitems));
	foreach($lists as $list) {
		echo '<ul>'.$list;
			foreach($listitems as $listitem) {
	echo '<li>' . $listitem . '</li>';
	}

	echo '</ul>';
}
	?>

Any pointer would be appreciated.

How do you know what list item belongs to what list? All you have is two separate arrays right now.

Sorry about that!. Here is the simple form that populates each list. Eventually in my testing I will go from one list with a set number of list items to several lists with a variation of list items.

<form method="post" action="list.php">
<label>List Name</label>
<input name="listname" type="text" size="4" />
<label>list item</label>
<input name="item" type="text" size="4" /> 
<label>list item</label>
<input name="item" type="text" size="4" /> 
<label>list item</label>
<input name="item" type="text" size="4" /> 

<label>List Name</label>
<input name="listname" type="text" size="4" />
<label>list item</label>
<input name="item" type="text" size="4" /> 
<label>list item</label>
<input name="item" type="text" size="4" /> 

<input type="submit" name="submit" value="Build" />
</form>

You can’t use that form to populate the array you want. There is still no connection between the items and the list names. The fact that you get arrays back at all is the browsers doing a favor for you, as you didn’t name the inputs as arrays. A less lenient browser would give you back just one value for listname and one value for item.

Perhaps an example would be more clear:


<form action="list.php" method="post">

<label>List Name</label>
<input name="listname[0]" type="text" size="4" />
<label>list item</label>
<input name="item[0][0]" type="text" size="4" />
<label>list item</label>
<input name="item[0][1]" type="text" size="4" />
<label>list item</label>
<input name="item[0][2]" type="text" size="4" />
<label>list item</label>
<input name="item[0][3]" type="text" size="4" />

<label>List Name</label>
<input name="listname[1]" type="text" size="4" />
<label>list item</label>
<input name="item[1][0]" type="text" size="4" />
<label>list item</label>
<input name="item[1][1]" type="text" size="4" />
<label>list item</label>
<input name="item[1][2]" type="text" size="4" />
<label>list item</label>
<input name="item[1][3]" type="text" size="4" />

<input type="submit" name="submit" value="Build" />
</form>

Your PHP code will now be able to tell what items go with what list names. $_POST would look like this:

Array
(
    [listname] => Array
        (
            [0] => first list
            [1] => second list
        )

    [item] => Array
        (
            [0] => Array
                (
                    [0] => one
                    [1] => two
                    [2] => three
                    [3] => four
                )

            [1] => Array
                (
                    [0] => five
                    [1] => six
                    [2] => seven
                    [3] => eight
                )

        )

    [submit] => Build
)

Code to turn that into a list:

foreach ($_POST['listname'] as $index => $name) {
  echo $name;
  echo "<ul>";
  foreach ($_POST['item'][$index] as $listitem) {
    echo "<li>" . $listitem . "</li>";
  }
  echo "</ul>";
}

Sample output:

first list

  • one
  • two
  • three
  • four

second list

  • five
  • six
  • seven
  • eight

This is not the only way to go about it, but the point is that the visual order of your inputs in the form is not something you have access to in your PHP code. You need to make the posted data carry the associations.

Oh man. Now I’m going backwards. I actually have a form where I can choose how many lists I want and how many list items to include in each lists. At this point the number of list items is standard across all list generated. For example if I choose 3 in the number of lists field and 4 in the list items field, when I select submit, it generates another form with the three lists each with 4 list items. From that form I can then choose the name of each list and the text for the list items. As you just pointed out clearly I can then populate the list details with your techniques. I’ll have to go back to my original output to see what I can do to output the array indicators. I may be back for more help but I’ll try to figure it out on my own first.

thanks a bunch Dan

Code to turn that into a list:


```php
foreach ($_POST['listname'] as $index =&gt; $name) {
  echo $name;
  echo "&lt;ul&gt;";
  foreach ($_POST['item'][$index] as $listitem) {
    echo "&lt;li&gt;" . $listitem . "&lt;/li&gt;";
  }
  echo "&lt;/ul&gt;";
}


By the way. Thanks for this code block. I'd never seen this before and I'm sure it will open up a new array of ideas and new directions.

If your still following this Dan, I was able to reproduce your form dynamically with my original dynamically generated form in the following manner. I’m not sure if this is the best way to do this but it works. If you would, let me know if there are any possible issues with this like efficiency or even security. Thanks.
Here is the form that is generated dynamically and includes the array counts the way your form does except there is no [0] starting point like all arrays start out with. I’ve been documenting this with several files saved so maybe there is something I can detect further back causing that issue. Anyway here is the code used to produce your style of form.
First the original form used to produce the number of lists desired.

<h2>Another for loop awesome</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Create New List</label>
<label>Enter number of lists</label>
<input name="list" type="text" size="4" />
<label>Enter number of items in each list</label>
<input name="item" type="text" size="4" />
<input type="submit" name="submit" value="Build" />
</form>

Here is the code that is generated dynamically when the submit button "build is activated. I was able to get the array by inserting the $u and $l variables for the number of the count into the echo statement.

if (isset($_POST['submit'])) {
echo '<form method="post" action="message.php">';
	$ul = $_POST['list'];
	$li = $_POST['item'];
	for ($u=1; $u <= $ul;$u++) {
		
		echo "<ul><label>List Name</label><input type='text' name='listname[$u]' />";
		for($l=1;$l <= $li;$l++) {

			echo "<li><label>List Item</label><input type='text' name='listitem[$u][$l]' /></li>";
		}
				echo "</ul>";
		}
		echo "<input type='submit' name='build' value='Build List' /></form>";
	}

This is the form code generated depending on the values intered into the orignial form.

<form method="post" action="message.php">
	<ul>
	<label>List Name</label><input type='text' name='listname[1]' />
	<li><label>List Item</label><input type='text' name='listitem[1][1]' /></li>
	<li><label>List Item</label><input type='text' name='listitem[1][2]' /></li>
	</ul>
	<ul>
	<label>List Name</label><input type='text' name='listname[2]' />
	<li><label>List Item</label><input type='text' name='listitem[2][1]' /></li>
	<li><label>List Item</label><input type='text' name='listitem[2][2]' /></li>
	</ul>
	<input type='submit' name='build' value='Build List' />
	</form>

And finally the desired output

List 1
<ul>
<li>1</li>
<li>2</li>
</ul>

List 2
<ul>
<li>3</li>
<li>4</li>
</ul>

Any feedback you have is welcome.

If you wanted the arrays to start at 0 then initialize $u and $l to 0 instead of 1, and use less than instead of less than or equals in the termination condition. Otherwise there’s nothing to comment on – you produced the same form as the example :slight_smile:

Thanks for your help