메뉴 건너뛰기

프로그램언어

조회 수 6041 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

쿠폰번호 발행 업데이트판. (간단한 클래스화[PHP4 기준] 등...)

 

 

 

DB

CREATE TABLE `coupon_test` (
  `couponNO` varchar(16) NOT NULL default '',
  `ID` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`couponNO`)
) TYPE=MyISAM;

 

 

쿠폰생성 소스

<?
// Mysql 클래스
class TMySql {
    var $dbconn; // DB 연결 리소스
    var $result; // Query 결과 저장 리소스

  

     // 생성자: DB에 연결
    function TMySQL($Host, $DB, $ID, $PW) {
        $this->dbconn=mysql_connect($Host, $ID, $PW) or die("데이터베이스 연결에 실패하였습니다.<br/>".mysql_error());
        mysql_select_db($DB, $this->dbconn) or die("{$DB}를 사용할 수 없습니다.<br/>".mysql_error());
    }

 

    // Query 실행. 결과를 $this->result 에 저장
    function Query($SQL) {
        mysql_real_escape_string($SQL);
        $this->result=mysql_query($SQL, $this->dbconn);
        if (!$this->result) die('INVALID QUERY: '.mysql_error());  
    }

 

    // Query 결과의 갯수 반환
    function Count() {
        return mysql_num_rows($this->result);
    }
}

 


// 쿠폰번호 클래스
class TCoupon {
    var $coupon_len;  // 쿠폰길이
    var $arr_no;      // 숫자배열
    var $arr_alphabet;// 알파벳배열

 

    // 생성자: 쿠폰길이를 받아 멤버에 세팅하고, 숫자와 알파벳배열을 세팅
    function TCoupon($CouponLength=16) {
         $this->coupon_len=$CouponLength;

         // 숫자
         for ($i=Ord('0'); $i<=Ord('9'); $i++) $this->arr_no[]=Chr($i);
         // 알파벳
         for ($i=Ord('A'); $i<=Ord('Z'); $i++) $this->arr_alphabet[]=Chr($i);    
    }

 

    // 쿠폰번호 반환
    function GetCoupon() {
         $result_str="";

         $len_no=count($this->arr_no);
         $len_alphabet=count($this->arr_alphabet);

 

         for ($i=0; $i<$this->coupon_len; $i++){
             // 랜덤을 돌려 0 이면 숫자, 1 이면 알파벳
             if (rand(0,1)==0) $result_str.=$this->arr_no[rand(0,$len_no-1)];
             else                 $result_str.=$this->arr_alphabet[rand(0,$len_alphabet-1)];
         } 
         return $result_str;
    }
}

 

 

 

// DB 객체 생성
$MySQL=new TMySQL('localhost', 'db_bloodguy', 'bloodguy', 'nicehide');

// 길이가 16인 쿠폰번호 객체 생성
$Coupon=new TCoupon(16);


// 쿠폰발행 루프 (10000개의 번호를 생성한다고 가정)
$x=0;
while ($x<10000){
    $CouponNo=$Coupon->GetCoupon();

   

    // 해당 번호가 DB 있는 중복번호인가 체크
    $MySQL->Query("select * from coupon_test where couponNO='{$CouponNo}'");
    // 중복번호가 아니라면 DB 에 넣음
    if ($MySQL->Count==0) {
         $MySQL->Query("insert into coupon_test VALUES ('{$CouponNo}' ,'')");
         //echo $CouponNo."<br>";
         $x++;
    }
    // 중복번호라면 다시
    else continue;
} // while ($x<10000)
?>



List of Articles
번호 제목 날짜 조회 수
200 phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성 file 2017.03.06 22787
199 phpMyAdmin WebMysql 에 CSV 엑셀 파일 업로드 입력하기 ( Excel / Upload / data / 데이터 / 데이타 ) file 2021.03.25 768
198 php로 db 컨트롤 1 2017.03.06 15769
197 PHP로 Excel 파일 만들기... 2014.02.27 30257
196 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
195 PHP에서 CSV 파일 export file 2016.04.22 22335
194 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
193 PHP에서 PDF파일 생성하기 2014.02.27 32777
192 PHP에서 UTF와 EUC-KR 변환 2019.02.19 1554
191 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18431
190 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2694
189 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
188 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2465
187 PHP에서 자바스크립트 값 가져오기 2014.02.27 31635
186 PHP에서 조건문 처리 2015.04.14 22046
185 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1808
184 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3548
183 PHP와 HTML과 자바스크립트의 관계 2021.03.26 311
182 PHP웹 보안 취약점 TOP5(웹해킹) 2023.01.12 293
181 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved