티스토리 뷰

반응형

자재관리를 웹으로 하고 있습니다.

자주 사용하는 부분에 대해서 json파일로 저장하고 불러오고,

수정되는 상황에는 다시 기록하는 방식으로 엑세스 시간을 줄여보려고 합니다.

아래의 구문을 하루에 수차례 반복해서 자료를 추출하는 것이 비효율적이어서 나는 아이디어를 낸다.


$sql="select * from mirae8440.steelsource "; 
try{  
   $stmh = $pdo->query($sql);            // 검색조건에 맞는글 stmh
   $counter=0;
   $item_counter=1;
   $steelsource_num=array();
   $steelsource_item=array();
   $steelsource_spec=array();
   $steelsource_take=array();
   $steelitem_arr=array();
   $steelsource_spec_yes=array();
   $spec_arr=array();
   $company_arr=array();
   $title_arr=array();
   $last_item="";
   $last_spec="";
   $pass='0';
   while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {    
    $steelsource_num[$counter]=$row["num"];   
    array_push($steelsource_item, $row["item"]);
    $steelsource_spec[$counter]=$row["spec"];
      $steelsource_take[$counter]=$row["take"];       
  $counter++;
 }   
   } catch (PDOException $Exception) {
    print "오류: ".$Exception->getMessage();
}    

// var_dump($steelsource_item);

array_push($steelsource_item," ");
$steelitem_arr = array_unique($steelsource_item);
sort($steelitem_arr);

$sumcount = count($steelitem_arr);

위의 코드를 steelsource가 수정될때마다 그 내용을 저장함과 동시에 json파일로 만들어 놓고 활용하면 연산을 계속 하지 않을 수 있을 것 같다.

위의 배열을 현재 폴더에 steelsourcejson.json파일로 저장하고 싶다.

이렇게 개선합니다.

<?php
// 여기에 이전 코드 (데이터베이스에서 데이터 추출) 부분을 포함

// 추출한 데이터를 배열로 조합
$data = array(
    "steelsource_num" => $steelsource_num,
    "steelsource_item" => $steelsource_item,
    "steelsource_spec" => $steelsource_spec,
    "steelsource_take" => $steelsource_take,
    "steelitem_arr" => $steelitem_arr
);

// JSON 형식으로 인코딩
$jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

// 파일에 저장
file_put_contents("steelsourcejson.json", $jsonData);
?>


매번 데이터베이스에서 데이터를 추출하는 것 대신에 데이터를 JSON 파일로 저장하여 사용하는 아이디어는 매우 효율적입니다. 특히 데이터가 자주 변경되지 않을 때 이 방법은 서버 부하를 줄이고, 응답 시간을 개선할 수 있습니다.
아래 PHP 코드는 추출한 데이터를 steelsourcejson.json 파일로 현재 폴더에 저장하는 방법을 보여줍니다:




이 스크립트는 다음과 같은 과정을 거칩니다:
데이터베이스에서 추출한 데이터를 $data 배열에 저장합니다.json_encode 함수를 사용하여 이 배열을 JSON 문자열로 변환합니다. JSON_PRETTY_PRINT 옵션은 JSON을 읽기 쉽게 포맷하고, JSON_UNESCAPED_UNICODE는 유니코드 문자를 그대로 유지합니다.file_put_contents 함수를 사용하여 이 JSON 문자열을 steelsourcejson.json 파일에 저장합니다.
이렇게 하면 steelsourcejson.json 파일에 데이터가 저장되며, 이 파일을 읽어서 필요한 정보를 얻을 수 있습니다. 데이터가 변경될 때마다 이 스크립트를 실행하여 JSON 파일을 최신 상태로 유지해야 합니다.

<?php
// 여기에 이전 코드 (데이터베이스에서 데이터 추출) 부분을 포함

// 추출한 데이터를 배열로 조합
$data = array(
    "steelsource_num" => $steelsource_num,
    "steelsource_item" => $steelsource_item,
    "steelsource_spec" => $steelsource_spec,
    "steelsource_take" => $steelsource_take,
    "steelitem_arr" => $steelitem_arr
);

// JSON 형식으로 인코딩
$jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

// 파일에 저장
file_put_contents("steelsourcejson.json", $jsonData);
?>

 

이제 다시 불러오고자 한다면 아래와 같이.

<?php
// JSON 파일 읽기
$jsonData = file_get_contents("steelsourcejson.json");

// JSON을 PHP 배열로 변환
$data = json_decode($jsonData, true);

// 데이터 배열에서 각각의 부분을 추출
$steelsource_num = $data["steelsource_num"];
$steelsource_item = $data["steelsource_item"];
$steelsource_spec = $data["steelsource_spec"];
$steelsource_take = $data["steelsource_take"];
$steelitem_arr = $data["steelitem_arr"];

// 필요한 작업 수행
// 예: var_dump($steelsource_item); 로 배열 내용 확인
?>

 


이 스크립트는 다음 과정을 거칩니다:
file_get_contents 함수를 사용하여 'steelsourcejson.json' 파일의 내용을 읽어들입니다.json_decode 함수를 사용하여 JSON 문자열을 PHP 배열로 변환합니다. 두 번째 파라미터 true는 결과를 연관 배열로 반환하도록 지정합니다.변환된 배열에서 필요한 데이터를 각 변수에 할당합니다.이후에는 이 변수들을 사용하여 필요한 작업을 수행할 수 있습니다.
이 코드를 사용하면, 'steelsourcejson.json' 파일에 저장된 데이터를 쉽게 PHP 배열로 불러와 사용할 수 있습니다.

 

속도를 개선해서 더 빠르게 사용해보고자 합니다.

 

중복되는 엑세스를 저장하고 불러오는 연구 계속할 것입니다.

반응형
댓글