How to create .csv text to table in PHP - php

Hello any ideas or suggestion how to make conversion my .csv text to table?
Check this link for reference: http://vis.stanford.edu/wrangler/app/

You can browse your .csv with fgetcsv and use foreach to browse the array returned. You simply displays the result.
Here is an exemple :
// Open the file with PHP
$oFile = fopen('PATH_TO_FILE', 'w');
// Get the csv content
$aCsvContent = fgetcsv($oFile);
// HTML Table
echo '<table>';
// Browse your csv line per line
foreach($aCsvContent as $aRow) {
// New table line
echo '<tr>';
// Browse your line cell per cell
foreach($aRow as $sContent) {
// New cell with the content
echo '<td>'.$sContent.'</td>';
}
// End the line
echo '</tr>';
}
// Close the HTML Table
echo '</table>';
// Close you file
fclose($oFile);
I have done this 3 weeks ago ^_^
If you have a problem, tell me. Maybe I can help you !

Related

How to view Excel file second sheet?

Basically, I have create a page to view xlxs file on browser. It works fine but the problem is this php code only can show one sheet.
What I am trying to achieve:
Display Excel spreadsheets in table format on browsers using PHP Excel Reader
Check how many sheets are in the .xls document, get the sheet name and display the amount of buttons accordingly
The buttons must be able to allow the user to traverse between different sheets of one document
Code:
<?php
if($the1['file_type']=='excel'){
require_once "Classes/PHPExcel.php";
$reader= PHPExcel_IOFactory::createReaderForFile($the1['path'].'/'.$the1['file_name']);
$excel_Obj = $reader->load($the1['path'].'/'.$the1['file_name']);
$worksheet=$excel_Obj->getSheet('0');
$lastRow = $worksheet->getHighestRow();
$columncount = $worksheet->getHighestDataColumn();
$columncount_number=PHPExcel_Cell::columnIndexFromString($columncount);
echo "<table border='1'>";
for($row=0;$row<=$lastRow;$row++){
echo "<tr>";
for($col=0;$col<=$columncount_number;$col++){
echo "<td>";
echo $worksheet->getCell(PHPExcel_Cell::stringFromColumnIndex($col).$row)->getValue();
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_query($con,"update file1 set status='read' where file_name='$id'");
}
?>
Use setActiveSheetIndex(sheet number) to specify worksheet 0 (1st worksheet), 1 (2nd worksheet), 2 (third worksheet) and so on. For example
$sheetindex=1; // use the 2nd sheet
$worksheet = $excel_Obj->setActiveSheetIndex($sheetindex);

code to expand excel column width in php while exporting

I am new to PHP and trying to export some DB data to Excel file as below. But in the output I want the column widths to be adjusted automatically to view the entire content. See the code I am using to export data. I am not using any third party libraries for export.
<?php
require_once("db.php");
$contents="email,time_in,v_id,time_out\n";
$user_query = mysql_query('SELECT * FROM table');
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['email'].",";
$contents.=$row['time_in'].",";
$contents.=$row['v_id'].",";
$contents.=$row['time_out']."\n";
}
// remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
header("Content-Disposition: attachment; filename=Report".date('d-m-Y').".xls");
print $contents;
?>
Is there any way to format the output ?

Styling each result of an fgets($file) list differently, is it possible?

G'day, first post so here we go. Very open to suggestions as I'm not sure if it's possible to do what I want with the type of coding I am using. Kinda new/rusty to coding, been a LOOOONG time.
Purpose of current coding:
I have a .txt file with a list of file names (map names to be exact for my situation, each on it's own line) that is constantly being changed and modified as we add/remove maps. I am using a script on a .php page to parse the contents of that file and display the results on a webpage. The script outputs each map name to a variable that is wrapped in an href tag, which results in a link to download that particular map. This continues until all maps have been created in a list, each as a link to download that map.
Problem I am having:
I do not wish some maps/entries to be created as links, and only as text. Is there a way to filter the results of an fgets() and style them different based on value? So like map1-map4 all get turned into download links but map5 does not, then map6 and on continue to be download links.
Current script/code being used:
<?php
$file = fopen("maplists/dodgeball.ini", "r");
while (!feof($file)) {
$dbmapname[] = fgets($file);
}
fclose($file);
foreach ($dbmapname as $dbmap){
echo "<a href='http://www.mydownloadurl.com/fastdl/maps/" . $dbmap . ".bsp.bz2'>$dbmap</a><br />";
}
?>
Coding revised thanks to help below. Here is my current coding to produce what I was looking for:
foreach ($dbmapname as $dbmap){
if(!in_array($dbmap, $stockmaps)){
echo "<a href='http://www.mydownloadurl.com/fastdl/maps/" . $dbmap . ".bsp.bz2'>$dbmap</a><br />";
}
else echo $dbmap."<br />";
}
I am still having a slight issue though regarding the last entry of the array being forced to be a link regardless if it is in the $stockmaps array or not.
There are a ton of ways to implement this. Below I've presented a nice general way where you create an array of maps for which you don't want a link and then loop through and print either link or the plain text depending on whether each map is in the list.
<?php
$file = fopen("maplists/dodgeball.ini", "r");
while (!feof($file)) {
$dbmapname[] = trim(fgets($file));
}
fclose($file);
$nolink = array( $dbmapname[4] ); //fifth map
foreach ($dbmapname as $dbmap){
if(!in_array($dbmap, $nolink)){
echo "<a href='http://www.mydownloadurl.com/fastdl/maps/" . $dbmap . ".bsp.bz2'>$dbmap</a><br />";
}
else echo $dbmap."<br />";
}
}
?>
You can add item to the filter based on whatever criteria you want
//Add item 2 to the list
$nolink[] = $dbmapname[1];
//Add all items after item 20 to the list
$nolink = array_merge($nolink, array_slice($dbmapname, 20));
//Don't link a specific set of maps
$nolink = array('bad_map', 'another_map_not_to_show');

php to display csv file in a paginated format

I am new to PHP and, I have a CSV file which, am displaying in my web page. I want the CSV file to be displayed with the pagination option so that the web page would look nice. This is the code I have so far.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
//constructor takes three parameters
//1. array to be paged
//2. number of results per page (optional parameter. Default is 10)
//3. the current page (optional parameter. Default is 1)
$pagedResults = new Paginated($names, 20, $page);
echo "<ul>";
while($row = $pagedResults->fetchPagedRow()) {
//when $row is false loop terminates
echo "<li>{$row}</li>";
}
echo "</ul>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
However, the CSV file gets displayed with the commas in the screen. Let us consider the below example. Let's assume we have 40 records in my csv file. The contents of the CSV file are as below.
Author1,Name1,Name2,email
1,John,Smith,john.smith#gmail.com
2,Jack,Gibbs,Jack.gibbs#gmail.com
3,Mike,Dell,Mike.dell#gmail.com
and so on.
In my web page, I am getting the output in 2 pages (as I have set my pagination option to display 20 records in each page.
$pagedResults = new Paginated($names, 20, $page);
The output however still contains the comma from the original CSV file. I want my output to be like below.
First Page:
Author1 Name1 Name2 Email
1 John Smith john.smith#gmail.com
and so on.
Second Page:
Author1 Name1 Name2 Email
and so on.
This is because you are pulling the line as a row, but not parsing the line and outputting it cleanly.
The easiest solution is to parse it and then output it separated by div's with each row wrapped in a div. Then make the whole thing pretty with CSS.
Like this:
<?php
$names = file('demo.csv');
$page = $_GET['page'];
/*
Constructor takes three parameters:
1. array to be paged
2. number of results per page (optional parameter. Default is 10)
3. the current page (optional parameter. Default is 1)
*/
$pagedResults = new Paginated($names, 20, $page);
echo "<div class='CSVtable'>";
while($row = $pagedResults->fetchPagedRow()) {
$data = str_getcsv($row);
$dataRow = implode("</div><div class='csvCol'>", $data);
echo "<div class='csvRow'><div class='csvCol'>{$dataRow}</div>";
}
echo "</div>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();

Got a bunch of images, need to put them together to form a big picture (like a puzzle)

I got a bunch of images (225 in total). Example of their names:
4n27e.png
4n28e.png
4n29e.png
4n30e.png
5n12e.png
5n25e.png
5n26e.png
5n27e.png
5n28e.png
I need to form one big picture out of all these images. For example first line of images would be 4n27e (2nd image 4n28e, 3rd image 4n29e and so on). Second line of images would start from 5n12e (2nd image 5n25e and so on). What methods do i need to search for to solve this?
Note: i need to do this only with php and maybe some javascript.
Test case for logic, you can replace the echo with an img tag, and /or use div.
<?php
$images=array();
$images[]="4n27e.png";
$images[]="4n28e.png";
$images[]="4n29e.png";
$images[]="4n30e.png";
$images[]="5n12e.png";
$images[]="5n25e.png";
$images[]="5n26e.png";
$images[]="5n27e.png";
$images[]="5n28e.png";
echo "<table border='1'>";
$oldIndex=0;
$row=1;
foreach($images as $image)
{
if(substr($image,0,1)!=$oldIndex)
{
if($row>1){echo "</tr>";}
echo "<tr>";
$oldIndex=substr($image,0,1);
$row++;
}
echo "<td>$image</td>";
}
echo "</table>";
?>

Categories