muszę napisać skrypt, który na podstawie wczytanych 2 plików csv pozwoli mi na nich wykonać łączenie po id (opcjonalnie joinem, left joinem lub right joinem).
Stworzyłem sobie takie 2 pliki:
index.php
CODE
<?php
echo<<<formularz
<form action = "laczenie2.php" method = "post" enctype="multipart/form-data">
<input type = "file" name="file" id="file" >
<input type = "file" name="file2" id="file2" >
<br>
join<input type = "radio" name="rodzaj" value="full">
left join<input type = "radio" name="rodzaj" value="left">
right join<input type = "radio" name="rodzaj" value="right">
<br>
<input type = "submit" name = "submit" value = "generuj">
<br><br>
<input type = "textarea" name="wynik" maxlenght="20" size="20">
formularz;
?>
echo<<<formularz
<form action = "laczenie2.php" method = "post" enctype="multipart/form-data">
<input type = "file" name="file" id="file" >
<input type = "file" name="file2" id="file2" >
<br>
join<input type = "radio" name="rodzaj" value="full">
left join<input type = "radio" name="rodzaj" value="left">
right join<input type = "radio" name="rodzaj" value="right">
<br>
<input type = "submit" name = "submit" value = "generuj">
<br><br>
<input type = "textarea" name="wynik" maxlenght="20" size="20">
formularz;
?>
oraz laczenie2.php
Kod
<?php
if ( isset($_POST["submit"]) ) {
if ( isset($_FILES["file"]) && isset($_FILES["file2"])) {
//if there was an error uploading the file
if (($_FILES["file"]["error"] > 0) && ($_FILES["file2"]["error"] > 0)){
echo "Return Code: " . $_FILES["file"]["error"] . " " . $_FILES["file2"]["error"] . "<br />";
}
else {
//Print file details
echo "<table>";
echo "<tr><td>Upload: </td><td>" . $_FILES["file"]["name"] . "</td><td>Upload: </td><td>" . $_FILES["file2"]["name"] . "</td></tr>";
echo "<tr><td>Type: </td><td>" . $_FILES["file"]["type"] . "</td><td>Type: </td><td>" . $_FILES["file2"]["type"] . "</td></tr>";
echo "<tr><td>Size: </td><td>" . ($_FILES["file"]["size"] / 1024) . " Kb</td><td> Size: </td><td>" . ($_FILES["file2"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Temp file: </td><td>" . $_FILES["file"]["tmp_name"] . "</td><td> Temp file: </td><td>" . $_FILES["file2"]["tmp_name"] . "</td></tr>";
echo "</table>";
//if file already exists
if (file_exists("upload/" . $_FILES["file"]["name"]) && file_exists("upload/" . $_FILES["file2"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
echo $_FILES["file2"]["name"] . " already exists. ";
}
else {
//Store file in directory "upload" with the name of "uploaded_file.txt"
$storagename = '/nowe';
move_uploaded_file($_FILES["file"]["tmp_name"], $storagename);
move_uploaded_file($_FILES["file2"]["tmp_name"], $storagename);
echo "Stored in: " . "nowe/" . $_FILES["file"]["name"] . "<br />";
echo "Stored in: " . "nowe/" . $_FILES["file2"]["name"] . "<br />";
}
}
} else {
echo "No file selected <br />";
}
}
if ( $file = fopen($storagename , "r" )) {
echo "Filed opened.<br />";
$firstline = fgets ($file, 4096 );
//Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
$num = strlen($firstline) - strlen(str_replace(";", "", $firstline));
//save the different fields of the firstline in an array called fields
$fields = array();
$fields = explode( ";", $firstline, ($num+1) );
$line = array();
$i = 0;
//CSV: one line is one record and the cells/fields are seperated by ";"
//so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell]
while ( $line[$i] = fgets ($file, 4096) ) {
$dsatz[$i] = array();
//$line[$i] = iconv('CP1250', 'UTF-8//TRANSLIT', $line[$i]);
$dsatz[$i] = explode( ";", $line[$i], ($num+1) );
$i++;
}
echo "<table>";
echo "<tr>";
for ( $k = 0; $k != ($num+1); $k++ ) {
echo "<td>" . $fields[$k] . "</td>";
}
echo "</tr>";
foreach ($dsatz as $key => $number) {
//new table row for every record
echo "<tr>";
foreach ($number as $k => $content) {
//new table cell for every field of the record
echo "<td>" . $content . "</td>";
}
}
echo "</table>";
}
?>
if ( isset($_POST["submit"]) ) {
if ( isset($_FILES["file"]) && isset($_FILES["file2"])) {
//if there was an error uploading the file
if (($_FILES["file"]["error"] > 0) && ($_FILES["file2"]["error"] > 0)){
echo "Return Code: " . $_FILES["file"]["error"] . " " . $_FILES["file2"]["error"] . "<br />";
}
else {
//Print file details
echo "<table>";
echo "<tr><td>Upload: </td><td>" . $_FILES["file"]["name"] . "</td><td>Upload: </td><td>" . $_FILES["file2"]["name"] . "</td></tr>";
echo "<tr><td>Type: </td><td>" . $_FILES["file"]["type"] . "</td><td>Type: </td><td>" . $_FILES["file2"]["type"] . "</td></tr>";
echo "<tr><td>Size: </td><td>" . ($_FILES["file"]["size"] / 1024) . " Kb</td><td> Size: </td><td>" . ($_FILES["file2"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Temp file: </td><td>" . $_FILES["file"]["tmp_name"] . "</td><td> Temp file: </td><td>" . $_FILES["file2"]["tmp_name"] . "</td></tr>";
echo "</table>";
//if file already exists
if (file_exists("upload/" . $_FILES["file"]["name"]) && file_exists("upload/" . $_FILES["file2"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
echo $_FILES["file2"]["name"] . " already exists. ";
}
else {
//Store file in directory "upload" with the name of "uploaded_file.txt"
$storagename = '/nowe';
move_uploaded_file($_FILES["file"]["tmp_name"], $storagename);
move_uploaded_file($_FILES["file2"]["tmp_name"], $storagename);
echo "Stored in: " . "nowe/" . $_FILES["file"]["name"] . "<br />";
echo "Stored in: " . "nowe/" . $_FILES["file2"]["name"] . "<br />";
}
}
} else {
echo "No file selected <br />";
}
}
if ( $file = fopen($storagename , "r" )) {
echo "Filed opened.<br />";
$firstline = fgets ($file, 4096 );
//Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
$num = strlen($firstline) - strlen(str_replace(";", "", $firstline));
//save the different fields of the firstline in an array called fields
$fields = array();
$fields = explode( ";", $firstline, ($num+1) );
$line = array();
$i = 0;
//CSV: one line is one record and the cells/fields are seperated by ";"
//so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell]
while ( $line[$i] = fgets ($file, 4096) ) {
$dsatz[$i] = array();
//$line[$i] = iconv('CP1250', 'UTF-8//TRANSLIT', $line[$i]);
$dsatz[$i] = explode( ";", $line[$i], ($num+1) );
$i++;
}
echo "<table>";
echo "<tr>";
for ( $k = 0; $k != ($num+1); $k++ ) {
echo "<td>" . $fields[$k] . "</td>";
}
echo "</tr>";
foreach ($dsatz as $key => $number) {
//new table row for every record
echo "<tr>";
foreach ($number as $k => $content) {
//new table cell for every field of the record
echo "<td>" . $content . "</td>";
}
}
echo "</table>";
}
?>
Część służąca do wyświetlania pliku jest tylko po to zeby zobaczyć jak on jest wczytywany. No i wyszedł jeszcze mały problem z polskimi znakami. Do łączenia tych csv'ów muszę użyć baze SQLite3 bo w pracy nie mam dostępu do MySQL.
Z góry dziękuję za każdą chęć pomocy z waszej strony
