如何将证书内容存储到WPLMS上的数据库 - Wordpress

时间:2016-04-12 15:36:21

标签: php mysql wordpress wordpress-plugin wordpress-theming

当有人生成认证时,我正在尝试将用户名,课程和课程老师保存到MySQL数据库,但我无法识别用于生成这些值的变量。

是否有人拥有wplms使用的变量?

据我所知,变量位于the_content()内,但我无法扩展它并查看内部。如果有人知道检查内容的方法,请提供建议。

<?php
get_header(vibe_get_header());
if ( have_posts() ) : while ( have_posts() ) : the_post();

$print=get_post_meta($post->ID,'vibe_print',true);


$class=get_post_meta($post->ID,'vibe_custom_class',true);
$css=get_post_meta($post->ID,'vibe_custom_css',true);

$bgimg_id=get_post_meta($post->ID,'vibe_background_image',true);

$bgimg=wp_get_attachment_info( $bgimg_id );

$width = get_post_meta(get_the_ID(),'vibe_certificate_width',true);
$height = get_post_meta(get_the_ID(),'vibe_certificate_height',true);

do_action('wplms_certificate_before_full_content');
?>
<section id="certificate" <?php echo 'style="'.(is_numeric($width)?'width:'.$width.'px;':'').''.(is_numeric($height)?'height:'.$height.'px':'').'"'; ?>>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php do_action('wplms_certificate_before_content'); ?>
                    <div class="extra_buttons">
                        <?php do_action('wplms_certificate_extra_buttons');
                        if(vibe_validate($print)){
                            echo '<a href="#" class="certificate_print"><i class="icon-printer-1"></i></a>';
                            echo '<a href="#" class="certificate_pdf"><i class="icon-file"></i></a>';
                        }
                        ?>
                    </div>
                    <div class="certificate_content <?php echo $class;?>" style="<?php
                            if(isset($bgimg_id) && $bgimg_id && isset($bgimg['src']))
                                echo 'background:url('.$bgimg['src'].');';
                        ?>" <?php 

                        if(is_numeric($width))
                            echo 'data-width="'.$width.'" ';

                        if(is_numeric($height))
                            echo 'data-height="'.$height.'" ';
                        ?>>
                        <?php echo (isset($css)?'<style>'.$css.'</style>':'');?>
                        <?php
                            the_content();
                        ?>
                         <?php do_action('wplms_certificate_after_content'); ?>
                    </div>
                </div>
                <?php

                endwhile;
                endif;
                ?>
            </div>
        </div>
    </div>
</section>
<?php

$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
  }
catch(PDOException $e)
  {
  echo "Connection failed: " . $e->getMessage();
  }

$stmt = $conn->prepare("INSERT INTO certificados (usuario, nome_professor, nome_curso) VALUES ('william', 'professor', 'curso')");
$stmt->execute();

do_action('wplms_certificate_after_full_content');

get_footer(vibe_get_footer());
?>

1 个答案:

答案 0 :(得分:0)

好的,经过多次尝试后,我通过使用get_the_content()函数将the_content()存储在变量上并过滤我需要的内容来实现这一目标。

如果有人看到它并找到更好的解决方案,请提供建议。

编辑:我最后必须修改它,但现在它正在工作。干杯。 请遵循以下工作代码。

上面的陈述没有用,因为我无法得到我需要的数据,因此我使用此代码在wordpress更新之后存储the_content()而不是之前。

ob_start();
the_content();
$content = ob_get_clean();
$content = htmlspecialchars($content);

旧代码:     

$print=get_post_meta($post->ID,'vibe_print',true);


$class=get_post_meta($post->ID,'vibe_custom_class',true);
$css=get_post_meta($post->ID,'vibe_custom_css',true);

$bgimg_id=get_post_meta($post->ID,'vibe_background_image',true);

$bgimg=wp_get_attachment_info( $bgimg_id );

