0

I am pretty sure that you can put a while loop inside of a while loop, as i have done it before, but i seem to be running into a problem here. As this is pretty complex, ill post my code then explain it.

PHP:

//post vars
$port=$_POST['ports'];
$super=$_POST['super'];
$col=$_POST['columns'];
$row=$_POST['rows'];
//rest of vars
$half_col=$col/$super;
$halfer_col=$col/$super;
$i=1;
$count=1;
$and=1;
$one=1;
$sector=array();
$dat_array=array();

echo $port."<br>";

echo $super."<br>";

echo $col."<br>";

echo $row."<br><br><br><br>";
echo "<h1>here goes nothen</h1><br><br>";

while($count<$port){
    while($i<=$half_col){
        $dat_array[]=$halfer_col;
        $i+=$and;
        $halfer_col-=$and;
        echo "<br>halfer_col:".$halfer_col."<br>";
    }
print_r($dat_array);
$sector[]=implode(',',$dat_array);
$count+=$half_col;
$halfer_col+=$half_col;
}

echo "<br><br>ANNNNNNND...<br><br>";
print_r($sector);

Now, this is with user input port:24 super:2 col:12 row:2 result:

24
2
12
2

here goes nothen

halfer_col:5

halfer_col:4

halfer_col:3

halfer_col:2

halfer_col:1

halfer_col:0
Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Array ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) 

ANNNNNNND...

Array ( [0] => 6,5,4,3,2,1 [1] => 6,5,4,3,2,1 [2] => 6,5,4,3,2,1 [3] => 6,5,4,3,2,1 )

basically, it takes the user input, breaks it down and implodes it in the prober order based on a ",". however from what i can tell, it is not adding here: $halfer_col+=$half_col; and that is causing the data to not increment, any ideas anyone? Or even an explanation as to why that will not add and affect my internal while loop the way that it is set.

in the end the final array should look like so:

Array ( [0] => 6,5,4,3,2,1 [1] => 12,11,10,9,8,7 [2] => 18,17,16,15,14,13 [3] => 24,23,22,21,20,19 )
6
  • 1
    What output do you want? (there might be another way of doing it) Commented Jan 22, 2013 at 18:55
  • 1
    Maybe you meant to restart the inner loop with $i = 1 every time? As it is now, $i is only less than $half_col during the first time the outer loop runs. Commented Jan 22, 2013 at 18:56
  • @h2ooooooo i added what the final print array should look like Commented Jan 22, 2013 at 18:57
  • @MrLister i tried reseting $i and the end of the outer while loop but the only thing that changes was that it addied 1 extra array output to each array. if that makes sense Commented Jan 22, 2013 at 19:07
  • Then it's not clear what you want. After the first (and only) time the inner loop runs, it start adding to halfer_col, but halfer_col isn't used any more after that. Commented Jan 22, 2013 at 19:17

2 Answers 2

1

You could change your code to the following:

$port = (int)$_POST['ports'];
$super = (int)$_POST['super'];
$col = (int)$_POST['columns'];
$row = (int)$_POST['rows'];

$colHalf = $col / $super;

$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
    $tempArray = array();
    for ($j = 0; $j < $colHalf; $j++) {
        $tempArray[] = $i - $j;
    }
    $finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);

echo "<pre>" . print_r($finalArray, true) . "</pre>";

This will print what you want (port = 24, super = 2, col = 12, row = 2):

Array
(
    [0] => 6,5,4,3,2,1
    [1] => 12,11,10,9,8,7
    [2] => 18,17,16,15,14,13
    [3] => 24,23,22,21,20,19
)
Sign up to request clarification or add additional context in comments.

1 Comment

Worked great thanks! Were you by any chance able to tell what is wrong with mine?
0

You could try making the outer while a for loop.

for(; $count<$port; $count+=$half_col){
    while($i<=$half_col){
        $dat_array[]=$halfer_col;
        $i+=$and;
        $halfer_col-=$and;
        echo "<br>halfer_col:".$halfer_col."<br>";
    }
print_r($dat_array);
$sector[]=implode(',',$dat_array);
$halfer_col+=$half_col;
}

1 Comment

I pondered the idea of a for loop but i couldnt think of a way for it to work. i just tried it that way and the output was the same^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.