Csv to Table
Convert at DSV File to a Table
CSV-TABLE/index.php ▾
<?php
include ("inc/header.php");
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
$title = '';
if (isset ($_POST['title'])) {
$title = $_POST['title'];
}
$error = false;
//If a csv file has been selected, save in temporary location and check for file type and size
if ($_FILES['uploadfile']['name'] !== ''){
$imagebase1 = $_FILES['uploadfile']['name'];
$imagename = pathinfo ($imagebase1, PATHINFO_FILENAME);
$extension = strtolower (pathinfo ($imagebase1, PATHINFO_EXTENSION));
if($extension === 'csv'&& $_FILES["uploadfile"]["size"] < 1000000 ) {
$fileTmpName = $_FILES['uploadfile']['tmp_name'];
move_uploaded_file($fileTmpName, 'temp.csv');
}
else {
$error = true;
echo "<h3 style='color: red'>File must be type 'csv' and less than 1 MB)</h3>";
unlink ('temp.csv');
}
}
if ($error === false) {
include ("inc/convert-csv-file-to-array.php");
include ('inc/display-spreadsheet.php');
unlink ('temp.csv');
}
}
else {
//Preload default csv file in case user does not provide one
copy ('inc/FCS.csv', 'temp.csv');?>
<a class = 'return' href = '../../csv-to-table'>← Return</a><br><br>
<h2>Upload CSV File</h2>
<div class = 'full-column'>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for = 'title'>Title: </label>
<input id = 'title' type = 'text' name = 'title' /><br><br>
<label for = 'uploadfile1'>CSV file (Click 'Upload' to use preloaded data):</label>
<input id = 'uploadfile' type="file" name="uploadfile" ><br><br>
<br><br><input class = 'submitbutton' type = 'submit' name ="submit-upload" value = 'Upload'>
</form>
</div>
<?php
}
?>
CSV-TABLE/inc
FCS.csv ▾
LASTNAME,FIRSTNAME,ADDRESS,CITY,STATE,ZIP,EMAIL,CELLPHONE,HOMEPHONE
Cat,Bob,421 Lower Treetrunk Drive,Felinia,OH,45150,bobcat772@catmail.com,(770) 111-2222,
Gigglesnark,Henrietta,3 Cheeseburger Circle,Felinia,OH,45150,Hgiggle@meowmail.com,(770) 111-2222,
Ritzsimmons,Mitzy,15 Hideaway Court,Felinia,OH,45150,Mitzy55@catmail.com,(770) 111-2222,
Shaggyback,Snow,194 North Arctic Avenue,Felinia,OH,45150,Sshaggy@catmail.com,(770) 111-2222,
Pawclaw,Martin and Betty,250 Mousetail Blvd,Felinia,OH,45150,MBPawclaw@catmail.com,(770) 111-2222,(770) 111-2222
Bluewhisker,Josephina,251 Mousetail Blvd,Felinia,OH,45150,Jblue222@meowmail.com,(770) 111-2222,
Calico,Gordon and Ophelia,21449 Longtail Road,Felinia,OH,45150,GOCalico414@catmail.com,(770) 111-2222,
Chaserson,Prissy,8 Feathertoy Circle,Felinia,OH,45150,Prissy1@purrmail.com,(770) 111-2222,
Jones,Cleopatra,314 Squared Circle,Felinia,OH,45150,Cleo30BC@catmail.com,(770) 111-2222,
Softpaw,Blackie,2 Dark Lane,Felinia,OH,45150,BlackieSoftpaw@meowmail.com,(770) 111-2222,
Snowball,Larry and Lucy,9 Feathertoy Circle,Felinia,OH,45150,LarryLucySnow@meowmail.com,(770) 111-2222,(770) 111-2222
Twitchitail,Tom and Ginger,45 Leafy Lane,Felinia,OH,45150,Twitch17@purrmail.com,(770) 111-2222,(770) 111-2222
Feliniov,Boris and Natasha,5 Dark Lane,Felinia,OH,45150,BNFeliniov@catmail.com,(770) 111-2222,
Armstrong,Antonio,252 Mousetail Blvd,Felinia,OH,45150,Aarmstrong@meowmail.com,(770) 111-2222,
Sheddington,Fluffy,195 North Arctic Avenue,Felinia,OH,45150,Fluffy1@purrmail.com,(770) 111-2222,
Dieter,Isley,5 Cheeseburger Circle,Felinia,OH,45150,IsleyD@meowmail.com,(770) 111-2222,
Dieter,Ralph,1 Cheeseburger Circle,Felinia,OH,45150,Ralph@meowmail.com,(770) 111-2222,
Rodgers,Holly,46 Leafy Lane,Felinia,OH,45150,HRod19@catmail.com,(770) 111-2222,
Rodgers,Willow,16 Hideaway Court,Felinia,OH,45150,WRod22@catmail.com,(770) 111-2222,
Rodgers,Scottie,252 Mousetail Blvd,Felinia,OH,45150,Scotrod78@catmail.com,(770) 111-2222,
Patterson,Princess,48 Longtail Court,Felinia,OH,45150,PrincessP@catmail.com,(770) 111-2222,
Patterson,Catzilla,422 Lower Treetrunk Drive,Felinia,OH,45150,CatzillaP@catmail.com,(770) 111-2222,
Rodgers,Squeekie,17 Hideaway Court,Felinia,OH,45150,SqueekRod@meowmail.com,(770) 111-2222,
Rodgers,Tanster,42 Leafy Lane,Felinia,OH,45150,TanROd@meowmail.com,(770) 111-2222,
Armstrong,Mercedes,9 Dark Lane,Felinia,OH,45150,MerArm67@meowmail.com,(770) 111-2222,
Softpaw,Mittens,7 Cheeseburger Circle,Felinia,OH,45150,MittensS@purrmail.com,(770) 111-2222,
Softpaw,Simkin,43 Longtail Court,Felinia,OH,45150,SSoft2525@purrmail.com,(770) 111-2222,
Patterson,Pinkie,255 Mousetail Blvd,Felinia,OH,45150,PinkieP@purrmail.com,(770) 111-2222,
Pantherson,Peter,50 Leafy Lane,Felinia,OH,45150,PeterP@purrmail.com,(770) 111-2222,(770) 111-2222
Kerspeckles,Wilhelmina,12 Feathertoy Circle,Felinia,OH,45150,WKersp671@catmail.com,(770) 111-2222,
Cat,Kitty,52 Leafy Lane,Felinia,OH,45150,KC6722@purrmail.com,(770) 111-2222,
Cat,Bunny,300 Mousetail Blvd,Felinia,OH,45150,BunnC25@catmail.com,(770) 111-2222,
Feliniov,Ivan and Caterina,380 Lower Treetrunk Drive,Felinia,OH,45150,ICFeliniov18@catmail.com,(770) 111-2222,(770) 111-2222
Cat,Baby,45 Longtail Court,Felinia,OH,45150,BC4938@meowmail.com,(770) 111-2222,
Pantherson,Bitsy,4 Dark Lane,Felinia,OH,45150,BitPan16@meowmail.com,(770) 111-2222,
Thagorus,Py,300 Squared Circle,Felinia,OH,45150,PyThag314@meowmail.com,(770) 111-2222,
Rachio,Goldy,307 Squared Circle,Felinia,OH,45150,GRachio618@meowmail.com,(770) 111-2222,
Mewton,Isaac,302 Squared Circle,Felinia,OH,45150,IMew1790@purrmail.com,(770) 111-2222,
convert-csv-file-to-array.php ▾
<?php
$file = fopen('temp.csv',"r");
$keyarray = $spreadsheetarray = array();
$count = 0;
while(! feof($file)){
$array1 = fgetcsv($file);
if ($array1) {
//create keys for associative array
if ($count === 0) {
foreach ($array1 as $item1) {
$item1 = preg_replace('/[^A-Za-z0-9-?!,\s\/]/', '', $item1);
$item1 = str_replace (' ', '', $item1);
$item1 = strtolower($item1);
array_push ($keyarray, $item1);
}
}
else {
//Save values in associative array
$row = array();
foreach ($keyarray as $id => $key) {
if (array_key_exists ($id, $array1)) {
$item1 = $array1[$id] ;
$item1 = preg_replace('/[^A-Za-z0-9-?!@\s\/]/', '', $item1);
$row[$key] = $item1;
}
}
array_push ($spreadsheetarray, $row);
}
}
$count++;
}
fclose($file);
?>
display-spreadsheet.php ▾
<?php
echo "<h2>" . $title . "</h2>";
echo "<table>";
echo "<tr>";
foreach ($keyarray as $key) {
echo "<th>" . ucwords ($key) . "</th>";
}
echo "</tr>";
echo "<br><br>";
foreach ($spreadsheetarray as $spreadsheetrow) {
echo "<tr>";
foreach ($keyarray as $key) {
if (array_key_exists($key, $spreadsheetrow)) {
echo "<td>" . $spreadsheetrow[$key] . "</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
footer.php ▾
</body>
</html> header.php ▾
<!DOCTYPE html>
<html lang="en" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spreadsheet from CSV file</title>
<link rel="stylesheet" href = 'inc/style.css'>
</head>
<body> style.css ▾
body {
color: black;
font-family: 'Arial', sans-serif;
text-align: left;
}
td, th {
padding: 15px;
border-bottom: 3px solid #ddd;
width: 140px;
max-width: 100%;
box-sizing: border-box;
}
h2 {
text-align: center;
}
b {
font-size: 12px;
}
.return {
text-align: center;
display: block;
}
.full-column {
width: 500px;
text-align: left;
margin: auto;
}
@media print {
.directorypage {
page-break-before: always;
}
}