与foreach的phpword库问题?

时间:2014-07-19 20:58:18

标签: php codeigniter for-loop foreach phpword

你好,我想循环我的信息,我的phpword库有问题

    // Education Foreach
$get_education = $this->db->query("SELECT * FROM `member_edu` WHERE Memberid='".$user_id."'");
foreach ($get_education->result() as $edu_row){
    $edu_json    = json_decode($edu_row->edu_details);
    $college     = $edu_json->College_name;
    $University  = $edu_json->University_name;
    $Degree      = $edu_json->Degree_name;
    $Grade       = $edu_json->Grade;
    $Special     = $edu_json->Speciality;
    $EduFrom     = $edu_json->From;
    $EduTo       = $edu_json->To;
    //// Values for loop in .docx file
    $document->cloneRow('rowEdu', $get_education->num_rows());
    $document->setValue('rowEdu#1', $college);
    $document->setValue('rowUniversity#1', $University);
    $document->setValue('rowDegree#1', $Degree);
    $document->setValue('rowGrade#1', $Grade);
    $document->setValue('rowSpeciality#1', $Special);
    $document->setValue('rowEdufrom#1', $EduFrom);
    $document->setValue('rowEduto#1', $EduTo);
}

这是我的代码,当member_edu有一条记录一切正常时,当我添加新的教育时,我得到循环错误。

错误代码:

Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 

'Can not clone row, template variable not found or variable contains markup.' in C:\xampp\htdocs\icareer\application\third_party\phpword\Template.php:186 Stack trace:
#0 C:\xampp\htdocs\icareer\application\views\resume\preview_view.php(59): PhpOffice\PhpWord\Template->cloneRow('rowEdu', 2)
#1 C:\xampp\htdocs\icareer\system\core\Loader.php(833): include('C:\xampp\htdocs...')
#2 C:\xampp\htdocs\icareer\system\core\Loader.php(419): CI_Loader->_ci_load(Array)
#3 C:\xampp\htdocs\icareer\application\controllers\ci_resume.php(722): CI_Loader->view('resume/preview_...', Array)
#4 [internal function]: CI_Resume->preview()
#5 C:\xampp\htdocs\icareer\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array)
#6 C:\xampp\htdocs\icareer\index.php(202): require_once('C:\xampp\htdocs...') #7 {main} thrown in C:\xampp\htdocs\icareer\application\third_party\phpword\Template.php on line 186

我不知道我能做些什么,这是第一次在PHP word库中工作有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您好我自己找到了解决问题的方法感谢您的帮助:)

这是正确的代码。

  // Education Foreach
  $get_education = $this->db->query("SELECT * FROM `member_edu` WHERE Memberid='".$user_id."' ORDER BY `member_edu`.`edu_id` DESC");
  $document->cloneRow('rowEdu', $get_education->num_rows());
  $i=1;
  foreach ($get_education->result() as $edu_row){
      $edu_json    = json_decode($edu_row->edu_details);
      // Values for loop in .docx file
      $document->setValue('rowEdu#'.$i, $edu_json->College_name);
      $document->setValue('rowUniversity#'.$i, $edu_json->University_name);
      $document->setValue('rowDegree#'.$i, $edu_json->Degree_name);
      $document->setValue('rowGrade#'.$i, $edu_json->Grade);
      $document->setValue('rowSpeciality#'.$i, $edu_json->Speciality);
      foreach ($countries as $key => $country) {
        if ($key == $edu_json->Cuntry){
          $document->setValue('rowCountry#'.$i,$country);
        }
      }
      $document->setValue('rowEdufrom#'.$i, $edu_json->From);
      $document->setValue('rowEduto#'.$i, $edu_json->To);
      $i++;
  }