Cell Hyperlink in PHP using HTML - php

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.";
}
?>

Related

PHP array to table with more than two columns

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;

How to parse data returned from a text file into an HTML table using PHP

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.

Print HTML table in foreach loop

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);

TCPDF - Having trouble with getting MySQL content to show in PDF

I am trying to make a printable version of a catalogue of products. If I display the products in HTML it works fine. But when I try and use TCPDF I loose half of it or nothing at all.
$query = "SELECT ID, Category_Name, Image FROM Categories ORDER BY Position ASC";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($cat = mysqli_fetch_row($result)) {
// add a page
$pdf->AddPage();
$query1 = "SELECT * FROM Products WHERE Category = " . $cat[0] . " AND Visibility = 1 ORDER BY Product_Name ASC";
$result1 = mysqli_query($connection, $query1);
$html = $query1;
confirm_query($result1);
//$html .= '<style>'. file_get_contents('./images/print.css').'</style>';
$html .= '<table style="margin: 0 auto;"><tbody>';
$html .= '<tr><th colspan="6"><div id="title" style="color: white;">' . $cat[1] . '</div></th></tr>';
$html .= '<tr>';
$html .= '<th></th>';
$html .= '<th>Product</th>';
$html .= '<th>Pack Size</th>';
$html .= '<th>Price(Ex VAT)</th>';
$html .= '<th>Price(Inc)</th>';
$html .= '<th>RRP</th>';
$html .= '</tr>';
// Show product prodcuts from selected category
while ( $row123 = mysqli_fetch_row($result1)) {
$html .= '<tr><td>';
$html .= '<img width="100px" height="100px" src="/images/' . $row123[6] . '"/>';
$html .= '</td><td>';
$html .= '<b><u>' . $row123[1] . '</u></b><br /><i>' . $row123[5] . '</i>';
$html .= '</td><td>';
$html .= $row123[3];
$html .= '</td><td>';
$html .= '£' . $row123[4];
//echo '<br />';
$html .= '</td><td>';
$query2 = "SELECT VAT FROM VAT WHERE ID = " . $row123[7];
$result2 = mysqli_query($connection, $query2);
confirm_query($result2);
// Put results in an accessible form
$result_array2 = array();
while ($row234 = mysqli_fetch_row($result2)) {$result_array2[] = $row234;}
$html .= '£' . number_format($row123[4] + ($result_array2[0][0] * $row123[4] / 100), 2, '.', '');
$html .= '</td><td>';
if ($row123[9] == "") {
$html .= "N/A";
} else {
$html .= '£' . number_format($row123[9], 2, '.', '');
}
$html .= '</td>';
$html .='</tr>';
}
$html .= '</tbody></table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
}
If I comment out the second while loop, I can get the first query ($query1) to echo out. One per page for each category. But when I uncomment it, it just creates 4 blank pages.

Parse error: syntax error, unexpected ';', expecting identifier (T_STRING) or variable (T_VARIABLE)

I have problem with my code. (I've looked for some solution but i didn't found anything, so thats why I'm posting a topic)
Error:
Parse error: syntax error, unexpected ';', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in XXXXXX on line 13
Line 13:
echo $view->;htmlError();
My code:
<?php
require_once 'classes/View.php';
echo DB_HOSTNAME; //output: hostname
$view = new View();
if ($htmlString = $view->tableThreads()) {
echo $htmlString;
} else {
echo $view->;htmlError();
}
echo $view->buttonPostThread();
?>
View.php
// we need the class db to make an object
require_once 'database.php';
//we'll also need the recaptcha helper later
require_once 'helpers/recaptcha.php';
class View{
private $db;
function __construct()
{
$this->db = new Database();
}
function tableThreads()
{
$content = "";
if (($threads = $this->db->getThreads()) && mysql_num_rows($threads) > 0) {
$content .= '<h1>Threads</h1>';
$content .= '<table border="0" width="" id="posts_list">';
$content .= '<tr>';
$content .= '<th class="title">Title</td>';
$content .= '<th>Date</td>';
$content .= '<th>User</td>';
$content .= '</tr>';
while ($row = mysql_fetch_assoc($threads)) {
$content .= '<tr class="thread">';
$content .= '<td class="title">';
$content .= '<a href="view_thread.php?permalink=';
$content .= htmlspecialchars($row['permalink']) . '">'.$row['title'].'</a>';
$content .= '</td>';
$content .= '<td class="date">'.htmlspecialchars($row['date']).'</td>';
$content .= '<td class="author">'.htmlspecialchars($row['author']).'</td>';
$content .= '</tr>';
}
$content .= '</table>';
return $content;
} else {
return false;
}
}
private function composeTable($post, $firstPost, $numRows)
{
$htmlTable = "";
if ($firstPost)
$htmlTable .= '<h1>'.htmlspecialchars($post['title']).'</h1>';
$htmlTable .= '<table border="0" width="895">';
$htmlTable .= ' <tr>';
$htmlTable .= ' <th>Message</th>';
$htmlTable .= ' <th>Date</th>';
$htmlTable .= ' <th>Author</th>';
$htmlTable .= ' </tr>';
$htmlTable .= ' <tr>';
$htmlTable .= ' <td class="title">'.htmlspecialchars($post['content']).'</td>';
$htmlTable .= ' <td class="date">'.htmlspecialchars($post['date']).'</td>';
$htmlTable .= ' <td class="author">'.htmlspecialchars($post['author']).'</td>';
$htmlTable .= ' </tr>';
$htmlTable .= '</table>';
if ($firstPost && $numRows > 1)
$htmlTable .= '<h1>Responses</h1>';
return $htmlTable;
}
function tableThreadContent($permalink)
{
$content = "";
if ($posts = $this->db->getContentThread($permalink)) {
$num_rows = mysql_num_rows($posts);
if ($num_rows > 0) {
while($row = mysql_fetch_assoc($posts))
$content .= $this->composeTable($row,
is_null($row['permalink_parent']),
$num_rows);
}
return $content;
} else {
return false; //database error
}
}
[/html]
<p>The second method goes around all the posts in a thread and composes the HTML with the first one (composeTable). </p>
<p>The method composeTable is private because we'll only call it from the tableThreadContent method in the same class and its functionality is only useful inside this class. In the future if we want to make a class that extends this one and uses that method all we need to do is change private for protected.</p>
<p>Now let's think about what happens if we don't have a single thread. Apart from being very sad it could be a problem if we don't show a warning message. This is a very simple method to do that:</p>
function htmlError($from_view_thread = false)
{
if ($from_view_thread) {
//From view_thread.php
$html = '<p class="error">There is no thread with this title. Sorry! ';
$html .= 'You can go back to the main page.</p>';
}else{
// From index.php
$html = '<p class="error">There aren\'t any threads. Sorry! </p>';
}
return $html;
}
function buttonPostThread()
{
return '<div class="newThread">'
.'Create a new thread'
.'</div>';
}
You need to correct the line:
echo $view->;htmlError();
To:
echo $view->htmlError();
For rest, please check your code.
You have to substitute the line 13 for:
echo $view->html Error();
because you have an extra ";"
in PHP 5.5 and above
$this->middlewares[$k] = Instance::ensure($middleware, Middleware::class);
and in PHP 5.5 below
$this->middlewares[$k] = Instance::ensure($middleware, 'Middleware');
PHP 5.4 not Support Class::class

Categories