List files in folder sub-directory - php

I have a script that when clients create a pdf file, it is placed in a folder named with there own code. This is in a main folder called 'destcerts' and the folder structure ends up looking like the illustration below. What I need to do as admin is have access to these files and the only way I can do that at the moment is to hard code the sub-directory in the code and that is not practical for obvious reasons.
What I thought I could do is, have a select dropdown with a list of clients which when selected could trigger perhaps a change event which would say loop for the chosen client and display the files. Unfortunately, my php or jquery is not good enough to achieve this and I would be grateful if someone could show me the way to go forward with this. Many thanks
php code
<div id="viewCerts" style="display:none;">
<?php
$sub = 'destcerts/';
// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
// SKIP THE "DOT" FILES
if ($filedata->isDot()) continue;
// ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
if ($filedata->getExtension() != 'pdf') continue;
// CREATE LINKS TO THESE FILES
$nom = $filedata->getFilename();
$lnk
= '<img src="destcerts/PDF_icon_100.png" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
. $sub
. '/'
. $nom
. '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
. $nom
. '</a>'
;
// COLLECT THE LINKS HERE
$sss[] = $lnk;
}
// ACCUMULATE THE TABLE ROWS HERE
$trs = NULL;
// COLLECT GROUPS OF FOUR
If(!empty($sss)){
while (!empty($sss))
{
$td1 = array_shift($sss) or NULL;
$td2 = array_shift($sss) or NULL;
$td3 = array_shift($sss) or NULL;
$td4 = array_shift($sss) or NULL;
// USE HEREDOC TO INSERT THESE INTO A TABLE ROW
$tr = <<<EOD
<tr>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
<td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
</tr>
EOD;
// APPEND THE TABLE ROW TO THE OTHER ROWS
$trs .= $tr;
}
}
else{
$msg = "There are no files to display";
$tr = <<<EOD
<tr>
<td style="text-align:center; padding: 10px !important; font-weight: normal;">$msg</td>
</tr>
EOD;
$trs .= $tr;
}
// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table id="pdfDownload" width="97%" align="center" border="1" cellspacing="10" cellpadding="0" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
<th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
<tr>
<th></th>
</tr>
$trs
</table>
EOD;
// SHOW THE WORK PRODUCT
echo $tab;
?>
</div>
directory structure
.
..
destcerts
client1
client2
client3
etc

Related

If variable "A" (value from a file) equals variable "B" (SPECIFIC value from PHP generated table), set a group of new variables

