/var/www/hkosl.com/imusiccircle/webadmin/generate_coupon_code.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
    
//not specific setting
    
$page_settings = array(
        
'formid'     => 'Approval'// for permission
        
'section'    => 'Master'// parent/page title
        
'subsection' => 'Musical Instrument Link'// page title
        
'domain'     => 'musical_instrument_link'// table/model name
        
'access'     => 'GNr'// for permission
    
);
    require_once 
"check_login.php";

    if (
$_SESSION["cmsrole"] != "super_admin" && $_SESSION["cmsrole"] != "admin") {
        
header("Location: index.php");
        exit;
    }

    if (
$_GET["generate"] == 1) {
        
//exit;
        
$num 1;
        do {
            
$chars       "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
            
$coupon_code substr(str_shuffle($chars), 06);

            
$sql        "select id from coupon where coupon_code = ?";
            
$parameters = array($coupon_code);
            
$result     bind_pdo($sql$parameters"selectone");

            if (empty(
$result)) {
                
$sql        "insert into coupon (limit_type, discount_type, number, coupon_code, status, one_time) values (?,?,?,?,?,?)";
                
$parameters = array("normal""cash""100"$coupon_code"1""1");
                
bind_pdo($sql$parameters);
                
$num++;
            }
        } while (
$num <= 105);
    } else {
        
//export csv
        // output headers so that the file is downloaded rather than displayed
        
header('Content-Type: text/csv; charset=utf-8');
        
header('Content-Disposition: attachment; filename=coupon_list.csv');
        
header("Pragma: no-cache");
        
header("Expires: 0");

        echo 
"\xEF\xBB\xBF";
// create a file pointer connected to the output stream
        
$output fopen('php://output''w');
// output the column headings

        
fputcsv($output, array('折扣碼'));

// fetch the data
        
$sql    "select coupon_code from coupon where status = 1 and one_time = 1 order by id ASC";
        
$result bind_pdo($sql, array(), "selectall");

        foreach (
$result as $row) {
            
$row_content = array(
                
'="' $row{"coupon_code"} . '"',
            );
            
fputcsv($output$row_content);
        }

        
fclose($output);
    }