I have created a simple html table within PHP. Here is my code:
<div id="wrapper">
<div class="chart">
<h2>Files Uploaded to Knowledge Base</h2>
<table id="data-table" border="1" cellpadding="10" cellspacing="0">
<tr id=header>
<td>Users</td>
<td id=center>Project Files</td>
<td id=center>Process Files</td>
<td id=center>System Files</td>
<td id=center>Total Files</td>
</tr>
<?php
$di = new RecursiveDirectoryIterator('upload/project/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file1 != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Project WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance1 = $row['uploader'];
$array1[] = $occurance1;
}
}
}
$di = new RecursiveDirectoryIterator('upload/process/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Process WHERE processlocation = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance2 = $row['uploader'];
$array2[] = $occurance2;
}
}
}
$di = new RecursiveDirectoryIterator('upload/system/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 14;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM System WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance3 = $row['uploader'];
$array3[] = $occurance3;
}
}
}
$table_rows = array();
$counter = 0;
$uploader = mysql_query("Select username from members");
while($Load = mysql_fetch_array($uploader)){
$value = $Load['username'];
$tmp = array_count_values($array1);
$cnt = $tmp[$value];
$tmp2 = array_count_values($array2);
$cnt2 = $tmp2[$value];
$tmp3 = array_count_values($array3);
$cnt3 = $tmp3[$value];
$total = $cnt + $cnt2 + $cnt3;
//putting the values into array
$counter++;
$table_rows[$counter] = array();
$table_rows[$counter]['username'] = $value;
$table_rows[$counter]['project'] = $cnt;
$table_rows[$counter]['process'] = $cnt2;
$table_rows[$counter]['system'] = $cnt3;
$table_rows[$counter]['total'] = $total;
}
//function to sort the highest total value
function cmp_rows($a,$b) {
if ($a['total'] == $b['total']) {
return 0;
}
return ($a['total'] > $b['total']) ? -1 : 1;
}
usort($table_rows, 'cmp_rows');
//loop that prints values
foreach($table_rows as $row) {
echo "<tr>";
echo "<td>{$row['username']}</td>";
echo"<td id=center>{$row['project']}</td>";
echo"<td id=center>{$row['process']}</td>";
echo "<td id=center>{$row['system']}</td>";
echo "<td id=center>{$row['total']}</td>";
}
?>
</table>
</div>
</body></html>
The users are populated from a database table. The file figures are populated by reading and counting the amount of files in the directory. The table is sorted by highest first. I would like the first, second and third rows to be a different colour than the rest.
I do not know how to do this. Can someone please guide me in the right direction?
Thanks!
Add a key to the foreach then create a simple if/else statement when you output the table row that says:
> foreach( $table_rows as $key => $row ) {
> if ( $key < 3 ) {
> echo "<tr style="background color here">;
> }
> else {
> echo "<tr>";
> }
> //Rest of Code Here }
Related
Just stating learning PHP, I would like to know is there any way I can declare more than 1 $value in a foreach loop? I am trying to echo out my 8 different type of arrays which i declared ($line0 - $line8). Apologies if my codes are abit messy. I'm still new to PHP.
PHP Code
<?php
$handle = #fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
if ($handle) {
while (!feof($handle)) {
$store = fgets($handle, 4096);
if ($row == 9){
$row = 0;
$count++;
}
if ($row == 0)
{
$line0[] = strval($store);
}
else if($row == 1) {
$line1[] = strval($store);}
else if($row == 2) {
$line2[] = strval($store);}
else if($row == 3) {
$line3[] = strval($store);}
else if($row == 4) {
$line4[] = strval($store);}
$row++;
}
?>
<table>
<tr>
<?php
foreach($line2 as $value1)&&(line3 as $value2){
echo "<td><b>Product ID: $value1</b>"
echo "<td><b>Selection ID: $value2</b>
</td>";
echo '</tr>';
}
?>
</table>
listings.txt
Cedric
93482812
cedric#hotmail.com
Guitar
---------------------------------------------
Wendy
98238432
wendy#hotmail.com
Guitar
---------------------------------------------
No you can't do that, but there's something else you can use. All your arrays have the name keys, and you can get the key with foreach like this:
foreach ($line1 as $key => $value1) {
$value2 = $line2[$key];
echo "<tr>";
echo "<td><b>Product ID: $value1</b></td>";
echo "<td><b>Selection ID: $value2</b></td>";
echo '</tr>';
}
That is getting pretty close to what you want.
I need to access an .xlsb file that does the same function as this code that accesses a .csv file:
<?php
$file1 = __DIR__ . '/download/Trabalhos.csv';
$csv1 = file($file1);
foreach ($csv1 as $row1 => $line1) {
$row1++;
$column1 = str_getcsv($line1, ';');
if ($row1 == 2) {
$column1[6]."<br>";
$valor1 = $column1[6];
}
}
$file2 = __DIR__ . '/download/produtividade do trabalho.csv';
$csv2 = file($file2);
foreach ($csv2 as $row2 => $line2) {
$row2++;
$column2 = str_getcsv($line2, ';');
if ($row2 == 3) {
$column2[9]."<br>";
$valor2 = $column2[9];
}
}
$file3 = __DIR__ . '/download/inatividade do trabalho.csv';
$csv3 = file($file3);
foreach ($csv3 as $row3 => $line3) {
$row3++;
$column3 = str_getcsv($line3, ';');
if ($row3 == 2) {
$column3[0]."<br>";
$valor3 = $column3[0];
}
}
$total = $valor1 * $valor2 * $valor3;
?>
Access the .xlsb file, scroll through the columns and rows, and display the row value on the screen.
Example: "Line 2, Column G(In this case, G = 6)
"A = 0
B = 1
C = 2)"
...
I want it to show me the value of row 2 that is in column G(6).
As in the first if ...
if ($row1 == 2) {
$column1[6]."<br>";
$valor1 = $column1[6];
}
You can use the PHP Excel library from EasyXLS.
$xls = new COM("EasyXLS.ExcelDocument");
$rows = $xls->easy_ReadXLSBActiveSheet_AsList("Trabalhos.xlsb");
for ($row1=0; $row1<$rows->size(); $row1++)
{
$rowLine = $rows->elementAt($row1);
if ($row1 == 2) {
$column1[6]."<br>";
$valor1 = $row->elementAt(6);
}
}
You can find some more code samples about importing data from xlsb file.
For your purpose you can use PHPOffice/PHPExcel
I have a code that pulls data from a .txt file and puts it in an HTML table. The way the code is written it pulls more data than I need. How can I hide columns from the output? This is returning 9 columns where I only need the first 5.
<?php
function toASCII($str) {
return strtr(utf8_decode($str), utf8_decode(
'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'), 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
}
require_once 'meekrodb.php';
DB::$user = 'root';
DB::$password = '';
DB::$dbName = 'test';
$dirArray = array();
$dirpath = 'C:\xampp\htdocs\auditscripts\SCRIPTS\\';
$SERVERNAME=array();
// open this directory
$myDirectory = scandir($dirpath);
// get each entry
foreach ($myDirectory as $value) {
if (is_dir($dirpath . $value)) {
$myDirectory = scandir($dirpath . $value);
if (is_file($dirpath . $value . '\secedit_mergedpolicy.txt')){
$dirArray[] = $dirpath . $value . '\secedit_mergedpolicy.txt';
$servername[]=$value;
}
}
}
// count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");
// sort 'em
sort($dirArray);
// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Server Name</TH><th>VALUE01</th><th>VALUE02</th><th>VALUE03</th><th>VALUE04</th><th>VALUE05</th></TR>\n");
// loop through the array of files and print them all
for ($index = 0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != ".") { // don't list hidden files
$data_tisert = array();
print("<TR><TD>$servername[$index]</td>");
$myString = file_get_contents($dirArray[$index]);
$l1 = explode('<br />', nl2br($myString));
$st = false;
foreach ($l1 as $vx) {
if (!strpos($vx,'=')){
$st=false;
}
if ($st) {
$l1_1 = explode('=', $vx);
print("<TD>$l1_1[1]</td>");
$data_tisert[] = $l1_1[1];
}
/// echo trim(utf8_encode($vx)) == '[Event Audit]' ? 'y' : 'n';
if (utf8_encode(trim($vx)) == utf8_encode('[Event Audit]')) {
$st = true;
}
}
DB::insert('event_audit', array(
'VALUE01' => $data_tisert[0],
'VALUE02' => $data_tisert[1],
'VALUE03' => $data_tisert[2],
'VALUE04' => $data_tisert[3],
'VALUE05' => $data_tisert[4],
));
print("</TR>\n");
}
}
print("</TABLE>\n");
?>
if you only want 5, just put a counter on your array:
Instead of:
foreach ($l1 as $vx) {
Use
for ($ii=0; ( ($ii < 5) && ($ii < count($l1)) ); $ii++){
$vx = $l1[$ii];
I have an array with a list of domains sorted by domain extensions, like:
values[0] = "programming.ca";
values[1] = "Stackoverflow.ca";
values[2] = "question.com";
values[3] = "answers.com";
values[4] = "AASystems.com";
values[5] = "test.net";
values[6] = "hello.net";
values[7] = "apple.nl";
values[8] = "table.org";
values[9] = "demo.org";
How do I print this array, while automatically grouping it in groups with same domain extension and separated by the line break <br />, so the result will look like this?
programming.ca
Stackoverflow.ca
question.com
answers.com
AASystems.com
test.net
hello.net
apple.nl
table.org
demo.org
Try this.
$ext = "";
for($i = 0; $i < count($values); $i++) {
$parts = explode('.', $values[$i]);
$e = $parts[count($parts) - 1];
if(strcmp($parts[count($parts) - 1], $ext) != 0) {
$ext = $e;
echo '<br/>';
}
echo $values[$i].'<br/>';
}
try this if domain names are in order of domain extensions,
$values[0] = "programming.ca";
$values[1] = "Stackoverflow.ca";
$values[2] = "question.com";
$values[3] = "answers.com";
$values[4] = "AASystems.com";
$values[5] = "test.net";
$values[6] = "hello.net";
$values[7] = "apple.nl";
$values[8] = "table.org";
$values[9] = "demo.org";
$prev_ext = "";
foreach($values as $domain_name)
{
$arr_temp = explode(".", $domain_name);
$domain_ext = $arr_temp[1];
if($prev_ext!=$domain_ext)
{
echo '<br/></br/><br/>';
}
echo $domain_name."<br/>";
$prev_ext = $domain_ext;
}
UPDATE : 2
try this if domain names are not in order of their extensions
$values[0] = "programming.ca";
$values[1] = "AASystems.com";
$values[2] = "demo.org";
$values[3] = "answers.com";
$values[4] = "Stackoverflow.ca";
$values[5] = "test.net";
$values[6] = "hello.net";
$values[7] = "apple.nl";
$values[8] = "table.org";
$values[9] = "question.com";
$arr_domains = array();
foreach($values as $domain_name)
{
$arr_temp = explode(".", $domain_name);
$domain_ext = $arr_temp[1];
$arr_domains[$domain_ext][] = $domain_name;
}
foreach($arr_domains as $ext=>$arr_name)
{
echo "<br/><br/><b>".$ext."</b><br/>";
foreach($arr_name as $name)
{
echo $name."<br/>";
}
}
I edited to hopefully make naming clearer. Basically, explode each domain to get name.extension, then store each name in a dictionary of (extension,domainArray) pairs, then foreach entry in the dictionary, grab the domainArray, then foreach name in the domainArray, echo out the domain name . extension, then a line break, then another line break for every dictionary entry.
<?php
$values[0] = "programming.ca";
$values[1] = "Stackoverflow.ca";
$values[2] = "question.com";
$values[3] = "answers.com";
$values[4] = "AASystems.com";
$values[5] = "test.net";
$values[6] = "hello.net";
$values[7] = "apple.nl";
$values[8] = "table.org";
$values[9] = "demo.org";
$domainsList = [];
foreach ($values as $val) {
$valArr = explode(".", $val);
$name = $valArr[0];
$extension = $valArr[1];
if (isset($domainsList[$extension])) {
$domainsList[$extension][] = $name;
} else {
$domainsList[$extension] = [$name];
}
}
foreach ($domainsList as $extension => $domains) {
foreach ($domains as $domain) {
echo $domain . "." . $extension . "<br />";
}
echo "<br />";
}
Try this code.
$domian = array(
"programming.ca",
"Stackoverflow.ca",
"question.com",
"answers.com",
"AASystems.com",
"test.net",
"hello.net",
"apple.nl",
"table.org",
"demo.org");
$result;
foreach ($domian as $value) {
$arr = explode('.', $value);
$result[$arr[1]][] = $value;
}
print_r($result);
I have created a simple html table within PHP. Here is my code:
<div id="wrapper">
<div class="chart">
<h2>Files Uploaded to Knowledge Base</h2>
<table id="data-table" border="1" cellpadding="10" cellspacing="0">
<tr id=header>
<td>Users</td>
<td id=center>Project Files</td>
<td id=center>Process Files</td>
<td id=center>System Files</td>
<td id=center>Total Files</td>
</tr>
<?php
$di = new RecursiveDirectoryIterator('upload/project/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file1 != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Project WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance1 = $row['uploader'];
$array1[] = $occurance1;
}
}
}
$di = new RecursiveDirectoryIterator('upload/process/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Process WHERE processlocation = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance2 = $row['uploader'];
$array2[] = $occurance2;
}
}
}
$di = new RecursiveDirectoryIterator('upload/system/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 14;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM System WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance3 = $row['uploader'];
$array3[] = $occurance3;
}
}
}
$table_rows = array();
$counter = 0;
$uploader = mysql_query("Select username from members");
while($Load = mysql_fetch_array($uploader)){
$value = $Load['username'];
$tmp = array_count_values($array1);
$cnt = $tmp[$value];
$tmp2 = array_count_values($array2);
$cnt2 = $tmp2[$value];
$tmp3 = array_count_values($array3);
$cnt3 = $tmp3[$value];
$total = $cnt + $cnt2 + $cnt3;
//putting the values into array
$counter++;
$table_rows[$counter] = array();
$table_rows[$counter]['username'] = $value;
$table_rows[$counter]['project'] = $cnt;
$table_rows[$counter]['process'] = $cnt2;
$table_rows[$counter]['system'] = $cnt3;
$table_rows[$counter]['total'] = $total;
}
//function to sort the highest total value
function cmp_rows($a,$b) {
if ($a['total'] == $b['total']) {
return 0;
}
return ($a['total'] > $b['total']) ? -1 : 1;
}
usort($table_rows, 'cmp_rows');
//loop that prints values
foreach($table_rows as $row) {
echo "<tr>";
echo "<td>{$row['username']}</td>";
echo"<td id=center>{$row['project']}</td>";
echo"<td id=center>{$row['process']}</td>";
echo "<td id=center>{$row['system']}</td>";
echo "<td id=center>{$row['total']}</td>";
}
?>
</table>
</div>
</body></html>
The users are populated from a database table. The file figures are populated by reading and counting the amount of files in the directory. The table is sorted by highest first. I would also like to display values from the column 'accessDate' from the members table for each member but I'm not sure how to do this. It doesn't work like the other values above..
I do not know how to do this. Can someone please guide me in the right direction?
Thanks!
If you are just trying to get a formatted date into your table, something like this will do it (- see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format for date formatting in MySQL)
...
//MySQL can format the date for you
$uploader = mysql_query(
"Select username, DATE_FORMAT(accessDate,'%m %d, %Y') formatted_date from members");
while($Load = mysql_fetch_array($uploader))
{
...
//putting the values into array
$counter++;
$table_rows[$counter] = array();
$table_rows[$counter]['date'] = $Load['formatted_date'];
$table_rows[$counter]['username'] = $value;
...
}
...
foreach($table_rows as $row)
{
echo "<tr>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['date']}</td>";
...
}
...