Mixed Bag of HTML and PHP - php

In my PHP page, I am extracting a bunch of variables from the URL, and formatting their output into a nice HTML table. One section in the table needs to be dynamically created, dependent upon what was ordered on the previous webpage. Finally, I'm using the $mail function to send the HTML table with all the info to an email recipient.
The table works great, EXCEPT for the dynamic section with the while loop. The compiler is getting confused because my syntax is wrong. I suspect this is because my code is inside the $message'...' variable. Any advice?
<?php
// Extracting the variables from URL
$params = $_SERVER['QUERY_STRING'];
// Placing the variables into $array
$array=array();
parse_str($params,$array);
// Identifying the length of the main array and creating an array of KEYS
$keys = array_keys($array);
$keysCount = count($keys);
// And creating an array of corresponding values
$values = array_values($array);
$to = "steve#dutchlandfrozenfoods.com";
$subject = "NEW EUROCLASSIC ORDER";
$message = '
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EuroClassic Fine Family of Pastries - Ordering</title>
</HEAD>
<BODY bgcolor="#F0EFEE">
<table id="hor-minimalist-b" summary="Customer Information">
<thead>
<tr>
<th scope="col">Customer Contact:</th>
<th scope="col">Shipping To:</th>
</tr>
</thead>
<tbody>
<tr>
<td>' . $values[0] . '</td>
<td>' . $values[1] . '</td>
</tr>
<tr>
<td>' . $values[2] . '</td>
<td>' . $values[3] . '</td>
</tr>
<tr>
<td>' . $values[4] . '</td>
<td>' . $values[5] . '</td>
</tr>
<tr>
<td></td>
<td>' . $values[6] . '</td>
</tr>
</tbody>
</table>
<table id="hor-minimalist-b" summary="Order Details">
<thead>
<tr>
<th scope="col">Product:</th>
<th scope="col">Item Code:</th>
<th scope="col">Quantity:</th>
<th scope="col">Ext Price:</th>
</tr>
</thead>
<tbody>
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
<tr>
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
</tr>
}
</tbody>
</table>
</BODY>
</HTML>
';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To:Steve <steve#dutchlandfrozenfoods.com>\r\n";
mail($to, $subject, $message, $headers);
?>

Prepare the tbody in a variable before.
$tbody = '';
while ($i = 13; $i < $keysCount-3; $i = $i+2;) {
$tbody .= '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td></tr>';
}
And then:
$message = "...<tbody>" . $tbody . "</tbody> ....";
But I must say, the logic here isn't understandable, and if something changes, for example, count of columns, code could become quite hard to maintain.

Briedis point is important and necessary. but more simply - that is not how a while loop works
while(exp)
{
//body runs while exp remains true
}
You don't put three expressions up there like you do in a for loop. If you want to keep it as a while loop, do
$i = 13
while($i < $keysCount-3)
{
//body, with necessary increments
}
And if you decide to do a for loop instead, there is no semi-colon after the third (generally the incrementing) expression
Whichever method of loop you use, you need to take it out of the string as Briedis indicated. either make a temp variable such as $tbody or repeatedly concatenate to $message in the loop body.

As far as I know, you cannot call a PHP function and set it equal to a variable at the same time. you would need to do something similar to the following:
'... <tbody>';
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
$message = '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td></tr>';
}
$message = '</tbody> ...'

Related

Show table with results grouped 3 by 3

I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc

Table with dynamic looping

I have created table with dynamic looping (if I have 100 of records, it should show only 15 record in one table)
With this I have 20 records creating two tables is done but the problem is in column dynamic is not getting and same records are displaying, for displaying in one table 15 records and 5 records in other table:
Here is my code:
<?php
$total = $totalQues / 15;
for ($row = 0; $row < $total; $row++)
{
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$j = 0;
$k = 0;
$l = 0;
for ($i = 0; $i < 15; $i++)
{
$j = $i + $totalQues;
$k = $i + 30;
$l = $i + 45;
$m = $i + 1;
$n = $k + 1;
$o = $l + 1;
if (empty($r[1][$i]->corr_ans))
{
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td></td>
<td></td>
</tr>';
}
else
{
$uans = strtolower($r[1][$i]->corr_ans);
$cans = strtolower($r[1][$i]->user_ans);
$u = ord($uans);
$cr = ord($cans);
// echo "u=".$u."cr=".$cr;
if ($u == $cr):
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
else:
echo '<tr style = "color:red;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
endif;
}
}
echo '</tbody>';
echo '</table>';
}
?>
I believe the following code should do the job for you:
$numberOfTables = $totalQues/15;
$maxRows = 15
for($table = 0; $table < $numberOfTables; $table ++){
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$tableMax = ($table < $numberOfTables-1 ? $maxRows*($table+1) : $totalQues)
for($row = $table*$maxRows; $row < $tableMax; $row++){
if (!empty($r[1][$row]->corr_ans)) {
$uans = strtolower($r[1][$row]->corr_ans);
$cans = strtolower($r[1][$row]->user_ans);
$u = ord($uans);
$cr = ord($cans);
//echo "u=".$u."cr=".$cr;
$color = ($u == $cr ? 'green' : 'red');
echo '<tr style = "color:'.$color.'">
<td>' . $row . '</td>
<td>' . strtoupper($r[1][$row]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$row]->user_ans) . '</td>
</tr>';
}
}
echo '</tbody>';
echo'</table>';
}
?>

