Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a \n value for a value like X

Tags:

php

I have a webpage for homework. Students fill in the boxes, then click send to send the form. My php thankyou file gets the answers and sends to my email. I download the emails and use Python to check each answer against the correct answer, line for line using readlines().

This morning I noticed, one student missed the first part, 1 to 5 'choose the right word from the table.' They should just enter a letter, A to E, in each textbox, G1 to G5. The email sent by php then contains 5 empty lines, \n, where the answers should be.

Because students often press enter in strange places, I run a Python script to get rid of empty lines in the emails after downloading.

So I would like to get php to put say X if a textbox is empty, before it sends the email.

The first part of the thankyou.php looks like this:

//should mail the contact form
<?php
$studentnr = $_POST['sn'];
$q1 = $_POST['G1'];
$q2 = $_POST['G2'];
$q3 = $_POST['G3'];
$q4 = $_POST['G4'];
$q5 = $_POST['G5'];

What I would like to do in php is something along the lines of:

for i in range(1, 6):  
    answer = $q + str(i)  
    if answer = '':  
        answer = 'X'

but that would be Python. What is the right way to do this in php?

I think this should be done after collecting all the $qs in the php script, but before making the body of the email:

$body = "

Studentnr = ".$studentnr."
".$q1."
".$q2."
".$q3."
".$q4."
".$q5."
";

Very grateful for any tips!

Edit: this is an actual thankyou.php with the loop to change '' for X, but all I get now in the email is: Studentnr = 1725010999, nothing else. How to tweak this? I just entered the student number and left all other boxes empty, so I was expecting a lot of Xs. I am not getting any errors in the error log of my php directory on the webpage host. Maybe a ; missing somewhere?

//should mail the contact form
<?php
$studentnr = $_POST['sn'];
$q1 = $_POST['G1'];
$q2 = $_POST['G2'];
$q3 = $_POST['G3'];
$q4 = $_POST['G4'];
$q5 = $_POST['G5'];
$q6 = $_POST['G6'];
$q7 = $_POST['G7'];
$q8 = $_POST['G8'];
$q9 = $_POST['G9'];
$q10 = $_POST['G10'];
$q11 = $_POST['G11'];
$q12 = $_POST['G12'];
$q13 = $_POST['G13'];
$q14 = $_POST['G14'];
$q15 = $_POST['G15'];
$q16 = $_POST['G16'];
$q17 = $_POST['G17'];
$q18 = $_POST['G18'];

for ($i=1; $i <= 18; $i++) {
    if (${"q$i"} == '') ${"q$i"} = 'X';        
}

$body = "

Studentnr = ".$studentnr."
".$q1."
".$q2."
".$q3."
".$q4."
".$q5."
".$q6."
".$q7."
".$q8."
".$q9."
".$q10."
".$q11."
".$q12."
".$q13."
".$q14."
".$q15."
".$q16."
".$q17."
".$q18."
";


$to1 = "[email protected]";

$subject = $studentnr . "sWeek1";
$headers = "From: [email protected]\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';

mail($to1, $subject, $body, $headers);

header("Location: email_success.php");

?>

Any more tips please?

Edit again: Normally, I get the answers OK. I'm just trying to correct for an empty answer. If a student does not fill out 1 box, and I remove empty lines, all the answers after the empty line are in the wrong order, so the score is very bad. I will go back to Q1 = ".$q1." Q2 = ".$q2." in the body part if I can't get his to work. At least I have a line!

Here is a paragraph of my html. If you see anything wrong, please let me know. I generate this from a text file with Python.

<p>
<b> World Recession </b> <br>
W: Now people are talking about <INPUT TYPE="Text" NAME="G1" size="15">, which started more than a year ago. Can you give us your personal understanding of the situation of the global economy? <br>
M: As you know, we are in a very special time. This is a very hard time for many countries&#39; economies, both <INPUT TYPE="Text" NAME="G2" size="24">. <br>
W: What challenges is our economy facing at the moment? <br>
M: We do face a lot of challenges because there is still much uncertainty about the world&#39;s economy. 
It&#39;s very important for us to strike a <INPUT TYPE="Text" NAME="G3" size="14"> between investment in <INPUT TYPE="Text" NAME="G4" size="14"> and household consumption. <br>
</p>

Thanks a lot!

like image 221
Pedroski Avatar asked Feb 28 '19 02:02

Pedroski


People also ask

How to replace N/a values in Excel?

How to Replace #N/A Values in Excel (With Examples) You can use the following basic syntax to replace #N/A values in Excel with either zeros or blanks: #replace #N/A with zero =IFERROR (FORMULA, "0") #replace #N/A with blank =IFERROR (FORMULA, "")

How do you change an x value to a z value?

Changing an x- value to a z- value is called standardizing. The so-called " z -formula" for standardizing an x- value to a z- value is: You take your x- value, subtract the mean , and then divide this difference by the standard deviation.

How to replace a value in a vector in R?

The replace () function in R syntax is very simple and easy to implement. It includes the vector, index vector, and the replacement values as well as shown below. This section will show how to replace a value in a vector. Execute the below code for the same.

How to replace N/a values from a VLOOKUP with zero or blank?

#replace #N/A with zero =IFERROR (FORMULA, "0") #replace #N/A with blank =IFERROR (FORMULA, "") The following example shows how to use this syntax in practice to replace #N/A values from a VLOOKUP with zero or blanks. And suppose we use the VLOOKUP () function to look up points based on team name:


2 Answers

You can do that like this:

   for($i=1; $i <= 5; $i++) {
        $answer= ${'q' . $i};
        if($answer=='') $answer='X';        
    }

Edit: If you just only need to update value, you can do that like this,

   for($i=1; $i <= 5; $i++) {
        if(${'q' . $i}=='') ${'q' . $i}='X';        
    }
like image 142
Whatatimetobealive Avatar answered Oct 11 '22 15:10

Whatatimetobealive


If you want to change any of the $q* variables to 'X' if they are empty, you can use PHPs variable variables to do that. For example:

$q1 = '';
$q2 = 'A';
$q3 = '';
$q4 = 'E';
$q5 = 'D';
for ($i=1; $i <= 5; $i++) {
    if (${"q$i"} == '') ${"q$i"} = 'X';        
}
echo "$q1\n";
echo "$q2\n";
echo "$q3\n";
echo "$q4\n";
echo "$q5\n";

Output:

X
A
X
E
D

Demo on 3v4l.org

like image 31
Nick Avatar answered Oct 11 '22 14:10

Nick