首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在WPLMS - Wordpress上将证书内容存储到数据库中

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

Stack Overflow用户
提问于 2016-04-12 15:36:21
回答 1查看 258关注 0票数 0

当有人生成认证时,我试图将用户名、所选课程和课程教师保存到MySQL数据库中,但我无法识别用于生成这些值的变量。

有人有wplms使用的变量吗?

据我所见,变量在the_content()内部,但我无法扩展它并查看内部。如果有人知道如何检查里面的内容,请提出建议。

代码语言:javascript
运行
复制
<?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());
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-12 20:48:17

好的,经过几次尝试之后,我通过使用the_content函数将get_the_content() ()存储在变量上,并过滤我需要的内容来做到这一点。

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

编辑:我最终不得不修改它,但现在它开始工作了。干杯。按照下面的工作代码操作。

上面的语句无法工作,因为我无法获得所需的数据,所以在wordpress更新the_content()之后,我使用了这段代码来存储它,而不是之前。

代码语言:javascript
运行
复制
ob_start();
the_content();
$content = ob_get_clean();
$content = htmlspecialchars($content);

旧代码:

代码语言:javascript
运行
复制
$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());

?>

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

代码语言:javascript
运行
复制
<?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());

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36577941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档