[SOLVED]PHP loop is skippinging numbers

Anyone ever had this problem: Here is the start of a loop to check student answers against the correct answers.

At home, it works perfectly, I put in all the correct answers and get a perfect score. ($length in this case is 61)

$score = 0;
for($i=0; $i < $length; $i++) {

On my webpage I couldn’t understand that my score was not 100%.

Using echo $i, I discovered, on the webserver, $i skips over 1, 2 goes straight to 3, then later skips more numbers: 41, 43, 44, 47, 48, 50, 52, 54, 57, 59

I blame the webhost. They had an outage last Saturday, I think that must have done some damage.

The students’ answers are still being saved correctly in MySQL, but the marking is not right.

Ever had this particular problem?

You did not show all your code. If you create a new page with this code, does it show them all?

<?PHP
for($i=0; $i<=100; $i++) { echo $i; }
?>

Do you see 100 numbers listed? If yes, then, something in your loop is causing the error.
If no, then your server is messed up. Easy to test… Let us know…

Thanks for your reply.

Here’s the bit to echo the values to the screen, which is how I found the problem.

I never had this problem before. All last term it worked perfectly. All the previous years it worked perfectly. And it works perfectly on my laptop.

I’m pretty sure the server has a problem! They probably need to reinstall PHP.

Sometimes, there is more than 1 correct answer, that’s what the explode() is for.

$length = count($correctAnswers);
echo ‘$length = ’ . $length . ‘

’;
//exit();
$score = 0;
for($i=0; $i < $length; $i++) {
$answers = explode(’|’, $correctAnswers[$i]);
if(in_array($studentAnswers[$i], $answers)) {
echo '$i = ’ . $i . ‘
’;
echo 'student answer is: ’ . $studentAnswers[$i] . ‘
’;
echo 'this $correctAnswers[0] is: ’ . $answers[0] . ‘
’;
$score++;
echo 'score is: ’ . $score . ‘
’;
}
}
$score = $score * $points;

Like I said $i starts at 0, then jumps to 3 and misses other numbers too! Weird and annoying.

This week at least, I will have to download each student’s answers and mark them with Python!
Thank God for Python!

I am a little confused on your comments and code. First, did you test my routine on your live server?
Did it pass or did it fail? That is step number one. If it fails, contact your server IT people right away!
If it passes, then you code is at fault or the data on the server is not correct.

As the code goes, why do you keep exploding answers? As soon as the test questions are created, and the answered marked, you can index them into the $answers array using the index where needed.
Then, that array of correct answers would be stored and just retrieved when needed to be checked.
( Of course, normally, this would all be done with one simple query and no calculations would be needed!

PS: Python is a command line programing language and not really a website database system. But, it does come in handy at times.

I tested

<?php
for ($i=1; $i <= 41; $i++) {
			echo '$i' . $i . '<br>';
		}
?>

At home and on my webpage, at the top of a file which just has links to other files.

NO trouble.

So you are probably right, something in my code is making $i jump, but what?

Well, I don’t know how you would do this.

I get a load of answers from a student as $_POST[‘G1’], $_POST[‘G2’] etc. I assign them to PHP variables $q1, $q2 etc.

I check them for empty answers and whitespace.

for ($i=1; $i <= 41; $i++) {
		    if (${"q$i"} == '') ${"q$i"} = 'X';
		    // get rid of possible white space with trim
		    $studentAnswers[] = trim(${"q$i"});
		    //echo ${"q$i"} . '<br>';

The correct answers are in a table called markingAnswers20BECW.

I read the correct answers into an array, then check each answer from the student against the correct answers or answer.

Then I save the student’s answers and score to a table called allstudentsAnswers20BE.

This all worked fine up to now!

EDIT
Strange tale to tell:

Tomorrow I have the 20EAP class again, I have them 2 times a week. We must have online class until October, because of the virus.

I uploaded the webpage for tomorrow.

I generate the php script to deal with the answers using Python. I just need to tell Python which class, how many variables, which week and how many points per answer.

1 second and the php is ready. I tried it at home, no problem.

So I uploaded tomorrow’s webpage and the php script, filled in the answers, 41 of them, and clicked “Send answers”, fully expecting trouble!

Nothing, everything OK, 100% correct, 100% score!

So I have no explanation for the problem above. But it seems to have gone away.

I will generate the problem script again and try, see if I get the same problem!!

Interesting! At least we know it is your code not the server now. That is step one in debugging this problem.
I would like to mention my viewpoints on your processes. ( You may not like or agree with them, but… )

Normally, in most webpages, this would be done with PHP and MySQL only. Using an offline editor is not as secure and can cause hidden problems. Normally, in my humble opinion, you would have an ADMIN page on your website and you would create the weekly quiz using that page. And, in the database, it would be one table using fields for Week#, Class Name, Question#, question text, correct answer. These would be filled in from the ADMIN page which would be secured and password protected. Then, there would be a student table keeping track of their answers. The student answer table would be similar to the question table. With this type of standard setup, it is so simple to get the data out. You just run simple queries joining the two tables together to get whatever you need for displays.

Okay, with that said, Python code that creates PHP code which is moved to the server could be creating the problem. Since it is your Python code that creates the data to be displayed, there might be an issue with the results from it. You would need to create the form again that your Python code created from the week that failed and then analyze it to see if it is 100% correct!

As I said, this is just my opinions, but, thought I would throw it all out for you to think about.
Good luck…

One further comment… The input forms can use indexing. You most likely should just use the question numbers instead of complicated Q1, Q2, etc. In this way, you can get the answers in one array without needing converting at all. Are these multiple answer tests? I mean, is there 1 to 5 for answers or can the students enter text? If just multiple answer tests, then it is super easier to use indexing. Either way, if you do not use Q1, Q2, etc, you can just use the question or answer number as an index and it is easy to just use the array instead of converting to a second array. ( Faster too! )

@ErnieAlex

I wouldn’t know how to do what I do just on the web, using only PHP and MySQL.

First, I have to make the webpages. That involves scanning parts of the textbook, ocr, cutting up audio files. Then I have Python tkinter window with buttons for all kinds of html, for making checkboxes, radio buttons, gapped text, etc. My homemade Python module for this is thousands of lines long!

Also a button for making the php file that gets the answers, marks them and saves them in MySQL

When everything is ready, I try it out at home. If all good, I upload and try again.

I still think the webhost had a problem in their PHP!

Today, I just finished making the webpage for next week, week 2. Tried it out at home, no problem. This week has 66 answers, some text, some multiple choice.

Uploaded, tried it, expecting trouble: BUT I got a perfect score, first time. NO problems.

I still don’t know what went wrong in Week 1. I regenerated the php file, tried it, but the problem remains.

Chalk it down to experience!!

Below is the $i loop which works perfectly for Week 2 and many, many other weeks, but skips in Week 1.
Can you see a problem?

$length = count($correctAnswers);
//exit();
$score = 0;
for($i=0; $i < $length; $i++ ) {
	$answers = explode('|', $correctAnswers[$i]);
	if(in_array($studentAnswers[$i], $answers)) {
			//echo 'student answer is: ' . $studentAnswers[$i] . '<br>';
			//$thisAnswer = explode('|', $correctAnswers[$i]);
			//echo 'this $correctAnswers[0] is: ' . $thisAnswer[0] . '<br>';
			$score++;
			//echo 'score is: ' . $score;			
			}
		}
		$score = $score * $points;

Well, again, you are using a Python script to create a real script and webpage. Then, you do not validate it’s output. You upload it to a server ( local or live ) and test it. You have no idea if the output of your Python system is valid or correct. Not to mention secure. Probably is not secure at all.

If you want to validate your HTML output from the Python code, you can post it here and see if there are errors. HTML Validator

This does not check PHP code. You have to use a different online validator for that.

In my humble opinion, you are using offline tools to “build” an online system and it is not really designed for your type of application. If you want to make it into a real online system, we can help you. It would require you learning several different parts such as CSS, HTML, MySQL, etc…

Thanks for your reply and your offer of help.

I have acquired enough html, css, php and MySQL to make what I want to do work, just about.

I understand you think the html and php scripts I create are faulty.

All I can say is, they worked in the past, they work now:

the webpage displays the way I want, the php, up to this term Week 1, always works.

Like I said, I test it at home, iron out any problems, then upload and test again.

In Week 1 the classwork PHP had some kind of problem, I don’t understand, possibly the webhost.

But the homework PHP for Week 1 worked perfectly. I know because several students got 100%. That would not be possible if the PHP dropped answers.

They are both generated using the same Python.

Week 2 is coming up, and the html and php have been tested and are working!

html and php are just text files. Python works well with text files.

I don’t think many people write complicated php scripts by hand. Just add the variables to existing templates.

I certainly would not want to sit down and write everything from scratch! Why do we have computers?

Security is not really an issue for me, but I try to follow advice on making PHP secure as best I can, especially when it interacts with my database.

Well, if it works great! I would never want to “fix” something that works. But why are you here if it “works”?
It does not, right?

So, first, most of the professionals here on this site who help upcoming programmers, well, they ALL write PHP code daily from scratch. Why not? I have some extremely long complicated PHP scripts that do extremely complicated linear regression code on website scraped data that run daily! So, I do not understand your comments on that. Python is a great tool for local data processing. I use it on my super computer to run huge artificial intelligence neural network models on several world processes. They are both great products for their own uses.

Now, with my rant out there… You run a program we have not seen that create data that in itself is a program. And, without seeing either the first level program or the output of it, you want us to help you…

If you are having a Python to HTML problem, show us the code that is the output for the part that is failing and we can point out what is wrong with it. Normally, a professional programmer would create this solution in PHP using a MySQL database to house the data and student answers. It would be super easy to just do simple queries to get totals, reports, etc…

So, to solve this problem, show the data for the bad week and the PHP code that is failing and we can probably tell you where the error is. Then, you can alter the Python code to fix the output of the PHP code to fix it…

What I mean to say is, generally, my php works first time, no problem.

I use Python to make the PHP. I have been doing that for at least 3 years now.

I never had the problem of $i skipping numbers before.

The problem arose on the webhost, just after they went down for most of Saturday a week ago.

Previously, I only didn’t get a perfect score if there was a problem in the answer key, or my answer.

That indicates to me that something else is wrong.

So I thought, ask if anyone else has ever encountered this problem. Seems not.

Also, the problem seems to have gone away.

SOLVED: I still don’t know WHY but I found the problem.

In my template, I have this to collect the student’s answers in an array:

for ($i=1; $i <= NUMVARS; $i++) {
if (${“q$i”} == ‘’) ${“q$i”} = ‘X’;
// get rid of possible white space with trim
$studentAnswers[] = trim(${“q$i”});
//echo ${“q$i”} . ‘
’;
}

Python replaces NUMVARS with an integer, the number of variables. This loop always works perfectly.

Later on I have this for checking the answers:

$length = count($correctAnswers);
// length is correct, this time 57
echo ‘$length is: ’ . $length . ‘
’;
//exit();
$score = 0;
for($i=0; $i < $length; $i++ ) {
for($i=0; $i < $length; $i++ ) {
$answers = explode(’|’, $correctAnswers[$i]);
if(in_array($studentAnswers[$i], $answers)) {
echo '$i is: ’ . $i . ‘
’;
echo 'student answer is: ’ . $studentAnswers[$i] . ‘
’;
//echo 'correct answer(s) are: ';
//print_r($answers);
echo ‘
’;
$score++;
echo 'score is: ’ . $score . ‘
’;
}
}

For reasons I do not understand, $length causes the loop to miss numbers, although it has the correct value, in this case 57.

Replace $length with the number, like 57, and the problem is gone!!

Moral of the story: don’t use a variable in the loop, use an integer!

Well, you have two for($i) 's in your list. You can not do that. You would need to use $i and $j or something else. The way that PHP works is that any one variable is incremented when inside a for. Then it hits the next for and uses the old value. That code is a bit mixed up. Also, you don’t use indexes which would remove the need for all that string processes with explodes. But, not a lot of steps in total, so does not really matter… Glad you got it working.

Actually, there is only 1 $i loop, don’t know how the other one sneaked into this post!

Sorry about that!

Sponsor our Newsletter | Privacy Policy | Terms of Service