I'm trying to use "greenlight.png" on the image in the corresponding table row only if the row value matches the value from my local file. I'm getting the $live_link value via:
<?php
$live_link_file = 'PIProjectSwitcher/live_link.php';
$f = fopen($live_link_file, 'r');
if ($f) {
$live_link = fread($f, filesize($live_link_file));
fclose($f);
}
?>
My PHP table is generated via:
<table width="700" border="1">
<tbody>
<tr bgcolor="#E0E0E0" style="font-family: Cambria, 'Hoefler Text', 'Liberation Serif', Times, 'Times New Roman', serif; font-size: small; text-align: center;"><td width="660" height="35" align="left" valign="middle">Package Name</td><td width="40" height="35">Live</td><td width="40" height="35">Ready</td><td width="40" height="35">Edit Mode</td></tr>
<?php
if ($insight_packages_list = opendir('Insight_Packages/')) {
while (false !== ($entry = readdir($insight_packages_list))) {
if ($entry != "." && $entry != ".." && $entry != "Blank-Package") {
echo "<tr valign=\"middle\">
<div class=\"floatleft\"><td height=\"25\" style=\"font-family: Cambria, 'Hoefler Text', 'Liberation Serif', Times, 'Times New Roman', serif; font-size: large;\">{$entry}</div></td>
<td height=\"25\" align=\"center\">
<img src=\"images/$live$ready\" class=\"floatcenter\" width=\"25px\" height=\"25px\" id=\"$entry\" name=\"$entry\" /></td>
<td height=\"25\" align=\"center\"><img src=\"images/$live$ready\" class=\"floatcenter\" width=\"25px\" height=\"25px\" /></td>
<td height=\"25\" align=\"center\"> </td>
</tr>";
}
}
closedir($insight_packages_list);
}
?>
</tbody>
</table>
The final result looks like:
The table results are looking at my local directory and one of those directories will always match the value from the live_link.php file.
I'm hoping there's a way to set a group of variables and apply them to that specific matching table row, but I don't know how to do that.
Here is what I have so far:
<?php
if ($live_link == $entry) {
$live = 'greenlight.png';
$ready = 'greylight.png';
$editmode = 'locked.png';
} else {
$live = '';
$ready = '';
$editmode = '';
}
if ($live_link != $entry) {
$live = 'greylight.png';
$ready = 'greenlight.png';
$editmode = 'locked.png';
} else {
$live = '';
$ready = '';
$editmode = '';
}
?>
UPDATE
The error you got may have had to do with the heredoc echo echo <<<EOT
PHP does not want to see anything else on the line after ending EOT; and it must start in column one of the line. Depend on your PHP rev.
PHP Heredoc
The code below is untested. I put a lot of effort to make your code more elegant.
I added a few things from my standard PHP/HTML page template.
The height attribute of <td> is not supported in HTML5. Use CSS instead.
td{height:25px;text-align:center;}
I don't think this is needed: <div class="floatleft">
I added text-align:left to the CSS
<img> width and height do not have a px suffix.
When the are no PHP errors, the next thing to do is check your HTML an CSS with the W3C validators.
W3C HTML Validator
W3C CSS Validator
I also highly recommend these performance checkers:
GT Metrix
Google PageSpeed Insights
This one has every detail of were every millisecond of page load went.
enter link description here
You have an anchor in the last column and the the <td></td> looks blank. IKadded the word Edit.
I do not like GET links. I much prefer POST. This is how I do links in a table cell.
<td><form action="http://firestar/ProjectInsight/create_insight_package_live_link.php" method="post"><input type="hidden" name="entry" value="$file" /><button>Edit</button></form><td>
And the new updated code.
<?php
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=5, max=100');
header('Cache-Control: max-age=1800');
echo <<<EOT
<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8">
<title>PI Project Switcher</title>
<style>
table width:700px;border: 1px solid black;>
td{height:25px;text-align:center;font 400 1em Cambria, 'Hoefler Text', 'Liberation Serif', Times, 'Times New Roman', serif; }
.large{font-size:1.2em;}
.small{font-size:.8em;background-color:#E0E0E0;}
</style>
</head>
<body>
<title>PI Project Switcher</title>
<table><tr class="small"><td style="width:660px; height:35px; text-align:left; valign:middle;">Package Name</td><td style="width:40px height:35px;">Live</td><td style="width:40px height35px;">Ready</td><td style="width:40px height35px;">Edit Mode</td></tr>
EOT;
​$live_link = file_get_contents('./PIProjectSwitcher/live_link.php');
foreach(glob("./Insight_Packages/*.*") as $file){
if ($file == '.' || $file == '..' || $entry == "Blank-Package"){continue;}
$equal = 0;
if ($live_link = $file) {$equal = 1;}
echo <<< EOT
<tr valign="middle">
<td class="large">$file</td>
<td><img src="./images/$live[$equal]" width="25" height="25" id="$file" name="$file" />Edit</td>
<td style="height:25px;text-align:center;"><img src="./images/$ready[$equal]" class="floatcenter" width="25px" height="25" /></td>
<td style="height:25px;text-align:center;"><img src="./images/$editmode[$equal]" class="floatcenter" width="25" height="25" /></td></tr>
</table>
EOT;
}
?>
End of Update
Let's start here:
Replace this:
<?php
$live_link_file = 'PIProjectSwitcher/live_link.php';
$f = fopen($live_link_file, 'r');
if ($f) {
$live_link = fread($f, filesize($live_link_file));
fclose($f);
}
?>
with this:
$live_link = file_get_contents('./PIProjectSwitcher/live_link.php');
And clean up the output code.
foreach(glob("./Insight_Packages/*.*") as $file){
if $file == '.' || $file == '..' || $entry == "Blank-Package"){continue;}
$equal = 0;
if ($live_link = $file) {$equal = 1;}
echo <<< EOT"<tr valign="middle">
<div class="floatleft"><td height="25" style="font-family: Cambria, 'Hoefler Text', 'Liberation Serif', Times, 'Times New Roman', serif; font-size: large;">{$file}</div></td>
<td height="25" align="center">
<img src="images/$live$ready" class="floatcenter" width="25px" height="25px" id="$file" name="$file" /></td>
<td height="25" align="center"><img src="images/$live$ready" class="floatcenter" width="25px" height="25px" /></td>
<td height="25" align="center"> </td>
</tr>";
EOT;
}
Select the .png's based on $equal
Define these arrays before the loop
Where the array outputs [not equal value,equal value]
$live = array('greylight.png','greenlight.png');
$ready = array('greenlight.png','greylight.png');
$editmode = array('locked.png','locked.png');
Add this near the top of the loop
$equal = 0;
if ($live_link = $file) {$equal = 1;}
Then in the loop
echo <tr><td>$live[$equal]</td><td> $ready[$equal]</td></tr>$editmode[$equal]

mysql table values merged into one value instead of multiple