Syntax error with Escaping html in php code

I am having syntax error with my following code
<?php
If (!empty($_SESSION['LogedinStudentId'])) {
echo '<h3>Your Scholarship Applications:</h3>
<table width="100%" class="table table-bordered">
<tr>
<th scope="col">Sr.No.</th>
<th scope="col">Date of Application</th>
<th scope="col">Course Type</th>
<th scope="col">Course Description</th>
<th scope="col">Subject</th>
<th scope="col">Applied for Semester No.</th>
<th scope="col">Scholarship Status</th>
<th scope="col">View / Print</th>
</tr>
<tr>
<td>' . ++$serialno . '</td>
<td>' . if(empty($row_studentdashboard['DateofApplication'])) {
echo ' ';
} else {
echo date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
. '</td>
<td>' . $row_studentdashboard['CourseType'] .'</td>
<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>
<td>' . $row_studentdashboard['Subject'] .'</td>
<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>
<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>
<td>View / Print</td>
</tr>
</table>';
} else {
echo '<h3>You do not have any application pending</h4>';
}
?>
I am getting syntax error on line no. 17 and 22. The second (nested) if statement is throwing syntax error. I can not judge what is wrong. If I run this second if statement outside of the html it is working fine.
Can anyone point out what's wrong?
What you are doing is an if-statement inside of echo-statement. It is wrong.
Run second if-statement outside of html and create a variable that you later print in your html.
A kind of this:
if(empty($row_studentdashboard['DateofApplication'])) {
$text = ' ';
} else {
$text = date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
}
.....
<td>' . ++$serialno . '</td>
<td>' . $text . '</td>
You 're not supposed to concatenate an if statement to a string. That is what you did on line 17/18
Try :
<?php
if (isset($_SESSION['LogedinStudentId']) && !empty($_SESSION['LogedinStudentId'])) {
$out = '<h3>Your Scholarship Applications:</h3>';
$out .= '<table width="100%" class="table table-bordered">';
$out .= '<tr>';
$out .= '<th scope="col">Sr.No.</th>';
$out .= '<th scope="col">Date of Application</th>';
$out .= '<th scope="col">Course Type</th>';
$out .= '<th scope="col">Course Description</th>';
$out .= '<th scope="col">Subject</th>';
$out .= '<th scope="col">Applied for Semester No.</th>';
$out .= '<th scope="col">Scholarship Status</th>';
$out .= '<th scope="col">View / Print</th>';
$out .= '</tr>';
$out .= '<tr>';
$out .= '<td>' . ++$serialno . '</td>';
$out .= '<td>';
if(!isset($row_studentdashboard['DateofApplication']) || empty($row_studentdashboard['DateofApplication'])) {
$out .= ' ';
} else {
$out .= date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
$out .= '</td>';
$out .= '<td>' . $row_studentdashboard['CourseType'] .'</td>';
$out .= '<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>';
$out .= '<td>' . $row_studentdashboard['Subject'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>';
$out .= '<td>View / Print</td>';
$out .= '</tr>';
$out .= '</table>';
} else {
$out = '<h3>You do not have any application pending</h4>';
}
echo $out;

How to place sum values (from MySQL using PHP) above presentation table?

I am looking for simple, the best practical way of placing summed value above HTML table.
Normally loops can generate sums after the loop is finished that is below the table of all results. It may be very difficult when the table is very long and we need to scroll down every time to see the sum of sales. Do I need to run two loops or is there any trick to do that? Task seems very simple. In below code I am summing Quantities from all orders and Total Sales values:
$lp=1; $total=0; $total_qt=0;
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
echo '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") echo '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
echo '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
echo '</tr>';
}
echo '</table><div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
You could use a variable to store your output while you are in the loop, rather than echo it, and when you finished the loop then echo the stored results with both totals before and after them. Like this:
$lp=1; $total=0; $total_qt=0;
$output = ''; //here you temporarily save your output
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
$output .= '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") $output .= '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
$output .= '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
$output .= '</tr>';
}
//Now you can put a div with the Total both at the start and end of the table
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div>';
echo $output . '</table>';
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
By using the SUM() SQL function you'll be able to access the sums without needing to run PHP:
SQL
SELECT SUM(Total) FROM Orders

