I have a little code that combines two arrays into a table with 2 columns. This works. But now I have a new array with values from $file3. I understand how to make a table with two columns, but how can I add a third column with the content of $file3?
<?php
$file1 = "c:/presencetool/ramfile1.txt";
$file2 = "c:/presencetool/ramfile2.txt";
$file3 = "c:/presencetool/ramfile3.txt"; //new file
if(file_exists($file1) && file_exists($file2))
{
$line1 = file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$line3 = file($file3, FILE_IGNORE_NEW_LINES); //new array
$combine = array_combine($line1, $line2); //I want to also combine $line3 here
$html = '<table align="center">';
$html .= '<tr><td width="350px";></td><td></td></tr>';
$i = 1;
foreach ($combine as $key => $value):
$html .= '<tr class="'.$value.'">';
$html .= '<td font-size:"90pt">'.$key.'</td>';
$html .= '<td font-size:"90pt">'.$value.'</td>';
//here something like $html .= '<td font-size:"90pt">'.$value2.'</td>';
$html .= '</tr>';
$i++;
endforeach;
$html .= '</table>';
echo $html;
}
In $file1, $file2 and $file3 are names, addresses and emails of some users.(one value per line)
if your array looks like this :
$line[] = ['name' => 'name1','email' => 'email#mail.com'];
$line[] = ['name' => 'name2','email' => 'email2#mail.com'];
$line[] = ['name' => 'name3','email' => 'email3#mail.com'];
Then try the below, hope will help you
$html = '<table align="center" border="1" width="50%">';
$html .= '<tr><th>Name</th><th>Email</th><th>Address</th></tr>';
for($i=0;$i<count($line1);$i++){
$html .= '<tr class="'.$line1[$i].'">';
$html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
Related
This should be a fairly simple problem, however I cannot for the life of me get it to work right.
I have JSON data in the form of an array like so and I am trying to generate a 2-column table where every 2 elements in the array take up a row.
[
{
"type": "Ground",
"field_location": "",
"field_user": "",
},
{
"type": "Water",
"field_location": "",
"field_user": "",
},
{
"type": "Sewer",
"field_location": "",
"field_user": "",
},
{
"type": "Tap",
"field_location": "",
"field_user": "",
}
]
I have overly simplified the code, however the function currently looks as follows.
function generateHtml($jsonDoc){
$html = "";
$html .= "<table>";
$count = 0;
foreach ($jsonDoc as $key => $value) {
$count ++;
if ($count % 2 == 0)
$html .= "<tr>";
$html .= "<td>";
foreach ($value as $key => $val) {
if(!empty($val))
$html .= $key.":".$val."<br />";
}
$html .= "</td>";
if ($count % 2 == 0)
$html .= "</tr>";
// $count ++;
}
$html .= "</table>";
print($html);
}
The resulting HTML output always seems to come out as follows and I cannot seem to understand what is missing.
<table>
<tr>
<td>Ground</td>
</tr>
<tr>
<td>Water</td>
</tr>
<tr>
<td>Sewer</td>
</tr>
<tr>
<td>Tap</td>
</tr>
</table>
as opposed to
<table>
<tr>
<td>Ground</td>
<td>Water</td>
</tr>
<tr>
<td>Sewer</td>
<td>Tap</td>
</tr>
</table>
```
which is what I am looking for.
One simple(r) way to achieve this, would be to use array_chunk to split your original data into an array of arrays, and then just use two nested loops to generate the output.
In the outer loop, you create the table row (output <tr> before, and </tr> after), and in the inner loop, you create a table cell for each item.
function generateHtml($jsonDoc){
$data = array_chunk($jsonDoc, 2); // split this into an array of arrays,
// containing two of the items each
$html = '<table>';
foreach ($data as $row) { // loop over the first level
$html .= '<tr>';
foreach ($row as $cell) { // loop over the second level
$html .= '<td>'.$cell->type.'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
print($html);
}
Try this
function generateHtml($jsonDoc){
$html = "";
$html .= "<table>";
$count = 1;
foreach ($jsonDoc as $key => $value) {
//$count ++;
if ($count % 2 != 0)
$html .= "<tr>";
$html .= "<td>";
foreach ($value as $key => $val) {
if(!empty($val))
$html .= $key.":".$val."<br />";
}
$html .= "</td>";
if ($count % 2 == 0)
$html .= "</tr>";
$count ++;
}
$html .= "</table>";
print($html);
}
I searched for some answers, but nothing worked. I have an HTML table in my index.php and everything works fine. I now want to add a new <td> and want it to be a clickable link.
This is the table now without the link:
<?php
$file1 = "c:/tablename.txt";
$file2 = "c:/tablestatus.txt";
$file3 = "c:/tablelocation.txt";
$file4 = "c:/userlist.csv";
if(file_exists($file1) && file_exists($file2))
{
$line1= file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$line3raw = file($file3, FILE_IGNORE_NEW_LINES);
$line3 = array_map("utf8_encode", $line3raw );
$line4 = file($file4, FILE_IGNORE_NEW_LINES);
$html = '<table align="center">';
$html .= '<tr><th width="460px"></th><th width="140px"></th><th width="340px"></th></tr>';
for($i=0;$i<count($line1);$i++){
$html .= '<tr class="'.$line2[$i].'">';
$html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
}else
{
echo "Files missing.";
}
?>
I found out how to make a cell as a link like this:
<td>title;</td>
That didn't work.
This works:
$html .= ''.$line1[$i].'';
But obviously, it's not part of the table anymore. How can I put this hyperlink into the table?
If you need more information or I missed something, just tell. I hope we can solve this issue.
Here, it is:
<?php
$file1 = "c:/tablename.txt";
$file2 = "c:/tablestatus.txt";
$file3 = "c:/tablelocation.txt";
$file4 = "c:/userlist.csv";
if(file_exists($file1) && file_exists($file2))
{
$line1= file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$line3raw = file($file3, FILE_IGNORE_NEW_LINES);
$line3 = array_map("utf8_encode", $line3raw );
$line4 = file($file4, FILE_IGNORE_NEW_LINES);
$html = '<table align="center">';
$html .= '<tr><th width="460px"></th><th width="140px"></th><th width="340px"></th></tr>';
for($i=0;$i<count($line1);$i++){
$html .= '<tr class="'.$line2[$i].'">';
$html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
}else
{
echo "Files missing.";
}
?>
I am returning book data (title, ISBN, author) information from a text file into an array. I am able to pull the data from the text file, read it it and display it as an array but how do I display this information into an HTML table? The HTML table needs to be created using PHP. Where I print the $book in the foreach, all the books are returned as an array but How do I output them in an HTML table created using solely php. I am NOT receiving errors, just need to know how to insert the return the data into an HTML table using PHP.
? Here is what I have so far
PHP
<?php
$infoArray = array(); //New empty array
$filename = 'books.txt';
$lines = count(file($filename));
$fp = fopen($filename, 'r');
for($ii = 1; $ii <= $lines; $ii++){
$line = fgets($fp);
array_push($infoArray, $line);
}
fclose($fp);
sort($infoArray);
list($title, $ISBN, $Author) = explode('*', $line);
$cntr = 0;
foreach ($dataReturned as $line){
print $line;
print '<br>';
}
This is what I want to print out as but it is not working. I am unsure how and where to incorporate this code.
$htmltable = "<table border='1'>";
$htmltable .= "<tr>";
$htmltable .= "<th>Title</th>";
$htmltable .= "<th>ISBN</th>";
$htmltable .= "<th>Author</th>";
$htmltable .= "</tr>";
$htmltable .= "<tr $style>";
$htmltable .= "<td>".$title."</td>";
$htmltable .= "<td>".$ISBN."</td>";
$htmltable .= "<td>".$Author."</td>";
$htmltable .= "</tr>\n";
print $htmltable;
TEXT FILE INFO
These are a few lines being pulled from the text file.
Healthy Living*1-4988-9986-x*Smith
How to Live Life*1-5698-9865-x*Romero
Better Speech*1-6996-9989-x*Grimes
This is one possible approach:
PHP:
<?php
#
$filename = 'file-books.txt';
$file = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
#
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>ISBN</th>";
echo "<th>Author</th>";
echo "</tr>";
echo "</thead>";
# Extract the lines.
echo "<tbody>";
foreach ($file as $row) {
$fields = explode('*', $row);
echo "<tr>";
echo "<td>".$fields[0]."</td>";
echo "<td>".$fields[1]."</td>";
echo "<td>".$fields[2]."</td>";
echo "</tr>";
}
#
echo "</tbody>";
echo "</table>";
?>
Text file:
Healthy Living*1-4988-9986-x*Smith
How to Live Life*1-5698-9865-x*Romero
Better Speech*1-6996-9989-x*Grimes
Just reading the file as a CSV (although with a * delimeter)...
$fp = fopen($filename, 'r');
$style = "";
$htmltable = "<table border='1'>";
$htmltable .= "<tr>";
$htmltable .= "<th>Title</th>";
$htmltable .= "<th>ISBN</th>";
$htmltable .= "<th>Author</th>";
$htmltable .= "</tr>";
while ( $row = fgetcsv($fp, null, "*")) {
$htmltable .= "<tr $style>";
$htmltable .= "<td>".$row[0]."</td>";
$htmltable .= "<td>".$row[1]."</td>";
$htmltable .= "<td>".$row[2]."</td>";
$htmltable .= "</tr>\n";
}
$htmltable .= "</table>\n";
fclose($fp);
echo $htmltable;
$html_table = '';
$html_table .= '<table>';
$html_table .= '<thead>';
$html_table .= '<th>Title</th>';
$html_table .= '<th>ISBN</th>';
$html_table .= '<th>Author</th>';
$html_table .= '</thead>';
$html_table .= '<tbody>';
foreach ($dataReturned as $line){
$html_table .= '<tr>';
$html_table .= "<th>$line[0]</th>";
$html_table .= '<th>$line[1]</th>';
$html_table .= '<th>$line[2]</th>';
$html_table .= '</tr>';
}
$html_table .= '</tbody>';
$html_table .= '</table>';
echo $html_table;
--- Sorry for creating this as new answer, cant edit my last comment with code formatting. ---
Can you show us an example of what data the variable $line contains? Because if its an array, you need to do another foreach inside the "foreach ($dataReturned as $line)" loop to get the data inside the variable $line or if its an object, try to get the respective attribute.
After knowing what you have, try to create an empty variable which is gonna contain all your HTML and use your foreach loop to append the columns and rows.
Are you sure that you have data in the array ? I was checking your code and look good, however you can use "echo" for print data in the browser.
You can test your code in this web page http://phptester.net/-
i hope that it could help you.
I am trying to fetch data in table row. The problem is I am getting different output. All columns are printed in one column.
foreach ($user1 as $key => $value) {
$date1 = date('Y-m-d', strtotime($date));
$timing= date('H:i:s', strtotime($date));
$error_p = implode(' ', $product[0])."<br>";
$error_d = implode(' ', $checked[0])."<br>";
$html = '<table border="1">';
$html .= "<tr>";
$html .= "<td bgcolor='#D0D0FF'>$error_p</td>";
$html .= "<td bgcolor='#D0D0FF'>$error_d</td>";
$html .= "</tr>";
$html .= "</table>";
$pdf->WriteHTML($html);
}
Output is like this:
The problem is that you create a new table element for every user. so put the table outside of your foreach loop so that it looks something like this:
$html='<table border="1">';
foreach ($user1 as $key =>$value) {
$date1 = date('Y-m-d',strtotime($date));
$timing= date('H:i:s',strtotime($date));
$error_p = implode(' ', $product[0]);
$error_d = implode(' ', $checked[0]);
$html .= "<tr>";
$html .= "<td bgcolor='#D0D0FF'>$error_p</td>";
$html .= "<td bgcolor='#D0D0FF'>$error_d</td>";
$html .= "</tr>";
}
$html.="</table>";
$pdf->WriteHTML($html);
I have an array that i pull from a database that looks like this
id - name - shortlink - downloadurl - count - date
Now I want to display that array as a table so the administartor of the site can se the content of the database. For that, I'm using this code:
function build_table($array){
// start table
$html = '<table id="usertable">';
// header row
$html .= '<tr class="header">';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
And it works great.
The problem is that i wan't do display some of the columns with a different code. E.g. 'downloadurl' which is an webadress, i want to make clickable. I just can't figure out how to split up the function so that i can write the code for the individual columns.
You could try something similar to the following:
using conditionals
This is probably the most straight forward way to go about handling different attributes per column - not without its drawbacks (noted below)
// data rows
foreach( $array as $key => $value) {
$html .= '<tr>';
foreach($value as $key2 => $value2) {
if ($key2 == 'downloadurl') {
$html .= '<td>Download</td>';
} else {
$html .= '<td>' . $value2 . '</td>';
}
}
$html .= '</tr>';
}
using switch statement
If you decide on this route, it may be a bit easier to manage in the long run as if () elseif () else() can become difficult to read over time.
foreach($value as $key2 => $value2) {
switch($key2) {
case 'downloadurl':
$html .= '<td>Download</td>';
break;
default:
$html .= '<td>' . $value2 . '</td>';
}
}
use this function to generate urls:
function getURL($downloadURL)
{
$html = "<a href = '$downloadURL'>Download</a>";
return $html;
}
And your function build_table():
function build_table($array){
// start table
$html = '<table id = "usertable">';
// header row
$html .= '<tr class = "header">';
foreach($array[0] as $key => $value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key => $value){
$html .= '<tr>';
foreach($value as $key2 => $value2){
$value2 = ($key2 == 'downloadurl') ? getURL($value2) : $value2;
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}