$width = get_post_meta(get_the_ID(),'vibe_certificate_width',true);
$height = get_post_meta(get_the_ID(),'vibe_certificate_height',true);

do_action('wplms_certificate_before_full_content');
?>
<section id="certificate" <?php echo 'style="'.(is_numeric($width)?'width:'.$width.'px;':'').''.(is_numeric($height)?'height:'.$height.'px':'').'"'; ?>>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php do_action('wplms_certificate_before_content'); ?>
                    <div class="extra_buttons">
                        <?php do_action('wplms_certificate_extra_buttons');
                        if(vibe_validate($print)){
                            echo '<a href="#" class="certificate_print"><i class="icon-printer-1"></i></a>';
                            echo '<a href="#" class="certificate_pdf"><i class="icon-file"></i></a>';
                        }
                        ?>
                    </div>
                    <div class="certificate_content <?php echo $class;?>" style="<?php
                            if(isset($bgimg_id) && $bgimg_id && isset($bgimg['src']))
                                echo 'background:url('.$bgimg['src'].');';
                        ?>" <?php 

                        if(is_numeric($width))
                            echo 'data-width="'.$width.'" ';

                        if(is_numeric($height))
                            echo 'data-height="'.$height.'" ';
                        ?>>
                        <?php echo (isset($css)?'<style>'.$css.'</style>':'');?>
                        <?php
                            the_content();
                        ?>
                         <?php do_action('wplms_certificate_after_content'); ?>
                    </div>
                </div>
                <?php

                endwhile;
                endif;
                ?>
            </div>
        </div>
    </div>
</section>
<?php

//Database Conection
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
catch(PDOException $e)
  {
  echo "Connection failed: " . $e->getMessage();
  }


$content = get_the_content();
$content = htmlspecialchars($content);

$contentArray = str_split($content);

$counter = 0;
$trigger = 0;
$valueToDB = array();
$thisValue = 0;

while ($counter < count($contentArray)) {

  if ($trigger == 3) {

    $counter += 11;

    while ($contentArray[$counter] != '&') {

      $valueToDB[$thisValue] .= $contentArray[$counter];

      $counter++;

    }

    $thisValue++;

  }

  if ($contentArray[$counter] == 'w' OR $contentArray[$counter] == 'x' OR $contentArray[$counter] == 'y' OR $contentArray[$counter] == 'z') {
    $trigger++;
  } else {
    $trigger = 0;
  }

  $counter++;

}

// $valueToDB[0] = Student's Name
// $valueToDB[1] = Course Taken
// $valueToDB[2] = Student's Email

$stmt = $conn->prepare("INSERT INTO certificados (aluno, email, nome_curso) VALUES (:nome, :email, :nome_curso)");
$stmt->bindParam(':nome', $valueToDB[0]);
$stmt->bindParam(':nome_curso', $valueToDB[1]);
$stmt->bindParam(':email', $valueToDB[2]);
$stmt->execute();

//

do_action('wplms_certificate_after_full_content');

get_footer(vibe_get_footer());

?>

带有重复验证的新工作代码:

<?php
get_header(vibe_get_header());
if ( have_posts() ) : while ( have_posts() ) : the_post();

$print=get_post_meta($post->ID,'vibe_print',true);


$class=get_post_meta($post->ID,'vibe_custom_class',true);
$css=get_post_meta($post->ID,'vibe_custom_css',true);

$bgimg_id=get_post_meta($post->ID,'vibe_background_image',true);

$bgimg=wp_get_attachment_info( $bgimg_id );

$width = get_post_meta(get_the_ID(),'vibe_certificate_width',true);
$height = get_post_meta(get_the_ID(),'vibe_certificate_height',true);