How to print each <td>..</td> on a new line in PHP?

Currently, each td../td prints in just 1 line which makes the source code very hard to read, any way I can print each td../td element on new line to make it much more readable.
thanks for replies. i tried the break but it prints on screen, need to put it in here but where?
$entries[$i] = '<td>'.$id[$i] .'</td>';
$entries[$i] .= '<td>'.$username[$i].'</td>';
$entries[$i] .= '<td>'.$first_name[$i].'</td>';
Further Edit:
The relevant HTML
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<div class="row">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Country</th>
<th>Referrer</th>
</tr>
</thead>
<tbody>
<tr>
<?php
if (isset($content) && !empty($content) && is_array($content)) {//2
$i = 0;
$entries = array();
while(array_key_exists($i, $content)) {//1
$entries[$i] = "\n" .'<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" .'<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$first_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$last_name[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$email_address[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$country[$i].'</td>';
$entries[$i] .= "\n" .'<td>'.$where_about[$i].'</td>';
//$entries[$i] .= '<td>'.$sample[$i].'</td>';
echo $entries[$i];
$i++;
}//1
}//2
?>
</tr>
</tbody>
</table>
concat "\n" to the front of the statement
Edit
$entries[$i] = "\n" . '<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" . '<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" . '<td>'.$first_name[$i].'</td>';
A neat trick is to define a constant with a linebreak:
define('NL', "\r\n");
Then we just concat that constant not worrying about using single our double qoutes usage:
$entries[$i] = '<td>'.$id[$i] .'</td>' . NL .
'<td>'.$username[$i].'</td>' . NL .
'<td>'.$first_name[$i].'</td>' . NL;
Same trick ofcourse can be applied to tabs:
define('TAB', "\t");
So we can do something radical like this, for instance:
$entries[$i] = '<tr>' . NL .
TAB . '<td>' . $id[$i] . '</td>' . NL .
TAB . '<td>' . $username[$i] . '</td>' . NL .
TAB . '<td>' . $first_name[$i] . '</td>' . NL .
'</tr>' . NL;
Spaces are up to personal preference ofcourse but when you're consistent it even keeps your code readable!
And in your case you could make your code even cooler by working through those columns (<td>) with a foreach loop through all fields.
$entries[$i] = '<tr>' . NL;
foreach(array('id', 'username', 'first_name') as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
Now tell me that isn't cool!
Seems like your code simply lacks the intelligence to add each column in its own table row (tr), here is your relevant PHP code with the fixes and suggested additions, it should work but I can't test it.
<?php
define('NL', "\r\n");
define('TAB', "\t");
if (isset($content) && !empty($content) && is_array($content)) {
$i = 0;
$entries = array();
$fields = array('id', 'username', 'first_name', 'last_name', 'email_address', 'country', 'where_about');
while(array_key_exists($i, $content)) {
$entries[$i] = '<tr>' . NL;
foreach($fields as $key) {
$entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}
$entries[$i] = '</tr>' . NL;
echo $entries[$i];
$i++;
}
}
?>
As I am reading this, walking through $content with a while might not be good idea, try foreach($content as $index => $value) instead then would make $index the equivelant of $i but you wouldn't need to use $content[$index] because that exact value will be present in $value in a foreach.
Use '\n' after the td tag you echo/print
You can add a line break to your print statement.
echo "My text\n";
You can also add tabs to get it indented neatly.
echo "\t\tMy text\n";
<?
foreach($array as $single) {
echo "<td></td> \n";
}
?>
I would just like to add: If you're using Windows to read the code, you should use \r\n instead of just \n
Not sure what you're asking but if you want to get rid of the :
$s .= '<td>' . $n . '</td>';
use ($co means close open)
$co = '</td><td>';
$row = '<tr><td>' . $data . $co . $data2 . $co . $data3 . '</td></tr>';

Categories