Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop validation problem 3

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
ZA
Hi have a problem in that I want to validate an array which is consecutive numbers from 1 to 18. Say, for example I have ten numbers 1 2 3 4 6 7 8 9 11 12 posted from my form, I need to loop through the result 18 times and if a number is missing(false), which is 4 in this example, it will print out the number and a variable which I will assign (in this example I used "data added".
the results I get are as follows:

1 1: valid
2 2: valid
3 3: valid
4 4: valid
6 6: Data Added
7 7: valid
8 8: valid
9 9: valid
11 11: Data Added
12 12: valid

as you can see it missed out 5 and 10 and recognised 6 as false and 11 as false eventhough they are in my results
Code:
foreach($_POST[quest_index] as $checker){
	if($validator == $checker){
		print "$validator $checker: valid<br>";
		$validator++;
	}else{
		$validator++;
		print "$validator $checker: Data Added<br>";
		$validator++;
	}
	
}

Reality is built on a foundation of dreams.
 
you're incrementing the counter before printing!!!

Code:
foreach($_POST[quest_index] as $checker){
    if($validator == $checker){
        print "$validator $checker: valid<br>";
        $validator++;
    }else{
        // $validator++;
        print "$validator $checker: Data Added<br>";
        $validator++;
    }
    
}

 
yip I know but then if I dont do that I get:
1 1: valid
2 2: valid
3 3: valid
4 4: valid


and thats it. That's where it stops.

Reality is built on a foundation of dreams.
 
the reason it prints 6 as data added is because you're incrementing the counter before printing. Which is the answer to the question you asked.

As for why it stops at 4, I'm stumped!

Sorry i couldn't be any more help!
 

this is the code i've used!

Code:
<?php
$array['1']=1;
$array['2']=2;
$array['3']=3;
$array['4']=4;
$array['5']=6;
$array['6']=7;
$array['7']=8;

$validator=1;

foreach($array as $checker){
    if($validator == $checker){
        print "$validator $checker: valid<br>";
        $validator++;
    }else{
        //$validator++;
        print "$validator $checker: Data Added<br>";
        $validator++;
    }
}
    
?>
 
can i have the variable inputted to the function?

i am confused...

Known is handfull, Unknown is worldfull
 
You see, there's a problem there cause your result is:

1 1: valid
2 2: valid
3 3: valid
4 4: valid
5 6: Data Added
6 7: Data Added
7 8: Data Added
8 9: Data Added
9 11: Data Added
10 12: Data Added

Which means array 5 to 10 returned as false. Ineed something like:
1 1: valid
2 2: valid
3 3: valid
4 4: valid
5 5: DATA ADDED
6 6: valid
6 7: valid
8 8: valid
9 9: valid
10 10: DATA ADDED
11 11: valid
12 12: valid

so line 5 and line 10 were written.
Any ideas???

Reality is built on a foundation of dreams.
 
$validator = 1;
$_POST[quest_index] is a bit difficult to explain. in this situation it is equivalent to 1,2,3,4,6,7,8,9,11,12. but this changes as different selections have a different string of numbers.

What I primarily need to do is create a comma seperated value with the different answers from the questions in place and MUST be 18 values long. In this circumstance:

"answer1","answer2","answer3","answer4","","answer6","answer7","answer8","answer9","","answer11","answer12","","","","","",""

so i need this to loop through and put the answers in the right place of the string by starting at 1. ie

if($index_question == $index_answer){
$answer = "$answer,"$posted_answer",";
}else{
$answer = "$answer","",;
}
I know formatting not right, just to give an example.

Reality is built on a foundation of dreams.
 
does this work???
<?
$str=explode(",","1,2,3,4,6,7,8,9,11,12");

$TheArr[19];
for($i=0;$i<19;$i++)
{
$TheArr[$i]="";
}

for($i=0;$i<count($str);$i++)
{
$TheArr[$str[$i]]="Answer".$str[$i];
}

for($i=1;$i<count($TheArr);$i++)
{
if($TheArr[$i]!="")
echo $i."-".$TheArr[$i]."<br>";
else
echo $i."-Data Inserted<br>";
}
?>


Known is handfull, Unknown is worldfull
 
You are right on the money buddy. You saved me yet another sleepless night!
Thanks.

Reality is built on a foundation of dreams.
 
Given your sample input the following works fine:
Code:
<?
$ans = array();
$quest = array(1,2,3,4,6,7,8,9,11,12);
for ($i=1;$i<19;$i++) {
    if (in_array($i,$quest)) $ans[$i] = '"answer'.$i.'"';
    else $ans[$i] = '""'; }
print_r($ans);
echo implode(',',$ans);
?>
Which outputs:
Code:
$ php -q chk.php
Array
(
    [1] => "answer1"
    [2] => "answer2"
    [3] => "answer3"
    [4] => "answer4"
    [5] => ""
    [6] => "answer6"
    [7] => "answer7"
    [8] => "answer8"
    [9] => "answer9"
    [10] => ""
    [11] => "answer11"
    [12] => "answer12"
    [13] => ""
    [14] => ""
    [15] => ""
    [16] => ""
    [17] => ""
    [18] => ""
)
"answer1","answer2","answer3","answer4","","answer6","answer7","answer8","answer9","","answer11","answer12","","
","","","",""

Ken
 
You see, there's a problem there cause your result is:

1 1: valid
2 2: valid
3 3: valid
4 4: valid
5 6: Data Added
6 7: Data Added
7 8: Data Added
8 9: Data Added
9 11: Data Added
10 12: Data Added

Which means array 5 to 10 returned as false. Ineed something like:

That's because they ARE false!

5!=6: Data Added
6!=7: Data Added
7!=8: Data Added
8!=9: Data Added
9!=11: Data Added
10!=12: Data Added
 
Ken,
thanks that helps as well!
Cheers guys thanks for all your help.

Reality is built on a foundation of dreams.
 
Ken, your method would be the best way, however, how do I get my POST_[answer] to display instead of answer1, answer2, answer3 etc etc etc?

Code:
<?
$ans = array();
$quest = array(1,2,3,4,6,7,8,9,11,12);
for ($i=1;$i<19;$i++) {
    if (in_array($i,$quest)) $ans[$i] = '"answer'.$i.'"';
    else $ans[$i] = '""'; }
print_r($ans);
echo implode(',',$ans);
?>

Reality is built on a foundation of dreams.
 
sorry type error, $_POST[answer] for above thread!

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top