한번 쿼리 입력에서 내부적으로 다른 DB컬럼 반복문 문의 드립니다.

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
한번 쿼리 입력에서 내부적으로 다른 DB컬럼 반복문 문의 드립니다.

QA

한번 쿼리 입력에서 내부적으로 다른 DB컬럼 반복문 문의 드립니다.

본문


<!doctype html>
<html lang="ko">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
<?php
 require_once ('connect.php');
    require_once __DIR__ . '/simplexlsx.class.php';
    //$xlsx = new SimpleXLSX(GLOBAL_PATH . 'import/online.xlsx');
 $xlsx = new SimpleXLSX( 'worksheet.xlsx' );
    $cont = 0;

    foreach ($xlsx->rows(1) as $fields) { // 워크시트
        $cont++;
        if ($cont > 1) {
   $type = $fields[0];  // 객관식=1, 주관식=2, 논술형=3
   switch($type) {
      case    "객관식" : $q_type = '1'; break;
      case "주관식" : $q_type = '2'; break;
      case "논술형" : $q_type = '3'; break;
      default  : $q_type = '1'; break;
   }
   
   $question_subject_id = $fields[1]; // 문제 번호
   $question_description = $fields[2]; // 문제 제목
   $answer_isright = $fields[3]; // 정답
   $answer_id = '';
   $answer_question_id =  $question_subject_id; // 문항에 해당하는 문제번호 추출
   $subject_type  = '1';    // 출제 유형 A형, B형
   $question_type = $q_type;   // 객관식=1, 주관식=2, 논술형=3
   $answer_type = $subject_type;  // 문항에 해당하는 문제 유형
   $answer_description1 =  $fields[4]; // 문항①(객관식/객관식5)
   $answer_description2 =  $fields[5]; // 문항②(객관식/객관식5)
   $answer_description3 =  $fields[6]; // 문항③(객관식/객관식5)
   $answer_description4 =  $fields[7]; // 문항④(객관식/객관식5)
   $answer_description5 =  $fields[8]; // 문항⑤(객관식5)
   //$answer_description =  $fields[9]; // 비고(설명)
   //$answer_description =  $fields[10]; // 점수
   //$answer_description =  $fields[11]; // 범주
   //$answer_description =  $fields[12]; // 지문
 
            //문제명 입력 테이블
            $sql = "INSERT INTO eTEST_questions (
   question_id,
   question_subject_id,
   subject_type,
   question_description,
   question_explanation,
   question_type,
   question_difficulty,
   question_enabled,
   question_position,
   question_timer,
   question_fullscreen,
   question_inline_answers,
   question_auto_next) VALUES (
   '$question_id',
   '$question_subject_id',
   '$subject_type',
   '$question_description',
   '$question_explanation',
   '$question_type',
   '$question_difficulty',
   '$question_enabled',
   '$question_position',
   '$question_timer',
   '$question_fullscreen',
   '$question_inline_answers',
   '$question_auto_next')";

   $res = mysqli_query($con,$sql);
        }
 }
 if($res)
        {
            echo "전체 : ".$cont."건"."</br>";
            echo "업로드 된 파일이 성공적으로 처리 되었습니다."."</br>";
        }
        else
        {
            echo "파일 업로드에 실패 하였습니다.";
        }
   
?>

현재 위 코드는 정상 작동합니다. (문제 입력 부분입니다.)

아래의 DB 항목을 위 퀴리 중간에 삽입하여 이것을 4회~5회 반복하려면 방법을 부탁 드립니다.


-- 테이블 구조 `eTEST_answers`
--

