0

这是我添加条目的表格。我想要做的是当我将表单提交到 php 文件进行处理并将条目添加到 xml 文件中时,我希望它自动增加我在 xml 文件中获得的 ID 标签。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Add an Entry to the Book Database</title>
<style>

table{
    width:600px;
    border:5px dotted black;
    border-radius:10px;
    padding:10px;
    margin:10px;
    }

</style>
</head>


<body>

<form action="addbook.php" method="post">
<table>
<tr>
    <td>Title:</td> 
    <td><input type="text" name="title" /></td>
</tr>
<tr>
    <td>Author:</td> 
    <td><input type="text" name="author" />(<strong>Last Name, First Name</strong>)</td>
</tr>
<tr>
    <td>Publisher:</td> 
    <td><input type="text" name="pub" /></td>
</tr>
<tr>
    <td>Edition:</td> 
    <td><input type="text" name="edition" /></td>
</tr>
<tr>
    <td>Cover:</td> 
    <td><input type="text" name="cover" />(<em>put file in directory "covers", this entry should be formatted "covers/*****.jpg" <strong>with the quotation marks</strong></em>)</td>
</tr>
<tr>
    <td>Genre:</td> 
    <td><input type="text" name="genre" />(<em>if non-fiction, format as "Non-Fiction, *****" where **** is type of non-fiction.  Self-Help, Biographical, Philosophical, etc</em>)</td>
</tr>
<tr>
    <td>Have I Read?:</td> 
    <td><input type="text" name="read" />(<em>"Yes", "No", "Partial"</em>)
</td>
</tr>
</table>
<input type="submit" value="Add Book" />



</form>

</body>

</html>

这是php表单处理程序

<?php

$title = $_POST["title"];
$author = $_POST["author"];
$pub = $_POST["pub"];
$edition = $_POST["edition"];
$cover = $_POST["cover"];
$genre = $_POST["genre"];
$read = $_POST["read"];


$xml = simplexml_load_file("books.xml");
$addbook = $xml->addChild("book");
$addbook->addChild("title", $title);
$addbook->addChild("author", $author);
$addbook->addChild("pub", $pub);
$addbook->addChild("edition", $edition);
$addbook->addChild("cover", $cover);
$addbook->addChild("genre", $genre);
$addbook->addChild("read", $read);

$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save("books.xml"); 

header("Location:add_book_form.html");

?>

最后,这是 xml 文件的示例

<book>
    <title>The Alphabet of Manliness</title>
    <author>Maddox</author>
    <pub>Citadel Press</pub>
    <edition>First, 2006</edition>
    <cover>"covers/alphabetmanliness.jpg"</cover>
    <genre>Humor</genre>
    <read>Yes</read>
    <id>0016</id>
</book>

<book>
    <title>Atheist Manifesto</title>
    <author>Onfray, Michel</author>
    <pub>Arcade Publishing</pub>
    <edition>First, 2007</edition>
    <cover>"covers/atheistmanifesto.jpg"</cover>
    <genre>Non-Fiction, Philosophy</genre>
    <read>Partial</read>
    <id>0017</id>
</book>

在添加标题、作者等方面一切正常。我只需要一种方法来检查最后一个 id,添加一个,并用它标记新条目。使用 simplexml 可以吗?

4

1 回答 1

0

对的,这是可能的:

$ids = $xml->xpath("//book/id"); // select all ids
$newid = max(array_map("intval", $ids)) + 1; // change objects to `int`, get `max()`, + 1

看到它工作:http ://codepad.viper-7.com/qi19Ef

于 2013-11-10T02:59:51.880 回答