CodeIgniter kullanılarak oluşturulan Uygulamaya csv veya xls import uygulanması gerekir. Bunun için kütüphane var mı? Herhangi bir öneri takdir edildi.CodeIgniter için CSV Alma Kitaplığı
cevap
İşte bunu yapmanın kolay bir yolu. Ben insanların ne yaptığını bilmiyorum ama kullanmak bu
Bu benim csv okuyucu kütüphane
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$this->fields[0]);
$content = array();
$keys = $this->escape_string($keys_values);
$i = 1;
while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if($row != null) { // skip empty lines
$values = explode(',',$row[0]);
if(count($keys) == count($values)){
$arr = array();
$new_values = array();
$new_values = $this->escape_string($values);
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
$arr[$keys[$j]] = $new_values[$j];
}
}
$content[$i]= $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = str_replace('"', '',$row);
}
return $result;
}
}
?>
Ve denetleyici yöntemi
function readExcel()
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('Test.csv');
$data['csvData'] = $result;
$this->load->view('view_csv', $data);
}
Ve bu görüşü
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width = "10%">ID</td>
<td width = "20%">NAME</td>
<td width = "20%">SHORT DESCRIPTION</td>
<td width = "30%">LONG DESCRIPTION</td>
<td width = "10%">STATUS</td>
<td width = "10%">PARENTID</td>
</tr>
<?php foreach($csvData as $field){?>
<tr>
<td><?php echo $field['id']?></td>
<td><?php echo $field['name']?></td>
<td><?php echo $field['shortdesc']?></td>
<td><?php echo $field['longdesc']?></td>
<td><?php echo $field['status']?></td>
<td><?php echo $field['parentid']?></td>
</tr>
<?php }?>
</table>
olduğunu Referans Here
Bu raheel Shan'ın kabul edilen yanıt geliştirilmiş bir versiyonu - Onun cevabını düzenlenebilir ama benim düzenleme reddedildi, ancak önemli bir değişiklik ...
her veri satırı ayrıştırma
, üzerindeexplode()
kullanmak akıllıca değil mi Buradaki virgül, virgül içeren alıntılanmış dizeleri işlemez. sonlarını alt dizelere içine bu dizeleri patlayabilir ve
$values
ekstra dizi elemanlarını verir, bu nedenle bu denetimi başarısız: Yerine
if (count($keys) == count($values)) {
PHP bu işlemek için bir amaca yöntem vardır; str_getcsv(). Orijinal cevap kodunu buna göre güncelledim.
CSV Okuyucu kitaplığı:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class CSVReader {
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys = str_getcsv($this->fields[0]);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0]);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
}
?>
Kontrolör yöntemi:
function readExcel() {
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('Test.csv');
$data['csvData'] = $result;
$this->load->view('view_csv', $data);
}
Ve bu görünümdür:
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="10%">ID</td>
<td width="20%">NAME</td>
<td width="20%">SHORT DESCRIPTION</td>
<td width="30%">LONG DESCRIPTION</td>
<td width="10%">STATUS</td>
<td width="10%">PARENTID</td>
</tr>
<?php foreach ($csvData as $field) { ?>
<tr>
<td><?php echo $field['id'] ?></td>
<td><?php echo $field['name'] ?></td>
<td><?php echo $field['shortdesc'] ?></td>
<td><?php echo $field['longdesc'] ?></td>
<td><?php echo $field['status'] ?></td>
<td><?php echo $field['parentid'] ?></td>
</tr>
<?php } ?>
</table>
Eh bana aldırma, ben sadece modifiye ajmedway en kütüphaneden ayrıştırıcıları dahil etmek istediğinizde sadece veri almak istediğinizde TSV ya da boruların ayrılmış dosyaları olduğunu söyleyebiliriz. Düz eski CSV istiyorsanız, onun iyi.
class CSVReader {
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath, $delimiter = FALSE) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
if ($delimiter==FALSE)
{
$keys = str_getcsv($this->fields[0]);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0]);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
}
else{
$keys = str_getcsv($this->fields[0],$delimiter);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0],$delimiter);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
}
fclose($file);
return $content;
}}?>
Bu, ($ delimiter = FALSE) FALSE için $ sınırlayıcı ayarlayıp durumunun kontrol edilmemesi nedeniyle yanlıştır. Olmalıdır ($ sınırlayıcı == FALSE) – Johnish
whoops! Afedersiniz. Acemi hatası. Teşekkürler @Johnish. Hemen düzelteceğim. –
Bu boş hatları ve fazladan boşluklar ve sekmeler için geliştirilmiş bir versiyonudur ...
sınıf CSVReader {
function csv_to_array($Filepath)
{
//Get csv file content
$csvData = file_get_contents($Filepath);
//Remove empty lines
$csvData = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\r\n", $csvData);
//String convert in array formate and remove double quote(")
$array = array();
$array = $this->escape_string(preg_split('/\r\n|\r|\n/', $csvData));
$new_content_in_array = array();
if($array)
{
//Get array key
$array_keys = array();
$array_keys = array_filter(array_map('trim', explode(';',$array[0])));
//Get array value
$array_values = array();
for ($i=1;$i<count($array);$i++)
{
if($array[$i])
{
$array_values[] = array_filter(array_map('trim', explode(';',$array[$i])));
}
}
//Convert in associative array
if($array_keys && $array_values)
{
$assoc_array = array();
foreach ($array_values as $ky => $val)
{
for($j=0;$j<count($array_keys);$j++){
if($array_keys[$j] != "" && $val[$j] != "" && (count($array_keys) == count($val)))
{
$assoc_array[$array_keys[$j]] = $val[$j];
}
}
$new_content_in_array[] = $assoc_array;
}
}
}
return $new_content_in_array;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", str_replace('"', '',$row));
}
return $result;
}
} >
Çağrı:?
$this->load->library('csvreader');
$import_csv_data = $this->csvreader->csv_to_array($path);
print_r($import_csv_data);
exit;
- 1. zend kullanarak alma CSV
- 2. Yakut - CSV alma - dize
- 3. Verileri CSV Olarak Dışarı Aktar CodeIgniter
- 4. Codeigniter - oturumlar için önbelleğe alma önleme nasıl yapılır
- 5. Bir uygulamada benim android kitaplığı alma kitaplığı olarak tanınmıyor
- 6. Karşıdan Yükle CSV Alma To Get-Date
- 7. Yükseltme Kitaplığı Biçimi; alma std :: string
- 8. CodeIgniter için Yönlendirme (: any)
- 9. StrTk Dize Araç Seti Kitaplığı kullanarak CSV yazma örneği
- 10. CodeIgniter
- 11. CodeIgniter
- 12. CodeIgniter
- 13. CodeIgniter
- 14. CodeIgniter
- 15. CodeIgniter
- 16. codeigniter
- 17. Codeigniter
- 18. Yerel bir makinede Codeigniter yükleme kitaplığı için yükleme yolunu nasıl ayarlıyorsunuz?
- 19. Codeigniter için mongodb destekli bir kullanıcı kimlik doğrulaması kitaplığı var mı?
- 20. Typescript'de JSON için CSV
- 21. .NET için SMS Kitaplığı
- 22. Oyun için Gui kitaplığı
- 23. libSystem kitaplığı için başvurular
- 24. kitaplığı için bulunamadı -lboost_system
- 25. PyPy için GUI kitaplığı
- 26. iOS için Logger Kitaplığı
- 27. Port Codeigniter Uygulaması PyroCMS için
- 28. .NET 4.5.2'de .NET Çekirdek kitaplığı başvuru alma
- 29. DoCmd.TransferText ağ izinlerini kullanarak csv dosyasını MS Access'e alma
- 30. Cassandra .csv alma hatası: toplu iş çok büyük
Thx Raheel, bunu csv'yi veri tabanına da girdiniz mi? –
Evet, sadece $ $ değerini veritabanı tablosuna iletin veya gereksinimlerinize göre bir dizi oluşturun. –