UseBB Community

The official board for UseBB help and discussion

UseBB Community » 1.0 Discussion » need help with looping

need help with looping

Page: 1

Author Post
Moderator
Registered: Oct 2005
Posts: 425
Location: canada
Me again with code mess-up's :p.

I have the following loop/code:


$content = '';

$c = 0;
$s = 0;

$cat_count = $_POST['cats'];
$cat_count = $cat_count-1;

while ( $cat_count >= $c ) {

$settings_count = $_POST['count-'.$c.''];

$content .= '<pre>Category: '.$settings_count.'</pre>'; //just debugging

for ( $i = 0; $i <= $c; $i++ ) {

$content .= '<pre>Setting: '.$_POST['setting-'.$s.'-'.$i.''].'</pre>'; //just debugging

}

$c = $c+1;
$s = $s+1;

}


I'll try and explain it best I can...


$cat_count = $_POST['cats']; // $_POST['cats'] == 3
$cat_count = $cat_count-1; // $cat_count == 2 ( 0,1,2 ). Therefore, 3 categories.


Categories are set up in numeric form. So, 0 is the first category, 1 is second, 2 is third, etc.


$settings_count = $_POST['count-'.$c.'']; //first loop through: $_POST['count-0'] == 2


That gets the number of settings per category. So, there is 2 settings in the first category - 0.


for ( $i = 0; $i <= $c; $i++ ) {

$content .= '<pre>Setting: '.$_POST['setting-'.$s.'-'.$i.''].'</pre>'; //just debugging

}


Now, that will output the settings in the one category. So, it will loop 2 times, with the same category 0.

$s == 0 first run through - category 0 ( first category )
$i == 0 first run through - setting 0 ( first setting )

( $_POST['setting-0-0']; )

$i has to loop again, because there is 2 settings in the category 0.

$s == 0 second run through -category 0
$i == 1 second run through - setting 1 ( second setting ).

( $_POST['setting-0-1'] )

Now it should pop out of the FOR LOOP and go back to the while loop for the second category.

But, when it goes around the second time, i get:

$_POST['setting-1-1'], which gives me an error, because there's only one setting in the second category, so it should be $_POST['setting-1-0'].

So, i'm not sure why $i isn't being reset back to 0 once finished the first category run.

Sorry if it makes no sense, i figured i'd try and explain it for some help :oops: I'm no good with loops.
Moderator
Registered: Oct 2005
Posts: 425
Location: canada
Well i changed it abit and it seems to be fixing the $_POST['setting-0-x'] number. But now, it's not doing the $_POST['setting-x-0'] correctly.

I'm getting ouputs of:

Quote
Category: 1

Setting: 2

Setting: 3

Category: 0

Setting: 4

Category: 0

Setting: 5


Which is backwards. the first category should be 0, and the next 1. But it goes from 1 to 0 to 0 again.

I'll keeping tinkering with it. My loop must be missing something.


$content = '';

$c = 0;
$s = 0;

$cat_count = $_POST['cats'];
$cat_count = $cat_count-1;

while ( $cat_count >= $c ) {

$settings_count = $_POST['count-'.$c.''];
$settings_count = $settings_count-1;

$content .= '<pre>Category: '.$settings_count.'</pre>';

for ( $i = 0; $i <= $settings_count; $i++ ) {

$content .= '<pre>Setting: '.$_POST['setting-'.$s.'-'.$i.''].'</pre>';

}

$c = $c+1;
$s = $s+1;
Moderator
Registered: Oct 2005
Posts: 425
Location: canada
Ok, got it :p. Guessed it helped by typing it all out :p.

Quote
Category: 0

Setting: 2

Setting: 3

Category: 1

Setting: 4

Category: 2

Setting: 5


:D.

Page: 1

UseBB Community » 1.0 Discussion » need help with looping

UseBB Community is powered by UseBB 1 Forum Software