i've ran into an issue with my code which has left me stumped. in the code below, i have created a table for mysql data to be fetched into. however when running my code all of the data (which includes titles, descriptions and prices), seem to be merged into one continuous row which fills the entire page. i'm not sure if this has to do with the size of the data i'm trying to fit into a specific row, or a fault in my code. so any help would be appreciated.
styling used:
table {
border: none;
border-collapse: collapse;
width: 100%;
color: #ffffff;
font-size: 15px;
}
th {
background-color: #a6a6a6;
color: white;
}
table td {
font-size: 5px;
border-left: 1px solid #000;
border-right: 1px solid #000;
}
table td:first-child {
border-left: none;
}
table td:last-child {
border-right: none;
}
</style>
table and php:
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table>
<?php //Beginig of php script for database events
include "Database_connection.php"; //Will make the database connection
$sql = "
SELECT e.eventTitle
, c.catDesc
, e.eventDescription
, v.venueName
, e.eventStartDate
, e.eventEndDate
, e.eventPrice
FROM NEE_venue v
JOIN NEE_events e
ON e.venueID = v.venueID
JOIN NEE_category c
ON c.catID = e.catID
ORDER
BY eventTitle ASC
";
$queryResult = $dbConn->query($sql);
if($queryResult === false) { //will detect if the connection failed, and give an error message
echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
exit;
}
else { //if connection is succsessful, code will retrive the events
while($row = $queryResult->fetch_assoc()){
echo "<tr><td>". $row["eventTitle"] ."</td><td>". $row["catDesc"] ."</td><td>". $row["eventDescription"] ."</td><td>". $row["venueName"] ."</td><td>". $row["eventStartDate"] ."</td><td>". $row["eventEndDate"] ."</td><td>". $row["eventPrice"]
."</td></tr>";
}
echo "</table>";
}
$queryResult->close();
$dbConn->close();
?>
screenshot of the issue:
mysql issue
Get rid of the end table tag in the top section.
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table> <--- That one.
Looks like from both the code and the image that the data gets printed outside of the table, because the end table tag gets added too early.

How do i put a <td> in my <table> with php?

I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!
First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';
There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>
There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.

Create a Table With Merged TH Cells

I am creating an Excel download code, in which I am merging the TH cells and looping through TR to put data under specific merged TH.
But all of data is reflecting on first TH row. Below is code:
$K=0;
while($ROW = $RESULT->fetch_assoc() )
{
if($ROW['type'] == $_GET['separate_id'])
{
$DATE[] = $ROW['end_time'];
$meta_values[] = $ROW['meta_values'];
$body = '
<table style="border:1px solid #000; border-collapse:collapse;">
<thead style="border:1px solid #000; padding:8px;width:150px; height:25px;font-size:15px">
';
foreach($DATE as $DATES)
{
$DATESVIEW = explode('T',$DATES);
$body .=' <th colspan="2">'.$DATESVIEW[0].'</th> ';
}
$body .= '
</thead>
';
foreach($meta_values as $VALS)
{
$META_VALS = explode(' | ',$VALS);
foreach($META_VALS as $VALUEs)
{
$REs = explode(' - ', $VALUEs);
$body .= '
<tbody>
<tr style="border:1px solid #000; padding:8px;width:150px; height:25px;font- size:15px">
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[0].'</td>
<td style="border:1px solid #000; padding:8px; width:150px; height:25px;font-size:15px">'.$REs[1].'</td>
</tr>
</tbody>
';
}
}
}
$K++;
}
$body .= '
</table>
';

Use tr:nth-child(odd) and tr:nth-child(even) selectors only on nested top tables

I want a zebra striped table.
The data is extracted from the database and every instance creates a new table.
I want the zebra-striped on the <table> level. this would mean that every other <table> element gets a different color.
I tried to add a class to my <table class="oddeven"> but this still does the changing on every tr.
Here is the code I use it on:
<?php
global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";
$results = $wpdb->get_results($sql) or die(mysql_error());
echo'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
foreach( $results as $result )
{
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$address = get_post_meta($post_id, 'address', true);
$postal_code = get_post_meta($post_id, 'postal_code', true);
$city = get_post_meta($post_id, 'city', true);
$phone = get_post_meta($post_id, 'phone', true);
echo '
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="oddeven">
<tr>
<td width="75%">
<strong>'.$company_name.'</strong>
</td>
<td rowspan="4"><img class="table_image" src="'.get_bloginfo('template_directory').'/images/arrow.png"></td>
</tr>
<tr>
<td>
'.$address.'
</td>
</tr>
<tr>
<td>
'.$postal_code.' '.$city.'
</td>
</tr>
<tr>
<td>
'.$phone.'
</td>
</tr>
</table>
</td>
</tr>';
}
echo '</table>';
?>
This is the styling:
(while typing this I realise that this is on tr:level)
.oddeven tr:nth-child(odd){
background: #ffffff;
}
.oddeven tr:nth-child(even){
background: #000000;
}
I hope I make myself clear
If you change your foreach loop to a for loop like this:
for($i = 0; $i < count($results); $i++) {
$result = $results[$i];
...
Then you can figure out if the current row is even or odd by testing if $i % 2 == 0. If that evaluates to true, then add an 'even' class; else add an 'odd' class.
If you do not want to propagate the change to child tables, you can also enforce the same color on the children :
.top tr:nth-child(odd) {
background-color: red;
}
.top tr:nth-child(odd) tr {
background-color: red;
}
.top tr:nth-child(even) {
background-color: yellow;
}
.top tr:nth-child(even) tr {
background-color: yellow;
}
I assume that's what your want since you already have stripped rows on your nested tables
Or maybe I got it wrong, you might have to explain what the <table> level is, since your have nested tables.

Categories