API를 사용하여 내 컨트롤러에 csv 파일을 게시하고 싶습니다. 필립 스터 전의 Codeigniter REST 라이브러리를 사용하고 있습니다. 클라이언트 측에서 내 REST Server에 CSV 가져 오기를 구현하는 방법. 관련 문서를 찾을 수 없어서 물어보고 싶습니다.Codeigniter REST CSV에서 mysql로 가져 오기
답변
쉬운 방법은 다음과 같습니다. 나는 사람들이 무엇을하는지 모르지만 이것을 사용합니다.
이것은 내 csv 리더 라이브러리입니다.이 파일을 라이브러리 폴더에 csvreader.php로 저장하십시오.
<?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;
}
}
?>
그리고 컨트롤러 메소드
function readExcel()
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('Test.csv');//path to csv file
$data['csvData'] = $result;
$this->load->view('view_csv', $data);
}
그리고 이것은보기
<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>
주입니다 : 이것은 단지 서버에있는 파일을 읽습니다. 파일을 업로드해야 할 경우 File Upload Class을 사용하여 파일을 업로드하고 서버의 특정 위치에 저장 한 다음 parse_file
방법으로 파일 위치를 지정하십시오. 모든 것이 잘 작동합니다.
감사합니다. 데이터를 추출하여 모델에 전달하기 만하면됩니다. –
반환 된 배열을 모델 –
에 전달할 수 있습니다. $ enclosure '' '가없는 CSV 필드가 $ enclosure =' '인 경우 오류가 발생합니다. –
- 1. CSV에서 데이터 가져 오기
- 2. Access에서 MySQL로 가져 오기
- 3. csv를 mysql로 가져 오기
- 4. 대용량 CSV에서 MySQL 가져 오기 문제
- 5. MySQL로 json 데이터 가져 오기?
- 6. IMDbPY MySQL로 가져 오기 문제
- 7. PostgreSQL에서 MySQL로 레코드 가져 오기
- 8. 오라클에서 mysql로 데이터 가져 오기
- 9. Codeigniter : 인스턴스 가져 오기
- 10. Drupal 7 노드 CSV에서 가져 오기
- 11. CSV에서 Atlassian OnDemand에 주석 가져 오기
- 12. FileUpload를 사용하여 .CSV에서 SQL로 데이터 가져 오기?
- 13. Excel/CSV에서 SQL Server로 데이터 가져 오기
- 14. OLE CSV에서 유니 코드 문자 가져 오기
- 15. C# 대용량의 데이터를 CSV에서 데이터베이스로 가져 오기
- 16. 가져 오기 CSV에서 구성 가능한 제품 만들기
- 17. CSV에서 MySQL 로의 변환 및 가져 오기
- 18. csv에서 여러 행을 가져 오기 R
- 19. 처음 사용자 암호를 MYSQL로 가져 오기
- 20. pdf 테이블 데이터를 내보내고 mysql로 가져 오기
- 21. phpMyAdmin을 통해 XML 파일을 MySQL로 가져 오기
- 22. MySQL로 텍스트 가져 오기 : 이상한 형식
- 23. Google 스프레드 시트에서 MySQL로 데이터 가져 오기
- 24. XLS를 읽어 PHP와 MySQL로 가져 오기
- 25. 여러 개의 CSV 파일을 mysql로 가져 오기
- 26. PHP로 XLS에서 데이터 편집하기 mySQL로 가져 오기
- 27. 모듈에서 CSS로 가져 오기 CodeIgniter
- 28. REST 호출에서 메소드 가져 오기
- 29. REST 가져 오기/게시 표준
- 30. Codeigniter + HMVC + REST
질문이 명확하지 않습니다. Codeigniter REST는 Codeigniter와 거의 동일하지만 차이점은 메서드와 응답을 호출하는 것입니다. 보통 codeigniter에서 우리는'controller/function'를 호출합니다. 여기서 REST는'resource/{resource_id}/child_resource/{child_resource_id}'이어야합니다. 헤더 파일을'.xls' 파일로 보내면 문제가 없습니다. – KuKu
간단히 말해서 원하는 것을 알려주세요. –
@raheelshan 클라이언트 측 API를 사용하여 내 컨트롤러에 csv 파일을 게시하고 싶습니다. jquery.post 그리고 csv 파일 데이터를 추출하고 mysql로 가져 오기. –