首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >添加这个函数需要什么PHP语句?

添加这个函数需要什么PHP语句?
EN

Stack Overflow用户
提问于 2018-09-14 01:36:32
回答 1查看 44关注 0票数 1

我运行了一段PHP代码,它收集用户从基于web的表单(CF7插件)输入的数据,然后将其导出为XML文件格式。

在此表单中,用户可以填写多种类型的课程。例如:工程学、市场营销学、医学……

我正在寻找一个句子来添加一个功能到我的代码中,只有“创建”属性的数据(字段块)时,需要时,即当用户填写超过1课程的信息,而不是有它之前设置和留空。

这是因为我不能预测用户可以填写多少课程,,,并且我不能仅仅预先创建几个字段“等待”用户的输入,这些字段可能不会出现。我不能把它留空。需要根据需要创建。

我的实际代码段(2个字段块):

$xmlSigam = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSigam);  
$xmlSigam->setAttribute("Text", "SQM");  

$xml_dados = $domDocument->appendChild($domDocument->createElement('Dados'));   
$xmlSigam->appendChild($xml_dados);

$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['code']));
$attribute->setAttribute('domainname', 'Code');

$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['course']));
$attribute->setAttribute('domainname', 'Course');

$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['description']));
$attribute->setAttribute('domainname', 'Description');

$xml_dadosmb = $domDocument->appendChild($domDocument->createElement('Dados')); 
$xmlSigam->appendChild($xml_dadosmb);

$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['codemb']));
$attributemb->setAttribute('domainname', 'Code');

$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['coursemb']));
$attributemb->setAttribute('domainname', 'Course');

$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['descriptionmb']));
$attributemb->setAttribute('domainname', 'Description');

当前可扩展标记语言文件格式输出:(以防用户只填写1块字段)

<SigaFiles Text="SQM">
  <Data>
    <attribute domainname="Code">00001</attribute>
    <attribute domainname="Course">ENGINEERING</attribute>
    <attribute domainname="Description">COMPUTER ENGINEERING</attribute>
  </Data>
  <Data>
    <attribute domainname="Code"></attribute>
    <attribute domainname="Course"></attribute>
    <attribute domainname="Description"></attribute>
  </Data>
</SigaFiles>
EN

回答 1

Stack Overflow用户

发布于 2018-09-17 01:48:44

您应该在表单中创建多选字段

<select  name="courses[]" id="countries" multiple="multiple">
  <option value="0001">Engineering</option>
  <option value="0002">History</option>
  <option value="0003">Computer Science</option>
  <option value="0004">Philosphy</option>
</select>

在你的帖子php代码块中:

$xmlSigam = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSigam);  
$xmlSigam->setAttribute("Text", "SQM");  

$xml_dados = $domDocument->appendChild($domDocument->createElement('Dados'));   
$xmlSigam->appendChild($xml_dados);

$course_array = $_POST['courses'];
foreach($courses as $courseCode => $course)
{
  $attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
  $attribute->appendChild($domDocument->createTextNode($courseCode));
  $attribute->setAttribute('domainname', 'Code');

  $attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
  $attribute->appendChild($domDocument->createTextNode($course));
  $attribute->setAttribute('domainname', 'Course');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52318998

复制
相关文章

相似问题

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