I am trying to format the csv reader to come out in that way:
1. somestuff, somestuff2, somestuff3
unfortunately the numbers are wrapping up at the top of the table instead of in beginning of the line. any help?
<?php
$row = 1;
$handle = fopen("random.csv", "r");
$number = 1;
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo($number . "." . " " . "<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
$numer ++;
}
echo("</table>");
fclose($handle);
?>
You should create a new cell for those numbers:
<?php
$handle = fopen("random.csv", "r");
$number = 1;
echo '
<table>';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo '
<tr>
<td>' . $number . '.</td>';
foreach ($data as $index => $val) {
echo '
<td>' . $val . '</td>';
}
echo '
</tr>';
$number ++;
}
echo '
</table>';
fclose($handle);
Related
I'm getting the data I need from fgetcsv and store them into $data. It contains a table with a header row and lot's of info. Every 7th column is the path to where the file is stored.
I've already searched for what my problem is but I can't seem the find the solution.
My Code yet:
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
I want to add a hyperlink via <a href=?> on every 7th column but I don't know how. How can I do it and is that the right way?
You check every column is a 7th column or divisible by 7 you can just check if the variable is divided by 7 like this.
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
if( $x && !($x % 7) ){
echo '<a href=?>'
}
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
Check if the remainder by 7 of your counter is 0. If it is, then it is a multiple of seven and you can echo your desired string.
if($start % 7 === 0){
echo '<a href=?>'
}
I need to create a table whose first column is populated from subdirectory names inside a directory and rest are from a CSV file. This have to be a dynamic table and table headers have to be added from the code. What's wrong with my code?
I am an absolute beginner. So, please ignore my stupidity.
$dir = 'D:\workspace';
$dirlist = preg_grep('/^([^.])/', scandir($dir));
$row = 1;
if (($handle = fopen("D:\workspace\demo\database.csv", "r")) !== FALSE) {
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
foreach ($dirlist as $rowdirectory)
{
echo '<td>' . $rowdirectory . '</td>';
echo '<td>'.$value.'</td>';
}
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
<?php
#------------------------------------------Function for Reading Directory-------------------------------------------
function readdirectory($dir)
{
$dirlist = preg_grep('/^([^.])/', scandir($dir)); // for all(./../anything that
starts with .)
//$dirlist = preg_grep('/[^.]/', scandir($dir)); //only . & ..
//$dirlist =preg_grep('/^[(^.)]/', scandir($dir));//only files that starts with .
return $dirlist;
}
#------------------------------------------Function for Reading CSV file-------------------------------------------
function readcsvfile($source)
{
$handle = fopen($source, "r");
$filecontent = null;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$filecontent[] = $data;
}
fclose($handle);
return $filecontent;
}
$filecontent= readcsvfile("D:\workspace\demo\database2.csv");
#-------------------Directory Function calling, header array cration and other declaration------------------------------------
$conf_prefix= "../";
$conf_suffix="index.php";
$headerarray= $filecontent[0];
#var_dump($headerarray);
$headersize= count($headerarray);
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
$dir = 'D:\workspace';
$dirlist= readdirectory($dir);
#------------------------------------------Creating 1st row/Header row of Table-------------------------------------------
echo '<tr>';
for($a=0; $a<$headersize;$a++)
{
echo '<td>'.$headerarray[$a].'</td>';
}
echo '</tr>';
#-----------------------------Creating Table elements by comparing both directory arrays and CSV file array-------------------------------------------
foreach ($dirlist as $rowdirectory)
{
foreach ($filecontent as $value) {
$num = count($value);
if ($rowdirectory== $value[0])
{
$link= $conf_prefix. $rowdirectory."/".$conf_suffix;
echo '<tr>';
echo '<td> ' . $rowdirectory . ' </td>';
for( $c=1; $c < $num; $c++) {// loop for csv file
{
echo '<td>'.$value[$c].'</td>';//else print "value"
}
}
echo '</tr>';
}
}
}
?>
I've used fgetcsv in a table and it works fine except the extra cell at the end.
see http://jobbel.nl/csv.php. How can I fix this?
This is my script:
<table>
<tr>
<th>Datum</th>
<th>Tijd</th>
<th>Temperatuur</th>
</tr>
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<tr><td>";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "</td><td>";
}
echo "</td></tr>";
}
fclose($handle);
}
?>
</table>
You're echoing the cells a bit strange.
Change it to just echo the <td>'s when you actually need them:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<td>" . $data[$c] . "</td>";
}
echo "</tr>";
}
There's an alternative solution, using implode(), that's a bit cleaner:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<tr><td>" . implode('</td><td>', $data) . "</td></tr>";
}
This should be it...
<?php
$file = file('templog.csv');
foreach($file as $line){
list($datum,$tijd,$temparatuur)=explode(",", $line);
echo "<td>$Date";
echo "<td>$Time";
echo "<td>$Temparature<tr>";
}
?>
This is csv file :
40405,Vodafone,2
405806,Aircel,1
41303,Etisalat,1
45201,MobilFone,3
51010,Telkomsel,1
63903,Zain,1
63905,yu,2
64005,Airtel,1
I tried using the rsort, ksort, asort, but unable to sort according to the column.
Echoing using the for each loop in php : I am trying to sort the whole data according to the 3rd column in reverse order( descending) ,
$f = fopen("file.csv", "r");
while (($line = fgetcsv($f)) !== false)
{
echo "<tr id='trdata'>";
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell). "</td>";
}
echo "</tr>\n";
}
Thanks.
$f = fopen("file.csv", "r");
$fileData = array();
while (($line = fgetcsv($f)) !== false) {
$fileData[] = $line;
}
echo arrayAsTable($fileData) . "<br />";
usort($fileData, function($a, $b) {
return $b[2] - $a[2];
});
echo arrayAsTable($fileData);
function arrayAsTable($array)
{
$out = "<table>";
foreach ($array as $line) {
$out .= "<tr id='trdata'>";
foreach ($line as $cell) {
$out .= "<td>" . htmlspecialchars($cell) . "</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
return $out;
}
I have a csv file in that i want to search in a specific column for a value, if found the corresponding row should be set in an array.
That i already have working, but if there are multiple rows that have that value, with my code only the last stands in the variable, because it overrides the previous entrys.
How can i make it so that i can echo all rows separatly ? maybe in a multidimensional array ?
Im a beginner in php, help is greatly apreciated.
Thanks
$search = $station;
if (($handle = fopen("CSV-data/airport-frequencies.csv", "r")) !== FALSE) {
$row=0;
$csv_row = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[2] == $search) {
$csv_row = $data;
}
}
fclose($handle);
echo $csv_row[3] . "<br />"; //type
echo $csv_row[4] . "<br />"; //description
echo $csv_row[5] . "<br />"; //frequency
echo "<hr /><br />";
}
You are right, you need an array of arrays. So something like this should work for you:
$search = $station;
if (($handle = fopen("CSV-data/airport-frequencies.csv", "r")) !== FALSE) {
$row=0;
$csv_row = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[2] == $search) {
$csv_row[] = $data;
}
}
fclose($handle);
foreach ($csv_row as $row) {
echo $row[3] . "<br />"; //type
echo $row[4] . "<br />"; //description
echo $row[5] . "<br />"; //frequency
echo "<hr /><br />";
}
}