Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get three inputs from a submitted form:

1. The amount of sections
2. The amount of items in each sections (array)
3. All the items (array)

The form loop starts with looping over the sections:

PHP
for($r = 0; $r < $sectionamount; $r++) {


Then it have an inside for loop to loop over each item in the section:

PHP
for( $ret = 0; $ret < $hoevelbinee; $ret++ ) {
	 
   $diwordeid = $myform[$fiv];
	 
   $diou .= "die input is " . $diwordeid . "</br>";
   $fiv++;
 }


So the complete function would be:

PHP
$tr = 0;
$fiv = 0;

for($r = 0; $r < $hoevelsections; $r++) {
	
   $hoevelbinee = $myformsekis[$tr];
   $ret = 0;

   for( $ret = 0; $ret < $hoevelbinee; $ret++ ) {
	 
      $diwordeid = $myform[$fiv];
	 
      $diou .= "die input is " . $diwordeid . "</br>";
      $fiv++;
   }

   $tr++;

}

If I have two sections with each two items, it works perfectly e.g.

PHP
Array
(
    [0] => Entrance
    [1] => Door & Door frame
    [2] => Kombuis
    [3] => Door & Door frame
)

Array
(
    [0] => 2
    [1] => 2
)

Output:

die input is Entrance
die input is Door & Door frame
die input is Kombuis
die input is Door & Door frame


But anything more that two sections, the for loop would miss the last two or three inputs e.g.

PHP
Array
(
    [0] => Entrance
    [1] => Door & Door frame
    [2] => Kombuis
    [3] => vloer
    [4] => kaste
    [5] => Kamer
    [6] => gang
    [7] => bed
    [8] => mat
)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Output:

die input is Entrance
die input is Door & Door frame
die input is Kombuis
die input is vloer
die input is kaste
die input is Kamer
die input is gang


So why would the for loop not do every entry in the array?

What I have tried:

PHP
$tr = 0;
$fiv = 0;

for($r = 0; $r < $hoevelsections; $r++) {
	
   $hoevelbinee = $myformsekis[$tr];
   $ret = 0;

   for( $ret = 0; $ret < $hoevelbinee; $ret++ ) {
	 
      $diwordeid = $myform[$fiv];
	 
      $diou .= "die input is " . $diwordeid . "</br>";
      $fiv++;
   }

  $tr++;

}
Posted
Updated 6-Dec-19 4:48am
v2

The code you have shown is not exact, so I have had to make some guesses. This is the output I get:
$tr 0
die input is Entrance

$tr 1
die input is Door & Door frame
die input is Kombuis

$tr 2
die input is vloer
die input is kaste
die input is Kamer


Which is correct according to those array values.

[edit]
In reading this again I am wondering if you just overlooked the fact that your second array is wrong. The first array contains 8 items, but the entries in the second array add up to 6, so that is how many items it will print. Change your second array to:
PHP
Array
(
    [0] => 1
    [1] => 2
    [2] => 5
)

But I am at a loss to understand why you have made something simple so complicated.
[/edit]
 
Share this answer
 
v3
Comments
JimmiWillTakeThisUsername 6-Dec-19 14:25pm    
Thanks Richard MacCutchan!

There is a method to this madness and without over-complicating it even more, I will just say this is from a form with unknown amount of inputs.

The array should be correct. I am not creating them by hand - they are created with inputs from a form. I've counted the array and you might be on the right track. The numbers are incorrect - which means that I might get the wrong inputs on the second array. I'm investigating that at the moment. But the array does not go up to 6, it goes up to 11.

Array(    
[0] => 1   -> this is actually two as we start counting at 0 (2)      
[1] => 2   -> this is actually three as we start counting at 0 (3)    
[2] => 5   -> this is actually six as we start counting at 0 (6)                
-> 2+3+6 = 11 )


The desired output should be:
die input is Entrance
die input is Door & Door frame
die input is Kombuis
die input is vloer
die input is kaste
die input is Kamer
die input is gang
die input is bed
die input is mat

In a bit more depth -
There are sections($hoevelsections):
Entrance
Kombuis
Kamer

Under each section there are the amount of items($myformsekis):
[0] =>
0(Entrance)->0(Entrance)
1(Entrance)->1(Door & Door Frame)

[1] =>
0(Kombuis)->0(Kombuis)
1(Kombuis)->1(vloer)
2(Kombuis)->2(kaste)

[2] =>
0(Kamer)->0(Kamer)
1(Kamer->1(gang)
2(Kamer)->2(bed)
3(Kamer->3(mat)

And the last array($myform) just have all the inputs
Richard MacCutchan 7-Dec-19 4:06am    
[0] => 1 -> this is actually two as we start counting at 0 (2)
No, you start at zero and continue only while the loop count is less than 1, so you display 1 item. Similarly the other values cause the display of 2 and 5 items respectively, not 3 and 6.
The result I gave you above is based on your code, not something I invented.
JimmiWillTakeThisUsername 7-Dec-19 4:18am    
Thanks! What you said made me released my second array numbers are screwed-up. So I'm going back and starting over from there.

Appreciate the time you took to look over my code.
JimmiWillTakeThisUsername 7-Dec-19 4:31am    
DUDE!!!! It works perfectly now!
Thank you so much!!!
Richard MacCutchan 7-Dec-19 4:32am    
You are welcome.
Try using the foreach loop:
PHP
foreach(element in arr)
echo element


You should check the php manual on this as i am not currently working with PHP and i am not sure the syntax is correct
 
Share this answer
 
v2
Comments
JimmiWillTakeThisUsername 6-Dec-19 14:30pm    
Thanks alex for your input. Not sure the Foreach function would create the desired results

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900