개발 Q&A

제목 동적변수 질문입니다.
글쓴이 freeseamew 작성시각 2014/07/03 01:19:09
댓글 : 9 추천 : 0 스크랩 : 0 조회수 : 11697   RSS
동적 변수에 관한 질문 드리겠습니다. 
아래와 같은 형태의 변수들을 만들어 사용하는데요 변수가 많아지니 하나하나 만드는게 좀 그래서 동적으로 변수들을 생성해서 작업하고 싶어서 질문 드립니다. 


                  $report_info_a_1 = $this->report_m->read_info_name_form_list('report_info_a_1');
                  $report_info_a_2 = $this->report_m->read_info_name_form_list('report_info_a_2');
                  $report_info_a_3 = $this->report_m->read_info_name_form_list('report_info_a_3');
                  $report_info_a_4 = $this->report_m->read_info_name_form_list('report_info_a_4');
                  $report_info_a_5 = $this->report_m->read_info_name_form_list('report_info_a_5');
                  .
                  .
                  .
 
                  $data = '';

                  $data['report_info_a_1_value'] = $report_info_a_1["info_name_value"];
                  $data['report_info_a_1_seq'] = $report_info_a_1["info_name_seq"];
                  $data['report_info_a_1_ex'] = $report_info_a_1["info_name_ex"];
                  $data['report_info_a_1_attr'] = $report_info_a_1["info_name_attr"];   
                   .
                  .
                  .
 

                  $this->load->view('report/create_report_info_a_v.php', $data);



위의 소스를 아래와 같이 만드니 에러가 나오고 있습니다. 

아직 php가 익숙하지 않아 어떤 부분이 잘 못 되었는 모르니 아시는 분은 도움 부탁드리겠습니다. ^^


                  for ($i = 0; $i < 49; $i++){

                        ${'report_info_a_'.$i} = $this->report_m->read_info_name_form_list('report_info_a_'.$i);

                  }



                  $data = '';

                  for ($i = 0; $i < 49; $i++){
                       
                        $data['report_info_a_'.$i.'_value'] = ${'report_info_a_'.$i}['info_name_value'];
                        $data['report_info_a_'.$i.'_seq'] = ${'report_info_a_'.$i}['info_name_seq'];
                        $data['report_info_a_'.$i.'_ex'] = ${'report_info_a_'.$i}['info_name_ex'];
                        $data['report_info_a_'.$i.'_attr'] = ${'report_info_a_'.$i}['info_name_attr'];

                  }

                  $this->load->view('report/create_report_info_a_v.php', $data);


 
 다음글 홈쇼핑 에 대해 (2)
 이전글 회사에서 진행한 프로젝트로 개발된 사이트의 저작권은 누... (5)

댓글

한대승(불의회상) / 2014/07/03 09:00:14 / 추천 0
아래처럼 해보세요.
 
 $data = array();
 for ($i = 0; $i < 49; $i++){

 $repo_data = $this->report_m->read_info_name_form_list('report_info_a_'.$i);

 $data['report_info_a_'.$i.'_value'] =$repo_data['info_name_value'];
 $data['report_info_a_'.$i.'_seq'] = $repo_data['info_name_seq'];
 $data['report_info_a_'.$i.'_ex'] = $repo_data['info_name_ex'];
 $data['report_info_a_'.$i.'_attr'] = $repo_data['info_name_attr'];
  }

  $this->load->view('report/create_report_info_a_v.php', $data);
게시물 이동 합니다.
/ 2014/07/03 09:50:05 / 추천 0
//freeseamew

이렇게 하면 될거 같은데 안해봐서 장담은 못해요 ^^;

한번 해보세요... 하고 피드백좀 ..

근대 변수라는게 메모리를 쓰는걸로 아는데, 불의회상님 코드처럼 하나의 변수에 담아서 쓰면,

루프를 돌면서 초기화 되니,

메모리 효율상 좋을거 같긴 하네요..;
 
$data = array();

for($i=1;$i<49;$i++){
    $dinamicv = report_info_a_.$i;
    $$dinamicv = $this->report_m->read_info_name_form_list('report_info_a_'.$i);
    
    $data['report_info_a_'.$i.'_value'] = $$dinamicv["info_name_value"];
    $data['report_info_a_'.$i.'_seq'] = $$dinamicv["info_name_seq"];
    $data['report_info_a_'.$i.'_ex'] = $$dinamicv["info_name_ex"];
    $data['report_info_a_'.$i.'_attr'] = $$dinamicv["info_name_attr"]; 
}

