PHP - Creating Template Pages But All Have Different Content?
Hi Everyone,
Please excuse my lack of tech. terminology. I'm in the learning stages of php/myslq Do you know how both the below pages are very similiar. The only thing that's different is the actual location(s). My question is, is the same code being used for all of these template pages? If so, how does the code know differentiate between different states/cities/? Does each state or even city have a different template? <<--- Thinking out loud here, I don't believe so because that would defeat the purpose of php/mysql. What do you think? http://cars.oodle.com/regions/illinois/ http://cars.oodle.com/regions/indiana/ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> http://cars.oodle.com/regions/illinois/ <<--- Assuming this code is being used on the "state template" how does the php/mysql code on this page know how to show data from the Bloomington area of Illinois? (assuming it's not custom code for the above page) http://cars.oodle.com/used-cars/bloomington-il-area/ <<--- Assuming this code is being used on the "cars template" how does the php/mysql code on this page know how to show data from the Bloomington area of Illinois? (assuming it's not custom code for the above page) Thanks everyone! Similar TutorialsI've made a test database from the book "PHP and mySQL Novice to Ninja"... now I'm testing out making the structure better by using templates to output the html. This is the template... note the part in with the arrow pointing to it. That's the dynamic content which should change based upon what page is passed to it.
<!DOCTYPE html> <html lang="en"> <head> <link href= "/project/includes/style.css" rel="stylesheet" type="text/css"> <meta charset="utf-8"> <title>Joke Database Project</title> </head> <body> <div id="topheader"> <p>Joke Database Project</p> </div> <div id="navigation"> <?php include $_SERVER['DOCUMENT_ROOT'] .'/project/includes/navbar.html.php'; ?> </div> <div id="container"> <?php include '/'.$content;?> <<<<--------------------------------------- </div> </body> </html>It works fine when I use the following code in my php script: $content="authors.html.php"; include $_SERVER['DOCUMENT_ROOT'] . '/project/includes/html_template.html.php';However I want to make it less clunky so I decided to create the following function include in my php script so I can show the relevant content by using showpage("author.html.php") function showpage($content) { include $_SERVER['DOCUMENT_ROOT'] . '/project/includes/html_template.html.php'; }The problem I'm having is when I use this function the outputed page no longer recognises the variables from the main script so the proper content doesn't show. (note it works fine when not using the function but just including it directly in the script). How can I get around this in an elegant way? Edited by holdorfold, 27 September 2014 - 01:16 PM. Hello everybody, I've started to work on building my own template engine for my website since I need to seperate the logical units from the graphical units and template engines such as Smarty and TBS are way too big for what I really need: a very basic, simple template engine which manage simple tasks such as value reference and conditions. The value referencing has been quite easy, a simple str_replace would do the job correctly. Where it gets complicated is at the conditional management. I need it to make, for exemple: Code: [Select] [IF USER_STATUS_ADMIN]{SHOW_ADMIN_LINK}[ELSEIF USER_STATUS_WRITER]{SHOW_WRITER_LINK}[ELSEIF USER_STATUS_TESTER]{SHOW_TESTER_LINK}[ELSE]{SHOW_ALL_LINK}[/IF] turn into: Code: [Select] if $user_status_admin { echo $show_admin_link; }elseif $user_status_writer{ echo $show_writer_link; }elseif $user_status_tester{ echo $show_tester_link; }else{ echo $show_all_link; } I must admit that I am quite rusted in PHP, so to reach this goal I first thought about using a regex to dispatch all my values... I used this regex: Code: [Select] /\[IF (.*?)\](.*?)(\[\/IF\]|\[ELSEIF (.*?)\](.*?)\[\/IF\])/i Before long I realized that I could only retrieve a single ELSEIF value. So I went with a string scaning method using this code: Code: [Select] $str = '[IF USER_STATUS_ADMIN]{SHOW_ADMIN_LINK}[ELSEIF USER_STATUS_WRITER]{SHOW_WRITER_LINK}[ELSEIF USER_STATUS_TESTER]{SHOW_TESTER_LINK}[ELSE]{SHOW_ALL_LINK}[/IF]'; $pos = strpos($str, '[IF '); A new problem arose... Finding the first "[IF " was going fine, but finding the USER_STATUS_ADMIN between "[IF " and "]" was giving me trouble... So here I am now, which method I should focus my energy on and how to achieve my goal. Any help on the matter would be greatly appreciated, Thanks ! I have not created any code for this as I dont know where to start, or what its even called. I am on about trying to create a page dynamically depending on the id which is selected. Parts of the URL will change depending on what id is selected for example: 'http://www.facebook.com/home.php?sk=h. i am wanting to create a video website, where when the thumbnail of a video is clicked, a new page is loaded with that more details of the video and the video itself, and im guessing the url would change to identify the id of the video. I cannot create a page for every video by hand, as i will have many videos and this is not very efficient. I am just after the name of this technique or a pointer to a tutorial for how to achieve this. Thanks, Matlab I have designed and created a ecommerce website that allows the user to add products to a shopping cart and for the admin to edit the inventory list. However i would like to have a navigational bar on the website where the user can choose a brand of product and the webpage shows only that brand. I know i can do this by using the WHERE command in a query however would i have to make a separate web page for each brand? Thanks Boldonglen Hi. I’m hoping someone can help me pull the styling/formatting from a static page onto a category template. The content is pulling fine, but it's not formatted/styled. Code used to pull content from static page onto custom category template: <?php $page = get_page_by_title( 'page-name' ); $content = apply_filters('the_content', $page->post_content); echo $content; ?> With the above code, the content is being pulled, but the formatting and styling is not being pulled. The following code is adapted to pull the styling/formatting: <?php $page = get_page_by_title( 'Wedding Venues in Costa Rica' ); $content = apply_filters('the_content', $page->post_content); echo do_blocks( $content );> It did not pull the styling/formatting. We’re using GeneratePress Pro + Generate Blocks (no page builder). The static page is built with Gutenberg and GenerateBlocks. By the way, I'mm not a coder or a developer. I know a little about this and about that, but this isn't my field of expertise. Could someone please suggest what can be done to this code to get it to pull the styling form the static page onto the custom category template? Thank you very much. Or should the Twig class be used to generate the content type header?
require_once '../../../vendor/autoload.php'; Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem('templates'); $twig = new Twig_Environment($loader, array('debug' => true,)); $twig->addExtension(new Twig_Extension_Debug()); header('Content-type: text/html; charset=utf-8'); echo $twig->render('myTemplate.html', array()); Hi all, I have a database which contains customer details. We have a php script which pulls the records from the database and puts them into a table on the page. Currently, it fetches all the customer records. This wasn't such a problem when there was only a few customers in the db but this is a bit higher now! Is there any way I can modify the script to only show 20 per page and add links to page 2 etc along the bottom, which can be clicked to show the next 20 and so on? I can post my current code if need be, Thanks. Hi, if I want to sepcify certain elements to a category: <?php if (category_id == 3) { ?>Content for that paticular category.<?php } ?> How would I set it for the pages within the category? - I can do it like so: <?php if (category_id == 3 || page_id == 4 || page_id == 5) { ?>Content for that paticular category.<?php } ?> But that means everytime I add a new page within that category I would have to manually add the new page_id to the list. Is there a way to write it so it can display category_id and all pages within? Any help would be great, thanks. Paul Hey everyone, I'm new to web programming so I thought I would join a active community to help me out. Anyhow, I'm making a game portal and I want the users games to have a url like so... games/username/gamename. From what I understand I could get this structure by simply using data from my login session(username) and using my upload form(gamename) and mkdir. Then I would need to have an index page inside every gamename folder? How would I add the index pages inside such folders. This way seems pretty inefficient to do considering I could pull the games dynamically in a single php file. Is there a way I can make my url look nice and still use one file to handle the embedding of the games, comments etc. Thanks I got a question regarding a news website content that i want to make ! my question is how do i call my contents without using a lot of page? i explain let's say i got 10 news how do i put this 10 news in different pages without using 10 pages ? ex: you can see some links having a number like this http://bbc.uk/news/murder_case-12 then the next page got http://bbc.uk/news/finance-13 the title and the number id change but the page news doesnt change thanks for your answer. This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=357949.0 Hi, I will start off trying to explain what I am trying to make the best I can. What I want to create is a script that gets the gold value from this website: http://www.lbma.org.uk/pages/index.cfm?page_id=46&title=current_statistics and then save it to a variable which I will use to calculate values of different gold karats. Here is the content in bold I need on the website I linked: Quote LONDON GOLD FIXING USD GBP EUR AM 1588.00 1005.127 1251.083 PM 1589.50 1004.741 1249.803 So what help do I need? Well, I don't expect you to figure out the calculating part for me but some help how to get that content pointed out above and save it to a variable is what I would appreciate getting some help with. I don't know much PHP, only some and I have been trying to figure this out for a day now without any success. I suppose php get contents and/or curl should be used here but I don't know how really. I would very much appreciate the help I can get on this. Thank you! hey guys im making a template class which is working and im trying to impliment a loop section for MySQL results... but i need to be able to find a match for a string such as: {section name=1 loop=$2} content {/section} but also extract the values of name, loop and the section content if anyone could advise me on the best way this can be done please...thank you Hello i am going to try out fpdf generator, but i wanted to know if anyone knows where i can get invoice templates or how would i go to create them. Could i create them with css and html? So I have a simple template that I want to be the index page of the website that I'm working on, however, I don't want to have to create several almost identical files for each of the directories.
Is there perhaps anyway that I can change the content of the main div yet the address be a different directory.
So for example, this is the main index page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <title>Matthew Noskiw's website - Home</title> <link rel='stylesheet' type='text/css' href='./inc/CSS/styles/default.css' /> <link rel='stylesheet' type='text/css' href='./inc/CSS/static/navigation.css' /> </head> <body> <div id='Container'> <div id='Header'> <div class='right'> <ul id='Navigation'> <li id='NavButton' class='active'><a href='./'>Home</a></li> <li id='NavButton'><a href='./about'>About</a></li> <li id='NavButton'><a href='./blog'>Blog</a></li> <li id='NavButton'><a href='./contact'>Contact</a></li> </ul> </div> <span>noskiw.co.uk</span> </div> <hr /> <div id='MainContent'> <h2>Home</h2> <br /> <span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </span> <br /><br /> <span> Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? </span> </div> <hr /> <div id='Footer'> <span>Matthew Noskiw</span> </div> </div> </body> </html>But if you look in the <h2> tag in the 'MainContent' div, then you'll see it reads "Home". What I want to happen is that when I change the page, for example, go to /about I want it to read <h2>About</h2>. I have 4 different pages for this so far, is there any way to get it down to just one yet they be in different directories? P.S The reason this is in the PHP Help section is because I assumed that it would more than likely be something to do with PHP. Edited by ghostrider1337, 02 August 2014 - 02:36 PM. 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? I'm trying to develop a website file manager. I want to allow SSI, and would like to handle it specifically.. Code: [Select] $regexp = "<!--#include\s[^>]*virtual=(\"??)([^\" >]*?)\\1[^>]*\/-->"; if(preg_match_all("/$regexp/siU", $body, $matches, PREG_SET_ORDER)) { foreach($matches as $match) { $includeFile = $match[2]; } } This snippet shows how you would get the included file path in the SSI code of the website body [ the $body tag ], but I don't need to just find it- I want to replace the SSI code with a simpler code in the HTML that appears in the editor for easy management.. Then switch back from my easier code to the actual SSI code.. Example: Replace Code: [Select] <!--#include virtual="/newsManager/output.php"--> With Code: [Select] {INCLUDE=/newsManager/output.php} THEN when the user saves the page, Replace Code: [Select] {INCLUDE=/newsManager/output.php} With Code: [Select] <!--#include virtual="/newsManager/output.php"--> Is there an easy fix or should I just switch to a full blown template engine.. even though this is the only required feature? Hello All!
Below is a simplified version how I render about every page. Should I start using a dedicated template engine? If so, which one and why? Or, should I stick with my existing system? If so, should I make any changes to it? Thank you <?php class myModel { public function getData() //This would really be a query to a DB { $dataFromMyDB=new stdClass(); $dataFromMyDB->firstname='John'; $dataFromMyDB->lastname='Doe'; $dataFromMyDB->age=111; return $dataFromMyDB; } } /** * Typically used to htmlspecialchars variable. * $nbsp=0: return htmlspecialchars input * $nbsp=1: return htmlspecialchars input or   if nothing * $nbsp=2: return input or   if nothing * @param $html * @param boolean $nbsp */ function h($html,$nbsp=0) { $html=trim($html); switch($nbsp) { //Consider changing ENT_QUOTES to ENT_QUOTES|ENT_XHTML or ENT_QUOTES|ENT_HTML401 case 0:default:$return=htmlspecialchars($html, ENT_QUOTES, 'UTF-8');break; case 1:$return=($html)?htmlspecialchars($html, ENT_QUOTES, 'UTF-8'):' ';break; case 2:$return=($html)?$html:' ';break; } return $return; } function render($viewFile,$model) { ob_start(); require $viewFile; $html_string=ob_get_contents(); ob_end_clean(); return $html_string; } $model=new myModel(); echo(render('mvc_view.php',$model->getData())); ?>mvc_view.php <p>Hello! This is my View</p> <?php echo(h($model->firstname).' '.h($model->lastname).' is '.h($model->age).' years old.'); ?> 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! |