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>';
Related
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 = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> " . while($res = $result->fetchArray(SQLITE3_ASSOC))
{
echo
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
} . "
</tbody>
</table>";
Hello friends, I have a php page that does not allow me to show the table correctly I do not know how to place the while, please can you help me.
works friend, but I want to save the result of this table in a php variable to print it in
$ pdf-> writeHTML ($table, true, false, true, false, '');
You can't use echo when you are already inside and echo. Try:
$output = '<p><table width="770px">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody>';
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$output .= '<tr><td>' . $res['fecha'] . '</td>' .
'<td>' . $res['nombre'] . '</td>' .
'<td>' . $res['cantidad'] . '</td>' .
'<td>' . $res['precio_hospital'] .
'<br></td></tr>';
}
$output .= '</tbody></table>';
Like this:
$myVar = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> ";
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$myVar .=
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
}
$myVar .= "</tbody></table></p>";
//echo $myVar; //if you want to see the result
//$pdf->writeHTML ($myVar, true, false, true, false, ''); //to pdf
You can't concatenate a control structure to a string. PHP will throw a syntax error.
Edited to use a variable
I've been trying to create a web application that functions like a library system, and I am currently working on building the table that will display all books in the system. However, after I close the first echo statement to start building the table in HTML, the app seems to just stop seeing it as code and prints the rest of the PHP code to the screen.
Here is the code in question:
<?php
$db = mysqli_connect('34.224.99.227','root','opendoor','library')
or die ('Could not connect to database');
$result = mysqli_query($db,'SELECT * FROM book');
echo '<table id="table_id" class="display">
<thead>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Status</th>
</tr>
</thead>
<tbody>';
while($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['isbn'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
mysqli_close($db);
?>
And here is the corresponding web page output:
ISBN Title Author Description Status '; while($row = mysqli_fetch_array($result)) { echo ''; echo '' . $row['isbn'] . ''; echo '' . $row['title'] . ''; echo '' . $row['author'] . ''; echo '' . $row['description'] . ''; echo '' . $row['status'] . ''; echo ''; } echo ''; mysqli_close($db); ?>
I'm obviously not too well-versed in PHP, so I'm at a loss as to what the problem could be.
After playing around with the settings of my LAMP stack, I realized that I had apparently configured it with PHP 5 in mind, while I had downloaded PHP 7. Once I fixed the discrepancy, everything worked as intended. Thanks for help nonetheless.
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;
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> ...'