제가 구현하고 있는 기능중에 아래와 같이 HTML 에서 array로 데이터를 입력받아야 합니다.
<input type=text name=uName[0] value='홍길동'><input type=text name=uPrice[0] value='1000'>
<input type=text name=uName[1] value='김철수'><input type=text name=uPrice[1] value='2000'>
<input type=text name=uName[2] value='이영희'><input type=text name=uPrice[2] value='3000'>
위의 값이 DB에 들어갈 때는 아래와 같이 INSERT가 되어야 합니다.
INSERT INTO mytable (refId, uName, uPrice) VALUES (1, '홍길동', 1000), (1, '김철수', 2000), (1, '이영희', 3000);
모델에서 아래와 같이 처리하는게 맞는지 고수님들의 지도 부탁드립니다.
$refId = 1;
$this->load->helper('array');
$data = elements(array('refId' => $refId, 'uName', 'uPrice'), $_POST);
$this->db->insert_batch('mytable', $data);
그리고 update는 어떻게 처리하면 되는지도 부탁드립니다.
각각의 레코드의 id 가 1, 2, 3 인 경우 아래와 같이 한건씩 처리해야 하나요 ?
매뉴얼 봐도 잘 모르겠습니다 ~
update mytable set uName = '홍길동', uPrice = 1000 where id = 1 and refId = 1;
update mytable set uName = '김철수', uPrice = 2000 where id = 2 and refId = 1;
update mytable set uName = '이영희', uPrice = 1000 where id = 3 and refId = 1;
제가 알기론 elements()함수가 php에는 없습니다.
uName[] 로 받아서 foreach나 for문 돌면서 insert 구문을 만들면 됩니다.
업데이트의 경우는 조건이 다르기 때문에 한건이 처리하는 것이 맞습니다.