我运行一个PHP代码,该代码收集用户从基于Web的表单(CF7插件)中输入的数据,然后将其导出为XML文件格式。
在此表格中,用户可以填写多种课程。例如:工程,市场营销,医学...
我正在寻找一个在我的代码中添加一个函数的语句,该函数仅在需要时(即,当用户填写了多门课程的信息时)“创建”属性的数据(字段块)而不是预先设置并留空。
这是因为我无法预测用户可以填写多少门课程,而且我不能仅仅预先创建几个字段块“等待”用户输入,而这可能不会发生。我不能空白。需要根据需要创建。
我的实际代码段(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');
当前XML文件格式输出:(如果用户仅填写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>
答案 0 :(得分:0)
您应该在表单中创建一个多选字段
<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');
}