Here is my code so far now i want to combine this what i can do ?
<table>
<tr>
<th>Imported Files</th>
<th>Report Files</th>
</tr>
<?php
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
foreach(glob($dir) as $file) {
echo "<tr>";
echo "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
echo "<td>". $file."</td>";
}
echo "</tr>";
?>
</table>
I want to print like this
echo "<tr>";
echo "<td>". $file1."</td>";
echo "<td>". $file2."</td>";
}
echo "</tr>";
i.e in the same td what I can do Help me
update:-
I want to print this in to td
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
This one handles files lists of different sizes.
Plus, you can have more than two columns of "file lists" displayed in the same table.
// retrieve data
$dirs = array(glob($dir), glob($dir2));
// display
$max_files = max(array_map('count', $dirs));
$count_dirs = count($dirs);
echo '<table>';
for($i = 0; $i < $max_files; $i++)
{
echo '<tr>';
for($d = 0; $d < $count_dirs; $d++)
{
$strFile = (isset($dirs[$d][$i]) ? $dirs[$d][$i] : '[NO FILE]');
echo '<td>'.$strFile.'</td>';
}
echo '</tr>';
}
echo '</table>';
$line .= "<tr>";
foreach(glob($dir) as $file) {
$line .= "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
$line .= "<td>". $file."</td>";
}
$line .= "</tr>";
echo $line;
If they should be combined and both are of the same size, perhaps this will be a solution (untested)
$dir1_files = glob($dir);
$dir2_files = glob($dir2);
for ($i =0; $i < count($dir1_files); $i++)
{
echo "<tr>";
printf("<td>%s</td>", $dir1_files[$i]);
printf("<td>%s</td>", $dir2_files[$i]);
echo "</tr>";
}
PHP 5.3+ has a MultipleIterator for this, combined with the GlobIterator you can have a solution like this:
$iterator = new MultipleIterator();
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". $current[0] ."</td>";
echo "<td>". $current[1] ."</td>";
echo "</tr>";
}
If you have a different number of files and want to display all of them, you can use the MultipleIterator::MIT_NEED_ANY Flag:
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". (isset($current[0]) ? $current[0] : ' ') ."</td>";
echo "<td>". (isset($current[1]) ? $current[1] : ' ') ."</td>";
echo "</tr>";
}
Related
I am trying to convert a spotify json file to a html table. However this json file is a bit complex. I am making a mistake somewhere, but can't find out what I am doing wrong. What is the proper code?
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents,true);
$results = $decoded->entries[0]->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
foreach($results as $entry){
$artist = $entry->track->artists->name;
$name = $entry->track->name;
$x = $entry + 1;
$color = ($x%2 == 0)? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>". $x ."</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
regards
Your code is a bit confused. I've cleaned it up a little bit in order that it can work, but I think you need to look at it more closely – it pulls very precise pieces of data out of the feed, which might not be safe.
Working code below:
<?php
$spotifylist = "http://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200";
$contents = file_get_contents($spotifylist);
$decoded = json_decode($contents);
$results = $decoded->entries->items;
echo "<table class='chart'> <thead><tr class='row2'><th class='dw'></th><th class='song'>Artiest</th><th class='song'>Titel</th></tr></thead><tbody>";
$counter = 0;
foreach($results as $entry){
++$counter;
$artist = (string)$entry->track->artists[0]->name;
$name = $entry->track->album->name;
$color = ($counter % 2 == 0) ? 'row2': 'row1';
echo "<tr class='$color'>";
echo "<td class='dw'>$counter</td>";
echo "<td class='song'>". $artist ."</td>";
echo "<td class='song'>". $name ."</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
I have a dynamic table created from my PHP script. The information is sourced from a CSV file. I would like everything in the fifth column to be a link to another page. So that would be every array index/key 4 is a link. Unsure on how i would modify the code below to achieve this?
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td
<td>File</td></tr>";
$f = fopen("filelog.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
Add a counter to your loop and look for every 5th iteration.
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
$i = 0;
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
if($i % 5 === 0) {
echo "<td>" . yourlinkstuff . "</td>";
}
}
echo "</tr>\n";
}
I am using modulus because it's unclear from your question whether or not there are more than 4 "cells" per "line". If the foreach($line as $cell) always iterates 4 times, you don't need a counter. You could just tag on the 5th column after that loop, like so:
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<td>" . yourlinkstuff . "</td>";
echo "</tr>\n";
}
You could add a counter and when you reach the 4th element, output a link.
$i = 1;
foreach ($line as $cell) {
if ($i == 4) {
// Create the link.
echo "<td> </td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
$i++;
}
Thank you all for your input, I really appreciate it. I have a table that has five columns, "First, Last, Email, Address, File". Everything in the "File" column is a link to that file. So yes in the array starting at index 0 the "File" data will be at index 4. I didn't want an extra column. This is code that ended up doing the job, thank you!!
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td>
<td>File</td></tr>";
$fp = fopen("filelog.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
$i=1;
foreach ($line as $cell) {
if(($i % 5) == 0) {
echo "<td><a href=" . htmlspecialchars($cell) . ">"
.htmlspecialchars($cell) . "</a></td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
}
}
echo "</tr>\n";
}
fclose($fp);
echo "\n</table></body></html>";
This is my PHP script,
printing the file.
$f = fopen("file_name", "r");
echo "<tbody>";
while (($line = fgetcsv($f)) !== false)
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
echo "\n</table>";
?>
This is the file_name
NODE,CGR,TERMID,VMGW,ET
GMSC01,932,1337,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1338,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1337,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1338,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
Now i want to print the file except first row, print the remaining data from 2nd row to till end.
please suggest how to display the file except first row.
Use a counter variable like $counter = 0; and increment it on every iteration, than use an if condition to skip the first iteration using if($counter != 1)
$counter = 0;
while (($line = fgetcsv($f)) !== false) {
$counter++;
if($counter != 1) {
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
}
Simply read the first line OUT of the while loop :
$f = fopen("file_name", "r");
$line = fgetcsv($f);
echo "<tbody>";
while (($line = fgetcsv($f)) !== false)
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
$f = fopen("file_name", "r");
echo "<tbody>";
$flag=0;
while (($line = fgetcsv($f)) !== false)
{
if($flag==0)
{
//skip the first line
$flag=1;
}
else
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";`
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
echo "\n</table>";
?>
Here's a more efficient version that doesn't stretch the condition over the entirety of the loop contents and uses the continue statement to short-cut the first iteration.
$first = true;
while (($line = fgetcsv($f)) !== false) {
if ($first) {
$first = false;
continue;
}
echo "<tr>\n";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
}
echo "<table>";
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
echo "<tr>\n";
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
I want to show only the first 4 fields in the table. I am decoding a multidimensional array in php. I
Try like
$cnt = 0;
foreach($arr['chart_data'][$x] as $key=>$val) {
if($cnt++ < 4) {
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
} else {
break;
}
}
Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}