高级写入文件

时间:2011-11-19 01:00:05

标签: php html domdocument

我正在尝试写这个文件 http://corporate.thechamberteam.com/livesearch/questions.htm

当我添加它时,它会一直关闭<html>标记,文本最终会出现在</html>下,并且也会像在该页面上那样使问题变为粗体,并且答案在与该页面上的答案相同的风格。标题是问题,描述就是答案。

而且每个问题都有一个id,所以我想问一个像<a id="Question33">

的问题

顺便说一下,这是一个片段,我包含在另一组支持和识别功能的代码中。

<?php
$myFile = "questions.htm";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST ['title'];
$stringData = $_POST ['description'];

fwrite($fh, $stringData);
fclose($fh);
?>

以下是向数据库添加问题的代码(不是页面,我希望它也将其添加到页面中)

这是页面本身&gt; corporate.thechamberteam.com/livesearch/list.php

<?php
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "search"; // provide your database name
$db_table = "searchengine"; // leave this as is

error_reporting (E_ALL ^ E_NOTICE);

# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>

<html>
<head>
<title>Add a Question and Answer to the Database</title>
<style type="text/css">
.style1 {
    font-family: Calibri;
    font-weight: normal;
    font-size: medium;
}
.style2 {
    color: #808080;
}
.style3 {
    text-align: center;
}
.style4 {
    border-width: 0px;
}
</style>
</head>
<body>

<?php
if (isset($_REQUEST['Submit'])) {
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
$sql = "INSERT INTO $db_table(title,description,url,keywords) values ('".mysql_real_escape_string(stripslashes($_REQUEST['title']))."','".mysql_real_escape_string(stripslashes($_REQUEST['description']))."','".mysql_real_escape_string(stripslashes($_REQUEST['url']))."','".mysql_real_escape_string(stripslashes($_REQUEST['keywords']))."')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your information has been entered into our database<br><br>';
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>

<h1 class="style3"><a href="index.php">
<img src='ChamberLogo.png' class="style4"></a> </h1>
<h1 class="style1">Add A Question to the FAQ
Database</h1>
<hr>

<form method="post" action="">
          Question:<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="title" style="width: 486px">
<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Answer: <br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="description" style="height: 24px; width: 352px">
<br>
          Keywords <span class="style2">(Type Question Again):</span><br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="keywords" style="height: 24px; width: 486px">
<br><br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="submit" name="Submit" value="Submit">
</form>

<?php
}
?>
</body>
</html>

我自己尝试使用一个脚本,只需要标题(问题)和描述(答案)并将其发布到questions.htm页面以及问题和答案页面上其他问题的格式class style1和答案使用class style2。我还希望在问题和答案之间有一个休息<br>

<?php
$myFile = "questions.htm";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST ['title'];
fwrite($fh, $stringData);
fclose($fh);
?>

我想象中的代码示例是

$myFile = "questions.htm";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST .'<br><p class="style1"><a id="Question33">"title"</p></a>'.  
$stringData = $_POST .'<br><p class="style2">"description"</p>';
fwrite($fh, $stringData);
fclose($fh);

但是将其插入到body标签中。有人得到了我说的话吗?

1 个答案:

答案 0 :(得分:0)

通过在附加模式下打开文件,您指示PHP在文件末尾开始写入(即在关闭正文和html标记之后)。要使用特定点的数据更新文件,最简单的方法是将文件内容读入变量,编辑该变量,然后将整个变量写回文件中覆盖旧数据。

因此,假设您希望在结束“body”标记之前直接插入,并假设文件的结尾是结束正文标记并关闭html标记,您可以执行以下操作:

//Read the entire file into the $file_contents variable
$file_contents = file_get_contents("questions.htm");

//Remove the closing body and html tags
$file_contents = substr($file_contents, 0, strlen($file_contents) - strlen("</body></html>"));

//Add in whatever other data you want
$file_contents .= "whatever you want to append goes here";

//Add your closing body and html tags back again
$file_contents .= "</body></html>";

//Overwrite the original questions.htm file with the new contents
$fh = fopen("questions.htm", "w");
fwrite($fh, $file_contents);
fclose($fh);