Jump to content

Output HTML files


Stevenn

Recommended Posts

I have the two files below and want to separate php and html. How do I output forum.html with forum_post.html in it? I want to post as many forum_post.html as mysql_num_rows() says.

 

forum.html
======
<div>
<form method="post">
<textarea name="text"></textarea>
<input type="submit" />
</form>
</div>
<div>
[b]???[/b]
</div>

 

forum_post.html
======
<div>
<h1>Title</h1>
<p>Text</p>
</div>

Link to comment
Share on other sites

I don't care whether it is php or HTML files. What would the code look like? I was thinking of a php file that included forum.html and forum_post.html many times

 

That's not going to work. Since it's .html, you have no dynamic capabilities. You would have to use placeholders and search/replace the placeholders with real values.

 

What you should do instead is have .php templates and then send the data to the template to loop over.

 

See this post. I made a small function to load a template and to optionally send it some data. You could then loop over the data in your template.

 

It was a really basic example so you will have to tweak it a bit.

Link to comment
Share on other sites

Files used - Create_Forum.php , forum_post.html

Description : forum_html is used as a seperate template file , this is a simple way to echo a forum post. You will also need to create a database with an ID , TITLE , and TEXT field . Hope this helps!!!

 

Code for : Create_forum.php

 

<?php

function CreateForum($id){

//Mysql information

define(HOST,"enter hostname");

define(USER,"enter username");

define(PASS,"enter password ");

define(DATABASE,"enter database");

//Connect to database

$con = mysql_connect(HOST,USER,PASS);

mysql_select_db(DATABASE);

//Include forum_post.html

$dir = "forum_post.html";

include($dir);

//Sql statement used to find a topic based on an id number

$sql = "SELECT * FROM database WHERE $id = topic";

//Execute query

$query = mysql_query($sql);

//Check for a successful query ELSE return error message

if($query){

//Loop through database if successful query

while($row = mysql_query($sql)){

$title = $row->title;

$text = $row->text;

//echo forum

echo "

<div>

<h1>".$title."</h1>";

echo "

<p>".$text."</p>

</div>";

}

}else{

echo "Mysql Error";

}

}

?>

Link to comment
Share on other sites

Thanks for the answers.

 

Now I use the code below. It works just fine - do you have any suggestions for improvements?

 

Now my problem is to make a design around each template file. How is that possible with the code I use?

 

thread.html
====
<div>
<h1><?php echo $title; ?></h1>
<p><?php echo $text; ?></p>
</div>
<div>
<ul>
	<li><a href="/forum/comment/<?php echo $thread_id; ?>">Comment</a></li>
</ul>
</div>
<div>
<h1>Comments</h1>
<?php
foreach($posts as $data) {
?>
<div>
	<h2><?php echo $data['name']; ?></h2>
	<p><?php echo $data['text']; ?></p>
</div>
<?php
}
?>
</div>

 

view.php
====
<?php
final class View {
public function render($filename, $variables = array()) {
	extract($variables);

	require $filename;
}
}
?>

 

forum_controller.php
====
class Forum {
public function thread($id = null) {
	if(is_numeric($id)) {
		$sql = sprintf("SELECT title, text FROM forum_threads WHERE id = %d LIMIT 1", $id);
		$result = MySQL::query($sql);

		if(mysql_num_rows($result) == 1) {
			$row = mysql_fetch_assoc($result);

			$sql = sprintf("SELECT text FROM thread_comments WHERE thread_id = %d", $id);
			$result = MySQL::query($sql);

			$posts = array();
			while($foo = mysql_fetch_assoc($result)) {
				$posts[] = $foo;
			}

			$variables = array(
				'title'		=> $row['title'],
				'text'		=> $row['text'],
				'posts'		=> $posts
			);

			View::render("thread.php", $variables);
		}
	} else {
		View::render("error.php");
	}
}
}

Link to comment
Share on other sites

There you go, that's a lot better.

 

A couple of things though:

1. If you want to call your render method statically, you need to make it static or else you will get the warning

Strict Standards: Non-static method View::render() should not be called statically

. You can do that by adding the static keyword after the visibility keyword (public).

 

2. You should be checking if the template file exists before requiring it, and handle it accordingly.

 

To build a design around the template you can use includes in your template files to include other stuff, like headers and footers.

include 'header.php';

... some template code

include 'footer.php';

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.