do_action('wplms_certificate_before_full_content');
?>
<section id="certificate" <?php echo 'style="'.(is_numeric($width)?'width:'.$width.'px;':'').''.(is_numeric($height)?'height:'.$height.'px':'').'"'; ?>>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php do_action('wplms_certificate_before_content'); ?>
                    <div class="extra_buttons">
                        <?php do_action('wplms_certificate_extra_buttons');
                        if(vibe_validate($print)){
                            echo '<a href="#" class="certificate_print"><i class="icon-printer-1"></i></a>';
                            echo '<a href="#" class="certificate_pdf"><i class="icon-file"></i></a>';
                        }
                        ?>
                    </div>
                    <div class="certificate_content <?php echo $class;?>" style="<?php
                            if(isset($bgimg_id) && $bgimg_id && isset($bgimg['src']))
                                echo 'background:url('.$bgimg['src'].');';
                        ?>" <?php 

                        if(is_numeric($width))
                            echo 'data-width="'.$width.'" ';

                        if(is_numeric($height))
                            echo 'data-height="'.$height.'" ';
                        ?>>
                        <?php echo (isset($css)?'<style>'.$css.'</style>':'');?>
                        <?php
                            the_content();
                        ?>
                         <?php do_action('wplms_certificate_after_content'); ?>
                    </div>
                </div>
                <?php

                endwhile;
                endif;
                ?>
            </div>
        </div>
    </div>
</section>
<?php

//--//--//--//--//

// Database Conection
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
catch(PDOException $e)
  {
  echo "Connection failed: " . $e->getMessage();
  }


// the_content() stored in a variable
ob_start();
the_content();
$content = ob_get_clean();
$content = htmlspecialchars($content);

// Split the content in an array to check all the information
$contentArray = str_split($content);

// $counter: iteration through the array
// $trigger: when the class wxyz was found
// $valueToDb: variable to store the values needed from the array
// $thisValue: array number of valueToDb
$counter = 0;
$trigger = 0;
$valueToDb = array();
$thisValue = 0;

// Checks for every letter on the array
while ($counter < count($contentArray)) {

  // If $trigger == 3, the record process will beggin
  if ($trigger == 3) {

    // Adds 11 more letters (the letters after class"wxyz") that the array has
    $counter += 11;

    // Adds the letters into a single word and stops if it finds an & (on the array)
    while ($contentArray[$counter] != '&') {

      $valueToDb[$thisValue] .= $contentArray[$counter];

      $counter++;

    }

    $thisValue++;

  }

  // This part checks if the class is wxyz, if so it starts again to record the word we need
  if ($contentArray[$counter] == 'w' OR $contentArray[$counter] == 'x' OR $contentArray[$counter] == 'y' OR $contentArray[$counter] == 'z') {
    $trigger++;
  } else {
    $trigger = 0;
  }

  $counter++;

}

// Check for duplicates in the DB before inserting the data
$checkDuplicate = $conn->prepare("SELECT email, nome_curso FROM certificados WHERE email = :email");
$checkDuplicate->bindParam(':email', $valueToDb[3]);
$checkDuplicate->execute();
$result = $checkDuplicate->fetchAll();

//That email already has this certification's course
$checkCourse = false;

// For each result, check if the certification was emitted
foreach ($result as $key) {

  if ($valueToDb[1] == $key['nome_curso']) {

    $checkCourse = true;

  }

}

// If the emails does not exist into the database or the email has not emitted the certificate before
if(empty($result) OR !$checkCourse) {

  // Insert into DB the data
  $insertDb = $conn->prepare("INSERT INTO certificados (aluno, email, nome_professor, nome_curso) VALUES (:aluno, :email, :nome_professor, :nome_curso)");
  $insertDb->bindParam(':aluno', $valueToDb[0]);
  $insertDb->bindParam(':nome_curso', $valueToDb[1]);
  $insertDb->bindParam(':nome_professor', $valueToDb[2]);
  $insertDb->bindParam(':email', $valueToDb[3]);
  $insertDb->execute();

}

//--//--//--//--//

do_action('wplms_certificate_after_full_content');

get_footer(vibe_get_footer());

?>