Although I know very little about computing, I have managed to make php send me a form, which is homework answers from students.
Every week, the number of answers is different, so I need to create different ‘thankyouWeekX.php’ files. I have automated this in Python. Below is part of the Python code:
filename = 'thankyou' + weeknr
print('How many boxes do we have? How many variables do we need?')
nrVariables = input()
# numVar = int(nrVariables)
PHP1 = ['\n','<?php\n', '$studentnr = $_POST[\'sn\'];\n']
for i in range(1, int(nrVariables) + 1):
variable = basicPHPVariable.replace('X', str(i))
PHP1.append(variable)
PHP1.append('\n')
PHP1.append('if($studentnr == \'\') {$studentnr = \'X\';}')
PHP1.append('\n')
PHP1.append('for ($i=1; $i <= ' + nrVariables + '; $i++) {\n')
PHP1.append(' if (${"q$i"} == \'\') ${"q$i"} = \'X\'; ')
PHP1.append('}')
PHP1string = '\n'.join(PHP1)
file = open(pathToPHP + filename, 'a')
file.write(PHP1string)
In the middle, the php checks for empty answer boxes and writes an X if there is no answer at all. For example, the students often forget to enter their student number.
In this line:
PHP1.append('for ($i=1; $i <= ’ + nrVariables + ‘; $i++) {\n’)
nrVariables corresponds to the number of boxes to fill in. nrVariables is a string in Python. I can’t add an integer to a string in the line above.
Question: Will python read nrVariables as an integer, like 29 this week, in the for loop? Should I somehow declare nrVariables as an integer in php?