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.
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!
I'm writing a program that has 3 pages.
On page 1 there is an option for the user to select a quantity of a breakfast product he wants to purchase. After selecting a quantity the user hits the submit button, if the user is not registered, it will take him to Page 2 for him to register. If the user is registered, it will direct them to Page 3.
However, if the user goes to Page 2 first and does not have a quantity selected from Page 1 it will redirect him to Page 1 after he registers and press submit, and then once they select a quantity and hit submit on Page 1 it will go to Page 3.
I'm struggling to maintain my session variables between the pages because two of them have forms that get overwritten if the user ever goes back to that page.
Page 1:
<?php
session_start();
$_SESSION['name']= $_POST['name'];
$_SESSION['email']= $_POST['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Product Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a class="active" href="product.php">Product</a></li>
<li>Registration</li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<?php
$action = '';
if (!empty($_SESSION['name']) or !empty($_SESSION['email'])) {
$action = "invoice.php";
}
else {
$action = "registration.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
<div class="container">
<img src="images/platter.jpg" alt="Breakfast Platter" style="float: left; width: 400px; height: 300px;";>
<h1>Breakfast Platter</p>
<p>The breakfast platter option comes with two fried eggs, four pancakes, and a bunch of bacon.</p>
Quantity: <input type="number" name="platter_quantity" min="0">
<p value="10.99" name="platter_price">Price: $10.99</p>
</div>
<div class="container">
<img src="images/yogurt.jpg" alt="Yogurt Parfait" style="float: left; width: 400px; height: 300px;">
<h1>Yogurt Parfait</p>
<p>The yogurt parfait option comes with two cups of yogurt, oats, and a mixture of berries.</p>
Quantity: <input type="number" name="yogurt_quantity" min="0">
<p value="6.99" name="yogurt_price">Price: $6.99</p>
</div>
<div class="container">
<img src="images/waffles.jpg" alt="Waffles" style="float: left; width: 400px; height: 300px;";>
<h1>Waffles</p>
<p>The waffles option comes with two buttermilk waffles with butter and syrup.</p>
Quantity: <input type="number" name="waffles_quantity" min="0">
<p value="$4.99" name="waffles_price">Price: $4.99</p>
</div>
<br>
<button class="button" type="submit" name="submit">Submit</button>
</form>
</body>
</html>
Page 2:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$_SESSION['platter_quantity'] = $_POST['platter_quantity'];
$_SESSION['yogurt_quantity'] = $_POST['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $_POST['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li><a class="active" href="registration.php">Registration</a></li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<br>
<?php
$action = '';
if (!empty($_SESSION['platter_quantity']) or !empty($_SESSION['yogurt_quantity']) or !empty($_SESSION['waffles_quantity'])) {
$action = "invoice.php";
}
else {
$action = "product.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
Name: <input type="text" name="name" pattern="[A-Za-z]" required><br><br>
E-mail: <input type="text" name="email" pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required><br><br>
<input type="submit">
</form>
<br>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $_SESSION["platter_quantity"] . "<br>";
echo "Yogurt: " . $_SESSION["yogurt_quantity"] . "<br>";
echo "Waffles: " . $_SESSION["waffles_quantity"];
?>
</body>
</html>
Page 3:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Invoice Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li>Registration</li>
<li><a class="active" href="invoice.php">Invoice</a></li>
<li style="float:right">Login</li>
</ul>
<h1>Hi! Welcome <?php echo $_SESSION['name']; ?>! </h1>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $platter_quantity . "<br>";
echo "Yogurt: " . $yogurt_quantity . "<br>";
echo "Waffles: " . $waffles_quantity;
?>
</body>
</html>
What's the best way for me to implement this using session variables without using a database?
I tried doing this as well, but it did not seem to work:
<?php
session_start();
if (empty($_SESSION['name']) or empty($_SESSION['email'])) {
$_SESSION['name'] = $POST_['name'];
$_SESSION['email'] = $POST_['email'];
}
else {
$name = $_SESSION['name'];
$name = $_SESSION['email'];
}
if (empty($_SESSION['platter_quantity']) or empty($_SESSION['yogurt_quantity']) or ($_SESSION['waffles_quantity'])) {
$_SESSION['platter_quantity'] = $POST_['platter_quantity'];
$_SESSION['yogurt_quantity'] = $POST_['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $POST_['waffles_quantity'];
}
else {
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
}
?>
You never insert $_POST or $_GET without first checking if they're set isset($_POST['variable']), and you can use checks here as well - do a check for the existence of $_POST-variables, and if they exist, use them, and if not, assign the $_SESSION-variables. So on page 3, you will have something like:
$name = $_SESSION['name'] = (isset($_POST['name']) ? $_POST['name'] : ((isset($_SESSION['name']) ? $_SESSION['name'] : '')));
And so on for the other variables. What this does is checks for $_POST, and if it's set, it updates the $_SESSION-variable, and if it's not set, it just updates the $_SESSION-variable with the already existing $_SESSION-variable, and if that doesn't exist either, it sets both variables $name and $_SESSION['name'] to empty, which you then can check for later in the script (and redirect etc.)
I am creating a div structure for displaying data from db.
In my case all are getting written else I was not able to print that data properly.
I have one main div (col-md-12) and in that I have added (col-md-4)
I have fetch images from db like echo $img; in php so the images will come properly but they are not from left to right like they are displayed below on each div rather than inserting left to right, so my question is how to insert that div from left-to- right rather than below of each other
and what i want like this below img
This is my code
Your HTML structure should be like that:
<div class="main">
<div class="col-sm-12">
<div class="col-sm-4">
// image one
</div>
<div class="col-sm-4">
// image two
</div>
<div class="col-sm-4">
// image three
</div>
</div>
</div>
IF you are using HTML inside the loop than you can follow this:
<div class="main">
<div class="col-sm-12">
<?
$i = 1;
foreach ( yourloop ) {
?>
<div class="col-sm-4">
// image one
</div>
<?
// after every third record use clear div
if($i == 3){
?>
<div style="clear:both"></div>
<?
$i = 1; // reassign the value
}
$i++;
}
?>
</div>
</div>
UPDATE 1:
Solution with your provided code:
<div class="main">
<div class="col-sm-12">
<?
while ( yourloop ... ) {
?>
<div class="col-sm-4">
// your stuff
</div>
<?
}
?>
</div>
</div>
What i have changed?
you need to use <div class="col-sm-4"> inside your loop and you can also follow the second example with clear:both; div.
UPDATE 2 (For Future aspects):
For new designers and developers you must need to understand Basic grid layouts:
FROM Bootstrap Examples:
You can also follow this link getting information about the difference between bootstrap classes:
FROM other answer:
.col-xs = *Extra small devices (ie Phones) (<768px)
.col-sm = Small devices (ie Tablets) (≥768px)
.col-md = Medium devices (ie laptops, or small desktops) (≥992px)
.col-lg = Large devices (ie Desktops) (≥1200px)*
Since this is bootstrap you can do this by keeping three images in a row.
Pseudocode
counter = 0
for i in images:
if counter = 0:
print "<div class = 'row'>"
print "<div class='col-md-3'>"+i+"</div>"
counter++;
counter = counter % 3;
if counter == 0:
print "</div>"
You could simply create blocks with the (CSS) style definition float:left; as in the example below:
<?php
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"de-de\" lang=\"de-de\" >
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
<style type="text/css">
";
echo '.panel { float:left; margin:4px; border:1px solid #999; }'.PHP_EOL;
echo '.clear { clear:both; }'.PHP_EOL;
echo 'H3 { font-size:0.8em; background-color: #666; color:white; margin:0 0 4px 0; font-weight:bold; text-align:center; }'.PHP_EOL;
echo "</style>
</head>
<body>
";
echo '<h2>Block 1</h2>';
for ($xi=1; $xi<=5; $xi++) {
echo '<div class="panel"><h3>Panel 1.'.$xi.'</h3><br />contents go here<br />contents go here<br />contents go here</div>';
}
echo '<div class="clear"></div>';
echo '<h2>Block 2</h2>';
for ($xi=1; $xi<=5; $xi++) {
echo '<div class="panel"><h3>Panel 2.'.$xi.'</h3><br />contents go here<br />contents go here<br />contents go here</div>';
}
echo '<div class="clear"></div>';
echo "</body>
</html>
";
?>
I have been trying to mimick the nested comment system that reddit use. Now i have a app that does in a way look like what it was intended to look. But, if you have a look at the reddit style:
I haven't been able to reproduce the lines that attach the previous answer.
My thought is that is nested divs somehow.
currenlty I'm outputting the db data lik this:
<?php if(!empty(getMessage())): ?>
<div class="container">
<h1>The nested comment exampel!</h1>
<?php foreach (getMessage() as $value){ ?>
<div class="level_<?= $value->level; ?> comment" id="<?= $value->id; ?>">
<?php if($value->visible == 0) : ?>
<?= $value->date ?><br>
Deleted
<?php elseif($value->visible == 1): ?>
<?= $value->date ?> X <?= 'id: '. $value->id . ', sort order: ' .$value->sort_order . ', level: '.$value->level ;?><br>
<?= $value->comment_text ?>
<form action="index.php" method="post">
<input type="hidden" name="id" value="<?= $value->id ?>" />
<input type="hidden" name="parent" value="<?= $value->reply_to ?>" />
Add comment: <input type="text" name="svar">
<input type="submit">
</form>
<?php endif; ?>
</div>
<?php } endif; ?>
</div>
And i have solved the left margin with a simpel (not so clean) solution with
<style>
div.level_ {
margin-left: 00px;
}
div.level_1 {
margin-left: 20px;
}
div.level_2 {
margin-left: 40px;
}
div.level_3 {
margin-left: 60px;
}
div.level_4 {
margin-left: 80px;
}
div.level_5 {
margin-left: 100px;
}
div.level_6 {
margin-left: 120px;
}
div.level_7 {
margin-left: 140px;
}
div.level_8 {
margin-left: 160px;
}
div.level_9 {
margin-left: 180px;
}
div.level_10 {
margin-left: 200px;
}
</style>
I have these columns in the database
id,comment_text, reply_to, sort_order, level, date, ip, visible
And my "clone" or what you can call it simply reads the parent id (reply to) and updates all rows if a comment is addad before. if a comment is addad to the first comment/post, the sort_order is simply +1.
But, how can i output a "comment" inside the post. and a answer
comment inside its parent comment.
Currently it looks like this
EDIT
To make it clear i have prepared some mock up data.
Here is the result I'm looking for: http://jsfiddle.net/wv9j9ueo/
With dis data http://pastie.org/10400764 notice the level column. Shouldn't it be possible to foreach the column levelfrom 0 and then keep going until there is nothing left (notice that the can be two or more of the same value).
If you put divs into the parents your code looks prettiest:
<div class="comment">
<p>The comment</p>
<div class="comment">
<p>The comment</p>
<div class="comment"> <p>The comment</p></div>
</div>
</div>
Css
.comment {
margin-left: 15px;
border-left : 2px dotted blue;
padding : 0 10px;
}
PHP
<?php if(!empty(getMessage())): ?>
<div class="container">
<h1>The nested comment exampel!</h1>
<?php
$i=0; //initialize flag
foreach (getMessage() as $value){ ?>
<div class="comment" id="<?= $value->id; ?>">
//you don't need level class
<?php if($value->visible == 0) : ?>
<?= $value->date ?><br>
Deleted
<?php elseif($value->visible == 1): ?>
<?= $value->date ?> X <?= 'id: '. $value->id . ', sort order: ' .$value->sort_order . ', level: '.$value->level ;?><br>
<?= $value->comment_text ?>
<form action="index.php" method="post">
<input type="hidden" name="id" value="<?= $value->id ?>" />
<input type="hidden" name="parent" value="<?= $value->reply_to ?>" />
Add comment: <input type="text" name="svar">
<input type="submit">
</form>
<?php endif; ?>
<?php
$i++; //sum flag
}
for($x=0; $x<=$i; $x++) { //paint div closers
echo '</div>' ;
}
?>
<? endif; ?>
</div>
See it working
http://jsfiddle.net/jc5104st/1/
I know you are able to do
<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
but is it possible to create a string outside a php script?
<?php $email_body = ' ?>
<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
<?php '; ?>
<?php
email($email_body, $to, $from);
?>
I want to keep the script formatted in html... but it is kinda ugly when you create a string with html... you can't really format it well.
You're looking for a Heredoc (or a Nowdoc, in which variables are not interpreted. More info in the same section of the PHP documentation linked)
<?php $email_body = <<<EOS
<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
EOS;
email($email_body, $to, $from);
?>
You can do it using buffers (this way you can use php in your html) :
<?php
ob_start();
?>
<body>
<h1>This is my email the <?=date('d/m/Y')?></h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
<?php
$html = ob_get_contents();
ob_end_clean();
?>
Do you want this?:
<php
$email_body = '<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>';
email($email_body, $to, $from);
?>