0

I am trying to write to this file here http://corporate.thechamberteam.com/livesearch/questions.htm

when I add it keeps going of off the <html> tags and the text ends up under </html> and also to to make the question bold just like on that page, and the answer Bold as in the same style as the answers on that page as well. Title is the question and description is the answer.

And also each question has an id, so I want a question to be like <a id="Question33">

Btw this is a snippet i am including in another set of code that supports and recognizes the functions.

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

Here is the code that adds a question to the database (Not the page, I want it to add it to the page as well)

and This is the page itself > 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>

I tried using a script myself that will just take the title (question) and description (answer) and post it to the questions.htm page as well with the formatting of the other questions on the question and answers page a question uses the class style1 and an answer uses the class style2. I also want there to be a break <br> between the question and answer such.

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

An example of a code I imagine in my head is

$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);

But inserting it in the body tags. Is anyone getting what I'm saying?

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Sorry, you're not making any sense. Try posting a very small example of the problem. Also &nbps; <-- needs a ; after it and it's not a formatting tool, so don't use to try to line things up -- It only looks right on your monitor and system and nowhere else. – awm Nov 19 '11 at 03:01

1 Answers1

0

By opening the file in append mode you are instructing PHP to begin writing at the end of the file (i.e. after the closing body and html tags). To update the file with data at a specific point the simplest way is to read the contents of the file into a variable, edit that variable, then write the entire variable back into the file overwriting the old data.

So assuming you want to insert directly before the closing "body" tag, and assuming the end of your file is the closing body tag and closing html tag you could do something like this:

//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);
TheOx
  • 2,208
  • 25
  • 28