freeseamew / 2014/07/03 17:38:02 / 추천 0
위의 소스처럼 해도 여전히 아래 부분에서 에러나 나오고 있습니다. 

다시한번 확인 부탁드릴게요ㅠㅠ

                        $data['report_info_a_'.$i.'_value']= $repo_data['info_name_value'];
                        $data['report_info_a_'.$i.'_seq'] = $repo_data['info_name_seq'];
                        $data['report_info_a_'.$i.'_ex'] = $repo_data['info_name_ex'];
                        $data['report_info_a_'.$i.'_attr'] = $repo_data['info_name_attr'];
freeseamew / 2014/07/03 17:45:56 / 추천 0
두번째 답변의 경우 좀 더 다양한 형태의 에러가 아래 메시지들이 주욱 뜨고 있습니다. ㅠㅠ
 

Severity: Notice

Message: Use of undefined constant report_info_a_ - assumed 'report_info_a_'

Filename: controllers/report.php

Line Number: 534

한대승(불의회상) / 2014/07/03 18:14:02 / 추천 0
freeseamew// 아래 부분의 에러라는건 뭔가요? ^^
darkninja / 2014/07/03 20:58:59 / 추천 0
해결 방법은 아니지만
변수명을 잘 만들면 이해도가 올라가서 문제해결이 좀더 쉽게 되더군요!
로직도 파악이 안되는데
변수명까지 길고 한눈에 들어오지 않으면
사태가 장기간 해결 불가능할수 있습니다.
변수명은 10자 이내로 하이픈은 1-2개정도만 사용하시면^^
배열에서 같은 요소가 반복된다면 배열키로 구분할게 아니라
배열의 차원을 하나더 늘리는 방법으로도 생각해보심이...
freeseamew / 2014/07/03 21:05:19 / 추천 0

아래와 같은 에러메시지가 나고 있구요.


Message: Undefined index: info_name_value

Filename: controllers/report.php

Line Number: 512

A PHP Error was encountered

Severity: Notice

Message: Undefined index: info_name_seq

Filename: controllers/report.php

Line Number: 513

A PHP Error was encountered

Severity: Notice

Message: Undefined index: info_name_ex

Filename: controllers/report.php

Line Number: 514

A PHP Error was encountered

Severity: Notice

Message: Undefined index: info_name_attr

Filename: controllers/report.php

Line Number: 515



이 부분들이 있는 위치가 소스에서 아래 부분입니다. 

                        $data['report_info_a_'.$i.'_seq'] = $repo_data['info_name_seq'];
                        $data['report_info_a_'.$i.'_attr'] = $repo_data['info_name_attr'];


혹시 모델이 필요할지 몰라 모델 소스도 동봉합니다. 아래는 모델 부분입니다. 

    function create_info_content() {
        

        $count = intval($this->input->post('count', true));

        //foreach ($list as $key => $value) {
        for($i = 1; $i < $count; $i++){
            
            $data = array(
                'report_pre_seq' => 2, // !수정예정
                'info_content_num' => $this->input->post('info_content_num_'.$i, true),
                'info_content_string' => $this->input->post('info_content_string_'.$i, true),
                'info_name_pre_seq' => $this->input->post('info_name_seq_'.$i, true)
            );

            $this->db->insert('info_content', $data);

            $data = array();
            
        }

        return true;
    }
 

darkninja / 2014/07/03 21:27:54 / 추천 0
배열에서 에러난 부분의 키값이 들어 있지 않습니다.
리포터모델에서 

read_info_name_form_list() 함수에서
 $repo_data['info_name_attr'] = 'abc'; <= 이렇게 값을 할당하는 부분이 없습니다.

report_m 의 read_info_name_form_list() 함수가 있는지 확인하시고
리턴하는 배열값을 먼저 읽어서 배열에 입력하세요!


 
freeseamew / 2014/07/03 21:42:25 / 추천 0
헉 제 실수입니다. 

for 문 시작을 0이 아닌 1로 하면 되는군요..ㅠㅠ

제가 실수로 시작 변수명을 착각했던 것 같습니다. 

모두에게 죄송 ..ㅠㅠ

결론은 위의 분들이 조언해주신 대로 작성하니 잘 작동합니다. 

모두 감사드립니다.  ^^