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);
Related
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;
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'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";
So I'm exploring the wonderful world of PHP and I'm still creating very dirty, poorly build code but I'm trying to get better! So my question is as follows:
Is there a way to automatically calculate the columns in the result set and spit out a pretty HTML table regardless of query used?
Here the current code:
<?php
include '../includes/connect.php';
include '../includes/queries.php';
$stid = oci_parse($conn, $export);
oci_execute($stid);
echo "<table class='pure-table pure-table-striped' style='font-size:11px;'><thead><tr><th>Name</th><th>File Name</th><th>Export Date</th></tr></thead>";
while (oci_fetch($stid)) {
echo "<tr><td>" . oci_result($stid, 'DISPLAY_NAME') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_FILE') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_DATE') . "</td></tr>";
}
echo "</table>\n";
oci_free_statement($stid);
oci_close($conn);
I'd like to use the same table every time, but just have it auto detect column header names and number of columns and return it in a table.
Is it possible or does it make sense?
I do exactly that using PDO.
$out = '';
$q = $conn->prepare($SQL);
if ($q->execute()) {
$rows = $q->fetchAll();
if (!empty($rows)) {
//We have something
$out .= "<table class='pure-table pure-table-striped' style='font-size:11px;'>";
$first = true;
//iterate on rows
foreach($rows as $row) {
//Clean out all the numeric keys - stops duplicate values
foreach ($row as $key => $value) {
if (is_int($key)) {
unset($row[$key]);
}
}
//header
if ($first) {
//write header
$out .= '<thead><tr>';
foreach ($row as $key => $value) {
$out .= '<th>' . $key . '</th>';
}
$out .= '</tr></thead>';
$first = false;
}
//write line
$out .= '<tr>';
foreach($row as $key => $value) {
$out .= '<td>' . $value . '</td>';
}
$out .= '</tr>';
}
$out .= '</table>';
}
}
echo ($out);
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;
}