Stevenn Posted March 20, 2012 Share Posted March 20, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/ Share on other sites More sharing options...
scootstah Posted March 20, 2012 Share Posted March 20, 2012 It's pretty much impossible if you use .html files. The only way to do it is to file_get_contents the html file and then search/replace, which is pretty stupid IMO. Instead, use .php files. This way you can loop over stuff in your template files. Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329629 Share on other sites More sharing options...
Stevenn Posted March 20, 2012 Author Share Posted March 20, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329634 Share on other sites More sharing options...
cpd Posted March 20, 2012 Share Posted March 20, 2012 Stevenn just make all your pages .php and use a while loop while(row = mysql_fetch_assoc(resourceID){ Do some stuff } Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329647 Share on other sites More sharing options...
scootstah Posted March 21, 2012 Share Posted March 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329673 Share on other sites More sharing options...
christianmiles40 Posted March 21, 2012 Share Posted March 21, 2012 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329694 Share on other sites More sharing options...
Stevenn Posted March 21, 2012 Author Share Posted March 21, 2012 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"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329741 Share on other sites More sharing options...
scootstah Posted March 21, 2012 Share Posted March 21, 2012 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'; Quote Link to comment https://forums.phpfreaks.com/topic/259365-output-html-files/#findComment-1329823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.