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

I when i add it keeps going of the 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">
Can somebody write me a sample of this please?

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

?>

Recommended Answers

All 9 Replies

You can divide the questions.html into 2 files. One for the page and the other for the questions & answers.
Your first file has to be a PHP file. lets call it questions.php

<html>
	<head>
	</head>
	<body>
		<!-- Put your page elemnts -->
		<div>
			<!-- Put your questions and answers here -->
			<?php
				include("qna.html");
			?>
		</div>
	</body>
</html>

qna.html is the file you should keep only the questions & answers. Following is a sample

<p class="style1">
	<a id="Question1">
	<strong>Q:&nbsp; Question 01</strong></a>
</p>
<p class="style2">A:&nbsp; Answer 01</p>

<p class="style1">
	<a id="Question2">
	<strong>Q:&nbsp; Question 02</strong></a>
</p>
<p class="style2">A:&nbsp; Answer 02</p>

Now you have to append content to qna.html

Ok, thanks for the assistance so far, but let me explain more in depth.

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 > http://corporate.the...search/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 (http://corporate.the...h/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?

Ok. Lets see whether I have the correct understanding of your problem.

1. You have a separate file with following query for adding Q&A to the database.

$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']))."')";

2. You have a single file called questions.htm for displaying stored Q&A which has the following format

<html>
	<head>
		<!--Head stuff-->
	</head>
	<body>
		<!--Other body Content-->
		
		<!--Following is the Q&A content-->
		<br><p class="style1"><a id="Question01">Question Title for Question 1</p></a>
		<br><p class="style2">Question Description for Question 1</p>

		<br><p class="style1"><a id="Question02">Question Title for Question 2</p></a>
		<br><p class="style2">Question Description for Question 2</p>

		<br><p class="style1"><a id="Question03">Question Title for Question 3</p></a>
		<br><p class="style2">Question Description for Question 3</p>
	</body>
</html>

3. Now you want to add the Q&A to questions.htm when data gets saved to in the database from the file I mentioned in the 1st point.

If my above understanding is correct, what you have to do is,
1. Divide the questions.htm file into two files
1st file(questions_layout.php) - to hold the layout of the page WITHOUT the questions and answers
2nd file(questions_and_answers.htm) - to hold ONLY the questions and answers

2. Design the layout in questions_layout.php

<html>
	<head>
		<!--Head stuff-->
	</head>
	<body>
		<!--Other body Content-->		
		<?php 
                      include("questions_and_answers.htm");
                ?>
	</body>
</html>

3. Design the Q&A structure in questions_and_answers.htm

<!--Following is the Q&A content-->
<br><p class="style1"><a id="Question01">Question Title for Question 1</p></a>
<br><p class="style2">Question Description for Question 1</p>

<br><p class="style1"><a id="Question02">Question Title for Question 2</p></a>
<br><p class="style2">Question Description for Question 2</p>

<br><p class="style1"><a id="Question03">Question Title for Question 3</p></a> from the browser  
<br><p class="style2">Question Description for Question </p>

Now what happens is when you run questions_layout.php from the browser, questions_and_answers.htm will be automatically gets included between the body tags of the questions_layout.php.

Then you should add new questions and answers to questions_and_answers.htm file as follows, NOT TO THE questions_layout.php :

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

I believe I explained it in the most easiest way to understand.

Ok thank you, i'll try it out and let you know how it goes

Ok i did what you recommended and it worked great.

Ok your recommendation wroked great.

Here is my code:

<?php
	$myFile = "questions_and_answers.htm";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData .= "<br><p style='font-size:18px; font-family: Calibri; font-weight: bold;'><a id='Question35'>".$_POST['title']."</p></a>"; 
	$stringData .= "<p style='font-family: Calibri; font-size:16px;'>".$_POST['description']."</p>";
	fwrite($fh, $stringData);
	fclose($fh);
?>

My last problem is,
I need to use the same form to write to the XML file that manages the live search results.
//I'm appending, not writing, It has to be inserted before </pages> tag, because if it is entered after that the XML code gives an error in the livesearch results.
//And also needs the necessary breaks as they presently are in the the XML file, because the XML cant have tags next to each other, they must be on another line.
//How i imagine the code for writing the data from the form to the XML file in my head:

<?php
	$xmlFile = "links.xml";
	$fh = fopen($xmlFile, 'a') or die("can't open file");
	$data .= "<link>";
	$data .= "<title>".$_POST['title']."</title>"; 
	$data .= "<url>questions_layout.php#".$_POST['id']."</url>";
	$data .= "</link>";
	fwrite($fh, $data);
	fclose($fh);
?>

I'ved looked for solutions and this is what i tried

<?php
$xml = simplexml_load_file('test.xml');
$vote = $xml->addAttribute('link', '');
$vote = $xml->addChild('title', '['title']');
$vote = $xml->addChild("url", "['url']");
$fp = fopen('test.xml', 'w');
fwrite($fp, $xml->asXML());
fclose($fp);
?>

But that just gives me a T_STRING error.

The XML File

<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy® -->
<pages>
<link>
<title>Q: I have local web developers as members, why don’t I just have them build me a website that can do this?</title>Q: 
<url>questions_layout.php#Question1</url>
</link>
<link>
<title>Q: Do I have to change my domain name?</title>Q: 
<url>questions_layout.php#Question2</url>
</link>
<link>
<title>Q: What if I want to make changes to the look of the site over time?</title>Q: 
<url>questions_layout.php#Question3</url>
</link>
<link>
</pages>

Could someone help me on this last part please?

Your XML not well formed. There is an additional <link> tag just before the </pages> tag

That was where another link for a question began, this is a snippet of the whole code itself, that's why.

Member Avatar for diafol

WHy is there data outside the tags?

<title>Q: What if I want to make changes to the look of the site over time?</title>Q:

THose Q: not supposed to be there surely?

WHy is there data outside the tags?

<title>Q: What if I want to make changes to the look of the site over time?</title>Q:

THose Q: not supposed to be there surely?

Not the one at the end, but in the title tags, yeah it's supposed to be there. I removed all those from the end. I'm still trying to figure out how to append to the XML file though.

So far i have:

<?php

    // create the SimpleXMLElement object with an empty <book> element
    $xml = simplexml_load_file('links.xml');

    //Defining Variables
    $title = $_POST['title'];
    $url = $_POST['url'];
    $id = $_POST['id'];

    $link = $xml->addChild('link');
    $link->addChild('title', $title);
    $link->addchild('url', $url);
    $link->addChild('id', $id);

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.