I'm a newbie in PHP; please guide me slowly and carefully.
I have a login page (login.php) and a user page (useracc-test.php). The user page (useracc-test.php) displays all personal data after a user logs in using his own username and password. I can display other data such as (name), (username) and (telno) in php script when I echo it.
But the problem is, when I tried to echo the data in html, just below the php script, only (username) shows on the screen. The other two (name) and (telno) show nothing. No errors displayed on browser also, and also no syntax error. Below is my user page (useracc-test.php).
<?php
//useracc-test.php
/**
* Start the session.
*/
session_start();
/**
* Include our MySQL connection.
*/
// require 'lib/password.php';
require 'connect-test.php';
$userName=$_POST['username'];
//$sql = "SELECT name, username FROM users WHERE username = '" . $_POST['username'] . "'";
//$result = $conn->query($sql);
$query = sprintf("select name, username, telno FROM users WHERE username='%s'", mysql_real_escape_string($userName));
$result = $conn->query($query); ?>
<?php while($row=$result->fetch_assoc()): ?>
<div id="satu"><?= $row['name'] ?></div>
<div id="dua"><?= $row['username'] ?></div>
<div id="tiga"><?= $row['telno'] ?></div>
<?php endwhile; ?>
<html>
<head>
<style type="text/css">
#apDiv2 {
position: absolute;
left: 51px;
top: 238px;
width: 237px;
height: 93px;
z-index: 1;
}
#apDiv1 {
position: absolute;
left: 50px;
top: 344px;
width: 234px;
height: 104px;
z-index: 2;
}
</style>
</head>
<body>
<div id="apDiv2"><span class="error"><?php echo $userName; ?></span></div>
<div id="apDiv1"><span class="error"><?php echo $row['telno'] ?></span></div>
</body>
</html>
Last html code should be
<html>
<head>
.....
</head>
<body>
...
</body>
</html>
You have put </head> in <body> section.
If the database connection is OK, also try changing
while($row=$result->fetch_assoc())
To
while($row=mysql_fetch_assoc($result))
And add
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
at above in php file.
However, this doesn't make PHP show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
And check for errors.
Problem solved by member of Stack Overflow just now. Regarding div tags which I posted previously, and the problem was a 2 in 1 coincidently. The person managed to solve everything, the person's name was Svdb.
Below is the code solved by him:
<body>
<div id="apDiv2"><span class="error"><?php echo $userName; ?></span></div>
<div id="apDiv1"><span class="error"><?php echo $row['telno'] ?></span></div>
<?php while($row=$result->fetch_assoc()): ?>
<div id="satu"><?= $row['name'] ?></div>
<div id="dua"><?= $row['username'] ?></div>
<div id="tiga"><?= $row['telno'] ?></div>
<?php endwhile; ?>
</body>
</html>
Related
I'm currently learning php so I'm a beginner I have learned the basics and some advanced stuff but now I'm trying to make a project to help me learn faster which will be basically a Math test in times table in which a user will enter the site, and then the user will enter his name and click 'Begin test' to enter the test which is 10 questions. the user need to answer the question first by clicking on a button and then click on 'Next' to go to next question and after finishing 10 questions the result will be shown to him something like "You have answered CorrectAnswersNumber from total of 10 questions!".
I have made something like this when I was learning ASP.Net MVC but in php it is a bit complicated. So my question is should I need to create 10 php pages that contains code to generate random numbers for the questions? if so how can I pass whether the user has answered the question right or wrong?
What I have did so far is the page in the first which contain the username and save it by using a session here is my code for the index.php page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD">
<h1 style="font-size: 75px;" align="center">Math Test</h1>
<form method="POST" action="Math_Test.php">
<div style="text-align: center;">
<input type="text" name="Name" style="width: 500px; height: 100px; font-
size: 75px; color: blue;">
<br /><br />
<input type="submit" value="Begin Test" style="width: 250px auto;
height: 100px auto; font-size: 75px;">
</div>
</form>
</body>
</html>
and the code for the secondpage:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$Name = $_POST['Name'];
} else {
echo "<h1 align='center' style='margin-top: 250px;'>Sorry, You can't
access this page directly.<br /> Please go back ant try again or simply
click here!</h1>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD;">
<?php echo "<h1 align='center' style='font-size: 100px;'>Hi " . $Name .
"</h1>"; ?>
</body>
</html>
You can do something like this on your index page
<?php
// starts session
session_start();
// check if submit button was clicked
if ( isset($_POST['submit']) ) {
$name = htmlspecialchars(trim($_POST['name']));
// check if name was provided
if ( $name !== '' ) {
// store name in session
$_SESSION = array(
'answered_questions' => 0,
'correct_answers' => 0,
'wrong_answers' => 0,
'name' => $name
);
// redirect to second page
header('Location: second_page.php');
exit();
}
// set error if name was not provided
$error = 'Please provide your name';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD">
<h1 style="font-size: 75px;" align="center">Math Test</h1>
<?php
// no name was provided so display the error message
if ( isset($error) ) {
echo '<p style="color: red">'. $error . '</p>'
}
?>
<form method="POST" action="">
<div style="text-align: center;">
<input type="text" name="name" style="width: 500px; height: 100px; font-
size: 75px; color: blue;">
<br /><br />
<input type="submit" name="submit" value="Begin Test" style="width: 250px auto;
height: 100px auto; font-size: 75px;">
</div>
</form>
</body>
</html>
And then on your second page you do something like so
<?php
// starts session
session_start();
// check if name key and value is not in the session
// if not there, then redirect back to the index.php page
if ( !isset($_SESSION['name']) ) {
header('Location: index.php');
exit();
}
if ( isset($_POST['submit']) ) {
$answer = (int)$_POST['answer'];
$expected_answer = $_SESSION['first'] * $_SESSION['second'];
$_SESSION['answered_questions'] += 1;
if ( $expected_answer == $answer ) {
$_SESSION['correct_answers'] += 1;
} else {
$_SESSION['wrong_answers'] += 1;
}
if ( $_SESSION['answered_questions'] == 10 ) {
// reset the session values from that page except name of course
header('Location: show_results.php');
exit();
}
$_SESSION['first'] = rand(1, 10); // random number between 1 and 20
$_SESSION['second'] = rand(1, 10);
} else {
$_SESSION['first'] = rand(1, 10); // random number between 1 and 20
$_SESSION['second'] = rand(1, 10);
}
// set the variable name to value in the session
$name = $_SESSION['name'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body>
<?php var_dump($_SESSION); ?>
<h1>Hello, <?php echo $name; ?></h1>
<form method="POST" action="">
<p><?php
echo $_SESSION['first'] . ' * ' . $_SESSION['second'] . ' = ';
?><input type="text" name="answer" id="answer" required></p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
Then after if you want you can handle all the logic on the second page if you want as per #SamiKuhmonen advice
You don't need to do 10 pages for PHP to handle the math test that you are trying to do, instead, there is a technique called AJAX, which you can read about it from this URL:
https://www.tutorialspoint.com/php/php_and_ajax.htm
they have a very nice tutorial that deals with databases as well, which will help you in your case.
Give it a look and try to implement it, it will help you learn much faster.
Just for the record, PHP does have MVC frameworks, two are popular for developers,
Laravel: https://laravel.com/
Codeigniter: https://codeigniter.com/
Both supports MVC, I'll recommend you to go first with Codeigniter, once you feel that you are comfortable in PHP, switch to Laravel.
All the best, happy coding!
How to order divs side by side and centered when get datas from database in php and css ?
php and html codes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="css/normalize.css">
</head>
<body>
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="cards">
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
</div>
<?php }?>
</body>
</html>
css style codes
body {
background-color:#282828;
}
a{
text-decoration: none;
}
.cards{
text-align: center;
font-size: 0;
}
.card{
margin: 0 10px;
width: 250px;
text-align: left;
display: inline-block;
font-size: medium;
}
.card-img{
width: 250px;
margin-bottom: -5px;
}
.card-title{
background-color: #FFFCF5;
width: 240px;
padding:5px;
margin-bottom: 15px;
color: #282828;
}
.card-title>p>a{
color: #00BFA5;
}
my page looks like this:
but I want to those pictures order side by side
I couldn't that. How could I do?
Thank you
You got .cards inside you while loop, take it out:
<body>
<div class="cards">
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
<?php }?>
</div>
</body>
The reason your code doesnt work, is because .card is inline-block which will place multiple card nexto eachother, but because you look the .cards (note the S) as well, you wrap each item in a div. A div is a block element, which means it'll take up one whole row, the next element will fall below it.
For each row from database you parse, you will have this following output:
`
<div class="cards">
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
</div>
`
So, all you have to do is to have the cotainer outside the while loop, like this:
`
<div class="cards">
<?php include ("connect.php");
$data = $db->query("select * from videos order by id desc");
$data ->execute();
while ($row=$data->fetch(PDO::FETCH_ASSOC)) {?>
<div class="card">
<img src="<?php echo $row['thumbnail'];?>" class="card-img" />
<div class="card-title">
<h3><?php echo $row['v_name'];?></h3>
<p><?php echo $row['v_models'];?></p>
</div>
</div>
<?php }?>
</div>
`
I know it's not exactly what you want, but try :
cards { display : flex ; flex-direction : row ; justify-content : space-between }
/ ! \ Doesn't work on IE8 and previous
Why I cant display the data I echo from the database in a PDF file ?
I used DOMPDF to convert my html. Anyone please help.
Here is the PDF result
PDF result image
Here is connection code
<?php
$id = $_GET['id'];
$conn = pg_connect("host=localhost dbname=mydatabase user=postgres password=admin");
if (!$conn) {
echo "An error occurred. cannot connectect to localhost.\n";
exit;
}
$sql = pg_query($conn, "SELECT * FROM mytable WHERE reference_no = $id ");
if (!$sql) {
echo "Error Ariel 7:50 !!!.\n";
}
while ($row = pg_fetch_assoc($sql)) {
$reference_no = $row['reference_no'];
$wo_name = $row['wo_name'];
}
Here is my convertion code:
error_reporting(0);
require_once ('xdompdf/dompdf_config.inc.php');
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno"><?php echo $reference_no;?></div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname"><?php echo $wo_name;?></div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>'
;
$name = date("Ymd").rand().'.pdf';
$reportPDF=createPDF(12, $pdf_content, 'activity_Report', $name );
function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
$path='xdompdf/';
/*$rndNumber=rand();
$filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path.$filename, $output);
return $filename;
}
echo '<a href="xdompdf/'.$name.'" > Download </a>';
?>
Please Help me how to display the data.
Change $pdf_content to
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno">' . $reference_no . '</div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname">' . $wo_name . '</div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>';
I think you just need to concat your data in $pdf_content :
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno">'.$reference_no.'</div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname">'.$wo_name.'</div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>';
basically I have a simple user page which I access after logging in via a login page. In the user page, there is my personal data displayed from my previous registration. And there is also a form where I can enter new information such as my favourite color and hobby (which I can insert multiple times). All the records goes into the DB succesfully. The problem is, everytime I submit a new data from the form in the user page, my personal data that are displayed in the same page dissapears. When I check the DB, nothing is lost, everything remains there. The only problem is, everytime I submit a data, my personal data which is displayed on the same page dissapears. BUt when I log in, it is there again. I suspect it might be due to non-infinite while looping everytime I press the submit button to insert new information.
How do I create an infinite while loop if this is the problem.??please help. If this is not the problem, please help me. tq so much. Below is partial of my script. tq.
<?php
//useracc-test.php
//start session
session_start();
// require 'lib/password.php';
require 'connect-test.php';
//retrieving part below
$userName= isset($_POST['username']) ? $_POST['username'] : '';
//$id= isset($_POST['id']);
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
?>
<html>
<head>
<style type="text/css">
#apDiv2 {
position: absolute;
left: 272px;
top: 148px;
width: 350px;
height: 338px;
z-index: 1;
}
#apDiv3 {
position: absolute;
left: 37px;
top: 379px;
width: 320px;
height: 157px;
z-index: 2;
}
</style>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css">
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
</head>
<body>
<div id="apDiv3">
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">Tab 1</li>
<li class="TabbedPanelsTab" tabindex="0">Tab 2</li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<p>
</p>
<?php while($row = $res->fetch_array()): ?>
<p><?php echo $row['id']; ?></p>
<p><?php echo $row['name']; ?></p>
<p><?php echo $row['username']; ?></p>
<p><?php echo $row['telno']; ?>
<?php endwhile; ?>
<?php
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2) VALUES (?,?,?,?)");
$stmt->bind_param("isss",$id,$name2,$color2,$hobby2);
$stmt->execute();
// $stmt->close();
// $conn->close();
}
?>
</p>
</div>
<div class="TabbedPanelsContent">
<form name="form2" method="post" action="useracc-test.php">
<p> </p>
<p>id :
<input type="text" name="id" id="id">
</p>
<p>name :
<input type="text" name="name2" id="name2">
</p>
<p>color2 :
<input type="text" name="color2" id="color2">
</p>
<p>hobby2 :
<input type="text" name="hobby2" id="hobby2">
</p>
<p>
<input type="submit" name="submit" id="submit" value="submit">
</p>
</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
<script type="text/javascript">
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
</script>
</body>
</html>
When you submit a form, the browser refreshes the page with whatever the submission form returns. So after the script updates the database, it needs to display the new contents of your profile, just like the original profile page does.
A simple way to do this is for the script to send a redirect back to the page that displays the profile form. When it's done, it can do:
header("Location: profile.php");
Another option is to use AJAX to submit the changes, rather than normal HTTP form submission. You'll need to learn Javascript for this.
the code would be:
while (true) {
//loop body
}
BUT in PHP code stops executing after the max_execution_time expires (ini_set('max_execution_time', 300);). You don't want an infinite loop in PHP because you want output from PHP and that will only happen after the script finishes.
Infinite loops are good for embedded code where the machine has to keep running code like a robot or a clockradio.
tq to all for the sugesstions. I appreciate. I apologise if my language is not polite because english is my 3rd language at home. It is not my native language. This is how i speak. I already solved the problem. I just use "print" function and made minor modifications. and the data never disappear. problem solved.tq all.
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
I am trying send email using php mail() function. It sends email but only HTML Part and not including the dynamic values through php. Any help will be appreciated.
I am trying to make it work but it is not working. I have tried multiple variations but still it only sends HTML part.
$to = $vendor_email;
$subject = "";
$mime_boundary = '<<<--==+X['.md5(time()).']';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: multipart/mixed;';
$headers[] = ' boundary="'.$mime_boundary.'"';
$message = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/home.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<title>BuyBusTours</title>
<style>
body{
font-family: Calibri,Candara,Segoe,"Segoe UI",Optima,Arial,sans-serif;
}
h4{
text-align: center;
}
#wrapper{
width: 60%;
}
hr{
border-color: black;
border-width: 1px;
}
label{
display: inline-block;
width: 250px;
}
</style>
</head>
<body>
<div class="container" id="wrapper">
<h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Booking Confirmation for Order Number: { <?php echo $order_id;?> }</h3>
<h4>Dear <?php echo $vendor_name ?></h4><br>
<h4>Thank you for Choosing BuyBusTours</h4>
<hr>
<p style="color:red;">This is the confirmation email that we have received your order and it is processing. Please do not confuse it with your e-ticket.</p>
<p>We have received your order and we are working on it. As soon as your order is confirmed we will send your E-Ticket. Complete information about your tour and other important details will be mentioned on your E-Ticket. If in case your tour is sold out or it is not operating we shall refund your complete payment.
</p>
<p>This is <b>NOT A CONFIRMATION</b> that you have reserved a seat on the bus. We are working on your order and as soon as it is confirmed we will send your e-ticket.</p>
<hr>
<h3 style="color:#F60">Order Details</h3>
<label>Tour Name:</label><?php echo $tour_name;?><br>
<label>Departure Date</label> <?php echo $dept_date;?> <br>
<label>Departure Location:</label> <?php echo $dept_loc;?> <br>
<label>Tour Code:</label> <?php echo $tourcode;?> <br>
<label>Vendor Tour Code:</label> <?php echo $vendorcode;?> <br>
<label>Number of People:</label> <?php echo $no_of_adult + $no_of_child;?> <br>
<?php if (!empty($no_of_rooms)) { ?>
<?php for ($i = 1 ; $i <= $no_of_rooms ; $i++) {?>
<?php if ($i == 1) {?>
<label style="color:#F33">Room <?php echo $i;?></label> <br>
<?php if (!empty($room1traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room1traveler1;?><br>
<?php } ?>
<?php if (!empty($room1traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room1traveler2;?><br>
<?php } ?>
<?php if (!empty($room1traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room1traveler3;?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 2) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room2traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room2traveler1;?><br>
<?php } ?>
<?php if (!empty($room2traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room2traveler2;?><br>
<?php } ?>
<?php if (!empty($room2traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room2traveler3;?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 3) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room3traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room3traveler1;?><br>
<?php } ?>
<?php if (!empty($room3traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room3traveler2;?><br>
<?php } ?>
<?php if (!empty($room3traveler3)) {?>
<label>Traveler 3: </label>
<?php $row["room3_traveler3"];?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 4) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room4traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room4traveler1;?><br>
<?php } ?>
<?php if (!empty($room4traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room4traveler2;?><br>
<?php } ?>
<?php if (!empty($room4traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room4traveler3;?><br>
<?php } ?>
<?php } ?>
<?php } }?>
<hr>
<h3 style="color:red; text-align:center">Total Amount Charged: <?php echo $total_price;?></h3>
<h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Contact us at: 0345-1272211</h3>
</div>
</body>
</html>';
$message = html_entity_decode($message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
<h4>Dear <?php echo $vendor_name ?></h4><br>
Should be
<h4>Dear '.$vendor_name.'</h4><br>
The reason being is that in the mail body you are already in <?php ?> tags, and so there is no needs to repeat them.
What's happening here is you are ending $message='.......... with Dear '
and then concatenating with the . $variable . (concatenate again) '</h4> Resume writing out $message.
Repeat this for all of the times you use a variable within a string.
Anything in there such as an if() statement or a for() loop, you should calculate these before you start defining any of the email strings, and assign each of their results an individual $var and assign just like I mention above. This will also make your code less messy, easier to navigate and thus adjust if and when you need to.
Your email message is being sent inside single quotes, ', so no PHP variables will be interpolated inside it. That will fix quite a few of the problems immediately, but a bigger problem is that you appear to have PHP statements inside there too, such as loops and conditionals – you can't put those inside strings.
What you need to do is pre-calculate your text, i.e. moving all those loops and conditionals out of your HTML message text, so that whatever they calculate end up just being variables. You should then switch to using double-quoted strings (or, in this case, heredoc is probably easier) to put those values into your message.