我使用PHP脚本创建了一个XML文件,该脚本创建节点并通过循环访问从查询生成的数组来填充它们。现在我有兴趣制作一个后端 PHP 部分,用户可以在其中选择复选框来指定他们想要在 XML 文件中的节点。
这样就可以根据他们的特定需求定制 XML 文件。从理论上讲,我希望复选框能够更改循环的内容,或者只是从XML文件中删除节点。
在最简单的理论形式中...
if(tickbox == checked){
output xml node by looping or just display specified node
} else{
dont... as we dont want this one
}
这是 PHP 脚本
<?php
header('Content-type: text/xml');
mysql_connect('mysql17.000webhost.com','user','pass');
mysql_select_db('a8273293_blogger');
$sql = "Select * from Messages";
$q = mysql_query($sql) or die(mysql_error());
$dom = new DOMDocument();
$data = $dom->createElement('data');
$dom->appendChild($data);
while($r = mysql_fetch_array($q)){
$text = $dom->createElement('text');
$textContent = $dom->createTextNode($r['text']);
$text->appendChild($textContent);
$posted_at = $dom->createElement('posted_at');
$posted_atContent = $dom->createTextNode($r['posted_at']);
$posted_at->appendChild($posted_atContent);
$messages = $dom->createElement('messages');
$messages->appendChild($text);
$messages->appendChild($posted_at);
$data->appendChild($messages);
}
$xmlString = $dom->saveXML();
echo $xmlString;
?>
我可以做到在勾选框时删除或隐藏节点吗?
理论
- 带有复选框的表单
- 每个复选框都有一个名称
- 这些在上面的 PHP 脚本中使用
- if 语句控制输出
您首先需要做的当然是设置一个表单。您必须首先预定义一个列数组,该数组将保存要在表单中使用的所有列名。
// pre defined columns
$columns = array('text' => 'Text', 'posted_at' => 'Posted At', 'messages' => 'Messages');
这也将用于循环表单和筛选结果。
然后,您应该根据定义的数组创建 from:
<form method="POST">
<?php foreach($columns as $column => $label): ?>
<label>
<input type="checkbox" name="selection[]" value="<?php echo $column; ?>" />
<?php echo $label; ?>
</label><br/>
<?php endforeach; ?><br/>
<input type="submit" name="submit" value="Generate" />
</form>
设置表单后,您将处理已选中的复选框。当然,主要思想是,处理那些只检查过的输入。
若要筛选提交的项目,请将它们与默认值相交。反过来,您将拥有已选择的那些:
// filtered array
$selection = array_intersect($_POST['selection'], $columns);
之后,创建节点的过程应该只是一个简单的循环,其中使用 in_array()
包含 if 条件。最后,这个想法是:
<?php
// pre defined columns
$columns = array('text' => 'Text', 'posted_at' => 'Posted At', 'messages' => 'Messages');
if(isset($_POST['submit'], $_POST['selection'])) { // if submitted
// filtered array
$selection = array_intersect($_POST['selection'], $columns);
// connection using mysqli, select query blah blah
$con = mysqli_connect('localhost', 'username', 'password', 'database');
$sql = 'SELECT * FROM messages';
$result = mysqli_query($con, $sql);
$dom = new DOMDocument();
// initialize domdocument and set the parent node
$data = $dom->createElement('data');
$dom->appendChild($data);
while($r = mysqli_fetch_assoc($result)){
// loop the rows, create a node for each row
$node = $textContent = null;
$row = $dom->createElement('row');
foreach($r as $column_name => $val) {
// so loop all the values on each row
if(in_array($column_name, $selection)) {
// so foreach column, check if its selected by the user
// if yes, its inside the selected array, create the node
$node = $dom->createElement($column_name);
$textContent = $dom->createTextNode($val);
$node->appendChild($textContent);
}
// then append it inside the row node
$row->appendChild($node);
}
// append the whole row inside the parent
$data->appendChild($row);
}
header('Content-type: text/xml; charset="utf-8"');
$xmlString = $dom->saveXML();
echo $xmlString;
exit;
}
?>
<form method="POST">
<?php foreach($columns as $column => $label): ?>
<label>
<input type="checkbox" name="selection[]" value="<?php echo $column; ?>" />
<?php echo $label; ?>
</label><br/>
<?php endforeach; ?><br/>
<input type="submit" name="submit" value="Generate" />
</form>