CREATE TABLE IF NOT EXISTS `eTEST_answers` (
  `answer_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `answer_question_id` bigint(20) unsigned NOT NULL,
  `answer_type` tinyint(1) NOT NULL DEFAULT '1',
  `answer_description` text COLLATE utf8_unicode_ci NOT NULL,
  `answer_explanation` text COLLATE utf8_unicode_ci,
  `answer_isright` tinyint(1) NOT NULL DEFAULT '0',
  `answer_enabled` tinyint(1) NOT NULL DEFAULT '0',
  `answer_position` bigint(20) unsigned DEFAULT NULL,
  `answer_keyboard_key` smallint(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`answer_id`),
  KEY `p_answer_question_id` (`answer_question_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=57 ;

--
-- 테이블의 덤프 데이터 `eTEST_answers`

 

(53, 15, 'A', '4.2 킷캣', NULL, 1, 1, NULL, NULL),
(54, 15, 'A', '4.0 ICS', NULL, 0, 1, NULL, NULL),
(55, 15, 'A', '2.3 진저브래드', NULL, 0, 1, NULL, NULL),
(56, 15, 'A', '2.2 프로요', NULL, 0, 1, NULL, NULL)

 

아래 추가 할 부분 인데 반복문으로 작성해서 테이블 덤프 테이터 처럼 입력 하는 방법

즉 4회 정도 수행하려면 어떤 반복문을 중간에 삽입하는지 알고 싶습니다.

 

     // 문항명 입력 테이블

     $sql = "INSERT INTO eTEST_answers (
   answer_id,
   answer_question_id,
   answer_type,
   answer_description,
   answer_explanation,
   answer_isright,
   answer_enabled,
   answer_position,
   answer_keyboard_key) VALUES (
   '$answer_id',
   '$answer_question_id',
   '$answer_type',
   '$answer_description',
   '$answer_explanation',
   '$answer_isright',
   '$answer_enabled',
   '$answer_position')";

 

추가 입력 하려는 부분 입니다. 이것을 내부적으로 4회 반복해야되는데..

