PHP - Template System With Layout (views)
I've made a template system, but the way I've chosed to do it doesn't give me the ability to choose a design. I've posted the code below with a bad solution for my design problem. Does anyone have any suggestions how to make a very simple and smart templatesystem? I don't have any problems coding. I just need ideas :-)
Code: [Select] <?php final class View implements IView { protected $filename, $variables = array(); public function __construct($filename = null, $variables = array()) { $this->filename = $filename; $this->variables = $variables; } public function __toString() { if(!file_exists($this->filename)) { throw new Exception("View does not exists."); } $content = file_get_contents($this->filename); foreach($this->variables as $variable => $value) { $content = str_replace("#{{$variable}}", $value, $content); } return str_replace("#{content}", $content, file_get_contents("design.html")); } } ?> Similar TutorialsThis topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=352167.0 I've created a template system. I've got a function to assign a page (the templates are in /template/skin). It all works fine in the main root, but if I had a directory it won't work. $dir = "template/skin"; That is found in template/ I need to be able to assign the template even in directories. The current system I am using has worked so far but it has its problems. Basically I have 2 files to display forum topics. view_topics.php and topic_list_tpl.php view_topics.php defines some variables like so $template->topic_name = $row[$key]['topic_name'] then it renders the page like so $template->render('topic_list_tpl.php'); then in topic_list_tpl.php i use this to print the name of the topic <?php echo $this->topic_name; ?> which works fine. But as you know a topic list needs a header. But the render function is within a foreach loop so it displays all of the topics from the query. This poses the problem of the header being looped, which it shouldnt be. So in view_topics.php i use: $template->topic_id = $row[$key]['topic_id']; $template->first_topic_id = $row[0]['topic_id']; then in topic_list_tpl.php i can use: <?php if($this->topic_id == $this->first_topic_id) { echo the header } else { echo the topics } but the problem comes when adding a footer bar at the bottom(for the new reply link and other topic options). i can use something like $amount_of_topics = count($row); $template->last_topic_id = $row[$amount_of_topics-1]['topic_id'] and then check it like that but that wont work when using pagination as the last topic displayed isnt always going to be the last topic in the query. So another option i have is to have the header and footer bars in seperate files and call them outside of the loop. But these files would only be 2 or 3 lines each. So is this the best option? or is there an easier way of doing things? Hey guys, I'm starting to build a website and I was wondering wether I should use a library template engine such as smarty, or use pure php, such as this tutorial - http://www.massassi.com/php/articles/template_engines/ Basically the website will not expose any of the users/designers to the designer portion of the website. I am scratching my head wether I should use smarty or pure php, even Facebook uses smarty, from what I have heard. I need some answers. Thank you all! Hey guys first of all I got 3 files - index.php require_once('/libs/template.class.php'); $template = new Template; $template->assign("title","test"); template.php <?php echo $title; ?> template.class.php function assign($var, $val) { $var = $val; } I'm trying to assign a variable in index.php, then recall it in the template.php, how would I go about this? Thank you! This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=359002.0 So I made a template class, it's very simple, pretty much allows the usage like so: $template = new template("blogpost"); $template->fillBraces(array( "TITLE" => "How to bathe your chimpanzee", "AUTHOR" => "Jim Bo James" )); $template->render(); Simple, uses tpl files, similar to what phpbb used a long time back, not sure if they still do. My main goal for this is to have a file that has something like this: {HEADER} {MAIN_CONTENT} {FOOTER} and the above usage to stay the same, however that would fill the main_content part, and the header and footer would be grabbed from header/footer tpl files. Any idea on suggestions? The full class is posted below. class template { protected $_viewContents; function __construct($viewName) { $this->_viewContents = file_get_contents(TEMPLATE.$viewName.TEMPLATE_EXT); return; } function sourceOf($fileName) { return @file_get_contents(TEMPLATE.$fileName.TEMPLATE_EXT); } function fillLayout() { } function fillBraces($information, $replacement=NULL) { if (is_array($information)) { foreach ($information as $key => $val) { $this->_viewContents = str_ireplace("{".$key."}", $val, $this->_viewContents); } return; } elseif ($replacement!=NULL) { $this->_viewContents = str_ireplace("{".$information."}", $replacement, $this->_viewContents); return; } } function render() { echo $this->_viewContents; } } Thanks! Ok i want it to make it that everything some clicks on a view the view goes up by one so i did this Code: [Select] <?php session_start(); $views = $_SESSION['views']++; ?> and echo the views like this Views<?php echo $views; ?> but it updates countines to add even if the video is a new video hello dear PHP-Fans - greetings to you - and a happy new year!! i set up a WAMP-System on my openSuse 11.4 system. In order to learn as much as i can bout PHP i want to do some tests and write some scripts. Well the WAMP is allready up and running. Now i try to give the writing access to the folder mkdir /srv/www/ where the php-scripts should go in... i want to give write permission to all to all files in /srv/www As root I generally: mkdir /srv/www/ chown <webmaster usrername> /srv/www/ /srv/www/ should be readable and traversable by all, but only writeable by it's owner (the user designated as the webmaster.) can i do this like mentioned above,... Love to hear from you greetings db1 hii i want to create a script where i want to store unique page views per 24 hrs using cookie.. i had create a code but rectify me if i'm wrong... 1) i wan to store a cookie so i set this code on top to create a cookie on views browser.. Quote <php setcookie("user", time()+84600); ?> 2) Now i want to find that if the cookie was set before, then the views will not update, if not then it will increase by 1. Quote <php if (isset($_cookie["user"])) echo "Total no of views" . $views . "; else echo" $views=$views+1; echo " $views"; $k=mysql_query("UPDATE `images` SET views = '$views' WHERE id = '$userid'") ?> but the problem i'm facing is that where to put that "$views" so that it will increase and will show to the user... now suppose every thing works well then how the cookie will understand that the views will be updated in specific users database... please guys i really need help... If some other possible ways are there then please refer... Thanks in advance.. Hello folks, good day! I'm having trouble here selecting my desired data. I hope somebody will help me. Im having two tables. tbl_a seId course year 1 IT 1999 2 HE 1992 3 RT 1990 tbl_b id seId status 1 1 NO 2 2 YES I want to select A.*,B.* from tbl_a AS A, tbl_b AS B having status = NO and status IS NULL my desired output is to see the following. course year status IT 1999 NO RT 1990 is it possible? thank you in advance. hii i had created a site but i'm facing one problem. where users had to show there page to earn money... like 1000 imp/1$. but what happened is that when ever page reloads in same browser with same ip, it gaining impression. is there any way that i can limit 1 impression per ip in 24 hours... Any help will be appreciated... hey guys im trying to make a page statistics, where i get a view count of certian pages users visit.
now im after a bit of advise on how the best way to achieve this
here are my tables:
CREATE TABLE IF NOT EXISTS `statistics` ( `statistic_id` int(11) NOT NULL AUTO_INCREMENT, `statistic_page_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `reference_id` int(11) NOT NULL, `ip_address` varchar(15) NOT NULL, `created_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`statistic_id`) ) CREATE TABLE IF NOT EXISTS `statistic_pages` ( `statistic_page_id` int(11) NOT NULL AUTO_INCREMENT, `page_name` varchar(90) NOT NULL, `created_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`statistic_page_id`) )what i was thinking is when a user views the page it will check if user has already visited in the database SELECT user_id FROM statistics WHERE user_id = '1' OR ip_address = INET_ATON('1.1.1.1') AND statistic_page_id = '1' AND refernce_id = '10'if user or guest hasnt visited then add to database INSERT INTO statistics (statistics_page_id, user_id, refernece_id, ip_address, created_timestamp) VALUES ('1', '1' '10', INET_ATON('1.1.1.1'), UTC_TIMESTAMP())statistics_page_id, would be the id representing a page ie. ITEMS and reference_id would be the id of the ITEM any advise on a better way on how it should be done would be super...cheers guys Hi,
I have a blog which records the amount of views on each article. I now want to be able to work out the average number of views per hour.
How can I work out the number of hours passed from a datetime format?
From there I can just do views divided by hours passed.
Thanks in advance!
If anybody could give me code for that gets site views, that would be really nice. Hiii i got a image hosting script but the problem i'm not able to solve is that in the show image page, the page views counter is not showing unique views. If we refresh the page 100 times from same ip, it shows (views=100)... Can any one help me with it... The views are stored in the database of the respective users... can anyone help me to get unique views per 24 hrs per ip.... The code is... Quote //UPDATE VIEWS COUNT AND LAST ACCESS DATE $views = $row['views'] + 1; $access = date("y-m-d"); $r = mysql_query("UPDATE `images` SET views = '$views', access = '$access' WHERE id = '$id' "); $imguserid = getUserId($id); $own = false; if ($imguserid != -1) { if ($userid == $imguserid) $own = true; $r = mysql_query("SELECT username FROM `members` WHERE id = '$imguserid'"); $row1 = mysql_fetch_row($r); $username = $row1[0]; } else $username = "Anonymous"; echo "<center>"; echo "<br><LABEL id='title'>Views:</LABEL> $views"; echo "<br><LABEL id='title'>Date Added:</LABEL> {$row['date']}"; if needed i can supply the full script to the person who can help me... You can check the demo in [ http://torrentz.0fees.net/wussa/show-image.php?id=c7693c02c3ad2fafe5f5d3f00263a68a ] The full page is attached below.... Currently on my website i record the statistics, for my pages they include unique views, and total page views. at the moment every time a visitor loads a page a script updates a MySql Db. This seems very resource intensive, is there a better way to record statistics?? So i have a bunch of random videos playig one after the other what i want is to track if the user viewed the videos by inserting a view lets say in to the database, so if they view the first video the database will increment by 1 then after the page refreshes and the next video loads it will increment again, the only part i dont know what im doing is how would i know if my user viewed the video, any help please thank you? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313049.0 |