2011-11-19 56 views
0

我想,當我將其添加擋<html>標籤去的寫入該文件在這裏 http://corporate.thechamberteam.com/livesearch/questions.htm先進的書面文件

</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); 
?> 

這裏是增加了一個問題,到數據庫的代碼(而不是頁面,我想它,將其添加到頁面以及)

,這是網頁本身> corporate.thechamberteam。 COM /實況搜索/ 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頁面以及問題和答案頁面上的其他問題使用類style1和an答案使用類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); 

但在身體標記插入。有人得到我說的嗎?

+0

對不起,你沒有任何意義。嘗試發佈一個非常小的問題的例子。另外&nbps; < - 需要一個;在它之後,它不是一個格式化工具,所以不要用它來排列一些東西 - 它只能在你的顯示器和系統上以及其他地方看不到。 – awm

回答

0

通過在追加模式下打開文件,您正在指示PHP開始在文件末尾(即在關閉正文和html標籤之後)寫入數據。要用特定點上的數據更新文件,最簡單的方法是將文件內容讀入變量,編輯該變量,然後將整個變量寫回文件中,覆蓋舊數據。

因此,假如你想直接關閉「身體」標籤之前插入,並假設你的文件的末尾的結束標記和一個結束的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);