for( 문으로 4회 반복하려면 어느 부분에 입력 해야 되는지 알려주실수 있나요?

 

작동부분은 문제을 액셀파일 읽어서 문제명 테이블(eTEST_question) 부분을 DB 저장후

문항 테이블(eTEST_answers)에 문항만큼 반복해서 읽어서 DB에 저장 하는 부분 입니다. 

이 질문에 댓글 쓰기 :

답변 1

foreach ($xlsx->rows(1) as $fields) { // 워크시트

이부분이 이상하군요....
느낌상 $xlsx->rows(1) 부분이 첫번째 워크시트 인지 아니면 워크시트의 row 데이터 값인지 알아보셔야 할듯 합니다.
만약 저렇게 해서 한줄만 데이터가 들어갔다면 

$xlsx->rows(1) 이 아니라 $xlsx->rows 의 카운트 만큼 for 문을 돌아야 합니다. 

우선 댓글 달아 주셔서 감사합니다.
위 부분은 워크시트 1번 부분 에 자료을 불어오는 부분이 맞습니다. (기본값은 공백, 0)
현재 정상 작동합니다.

우선 짜집기 해서 구현은 했는데 문의 드립니다.
답변이 없으면 다시 질문으로 올려야 될것 같아요...



<!doctype html>
<html lang="ko">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>

<?php

	require_once ('connect.php');
    require_once __DIR__ . '/simplexlsx.class.php';

	$xlsx = new SimpleXLSX( 'worksheet.xlsx' );
    $cont = 0;
	$answer_num = 5;
	$subject_num = 4;

						
    foreach ($xlsx->rows(1) as $fields) { // 워크시트 
        $cont++;

        if ($cont > 1) {


			$type = $fields[0];		// 객관식=1, 주관식=2, 논술형=3
			switch($type) { 
						case    "객관식"	:	$q_type = '1';	break;
						case	"주관식"	:	$q_type = '2';	break;
						case	"논술형"	:	$q_type = '3';	break;
						default		:	$q_type = '1';	break;
			}
			
			$question_subject_id = $fields[1];	// 문제 번호
			$question_description = $fields[2];	// 문제 제목
			$answer_isright = $fields[3];	// 정답
			$answer_id = '';
			$answer_question_id =  $question_subject_id; // 문항에 해당하는 문제번호 추출

					$subject_type  = $s;				// 출제 유형 A형, B형

			$question_type = $q_type;			// 객관식=1, 주관식=2, 논술형=3
			$answer_type = $subject_type;		// 문항에 해당하는 문제 유형
			$answer_description1 =  $fields[4]; // 문항①(객관식/객관식5)
			$answer_description2 =  $fields[5]; // 문항②(객관식/객관식5)
			$answer_description3 =  $fields[6]; // 문항③(객관식/객관식5)
			$answer_description4 =  $fields[7]; // 문항④(객관식/객관식5)
			$answer_description5 =  $fields[8]; // 문항⑤(객관식5)
			//$answer_description =  $fields[9]; // 비고(설명)
			//$answer_description =  $fields[10]; // 점수
			//$answer_description =  $fields[11]; // 범주
			//$answer_description =  $fields[12]; // 지문



            //CADASTRA fields
            $sql = "INSERT INTO eTEST_questions (
			question_id,
			question_subject_id,
			subject_type,
			question_description,
			question_explanation,
			question_type,
			question_difficulty,
			question_enabled,
			question_position,
			question_timer,
			question_fullscreen,
			question_inline_answers,
			question_auto_next) VALUES (
			'$question_id',
			'$question_subject_id',
			'$subject_type',
			'$question_description',
			'$question_explanation',
			'$question_type',
			'$question_difficulty',
			'$question_enabled',
			'$question_position',
			'$question_timer',
			'$question_fullscreen',
			'$question_inline_answers',
			'$question_auto_next')";
		$res2 = mysqli_query($con,$sql);

	for ($i=1; $i<$answer_num; $i++) {
		if($fields[3] == $i) { $answer_isright = "1"; } else { $answer_isright = "0"; } // 해당 문항이 정답이면 = 1, 아니면 = 0
		switch($i) {
				case    "1" : $answer_description =  $fields[4];  // 문항①(객관식/객관식5)
				break;
				case    "2" : $answer_description =  $fields[5];  // 문항①(객관식/객관식5)
				break;
				case    "3" : $answer_description =  $fields[6];  // 문항①(객관식/객관식5)
				break;
				case    "4" : $answer_description =  $fields[7];  // 문항①(객관식/객관식5)
				break;
				case    "5" : $answer_description =  $fields[8];  // 문항①(객관식/객관식5)

				default : 
				break;
				}

            $sql = "INSERT INTO eTEST_answers (
			answer_id,
			answer_question_id,
			answer_type,
			answer_description,
			answer_explanation,
			answer_isright,
			answer_enabled,
			answer_position) VALUES (
			'$answer_id',
			'$answer_question_id',
			'$answer_type',
			'$answer_description',
			'$answer_explanation',
			'$answer_isright',
			'$answer_enabled',
			'$answer_position')";
			$res1 = mysqli_query($con,$sql);
	}

        }
	}
	if($res2)
        {
            echo "전체 : ".$cont."건"."</br>";
            echo "업로드 된 파일이 성공적으로 처리 되었습니다."."</br>";
        }
        else
        {
            echo "파일 업로드에 실패 하였습니다.";
        }



위 코드중에
$subject_type  = $s; // 출제 유형 A형, B형

값(%s) 한문제 끝나면 자동 2 넘어 가게 하는 방법이 궁금합니다.

엑셀에서 불어오는 컬럼은 유형이 3가지 있습니다. 문제 번호는 같으나 문제명이 조금 틀리게
기제되어서 문항 4가지(객관식) 정답의 위치가 조금 틀리게 기제되어 있습니다.

php 에서 추출 할때 $subject_type 값 랜덤으로 2번 선택이되면 2번에 해당 항목을 추출 하려 합니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 0
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT