php dynamically create new pages in custom cms - php

I'm creating my own CMS from scratch as a way to build my php and mysql skills. Everything is going well, but I'm at the point where I want to create individual post pages for each blog post I write. So the index.php page has a list of all my blogs with snippets of each post and there is a read more button that should take the user to the full page for each blog post. Each post has a url created from the blog title entered in the "create post" form. I'm trying to figure out how to create unique pages for each post without passing the title, subhead, post content and other info through the GET.
This also dovetails with another feature I'm trying to add. I want to be able to create individual pages using a "create page" form like I did for my posts. So if I want an "about us" page I go to my admin form, fill out the title, add the content, and when I hit submit it creates the page dynamically. I have thought all day about how I'd do these two things but can't quite figure out how I can do this.
FYI, I'm not asking for code, I just need a push in the right direction as I try to conceptualize how to achieve this. Thanks!

If you're not familiar with the Model-View-Controller pattern, reading up on it might be prudent. MVC is frequently the right starting place for high-level design of web applications.
Also, a CMS is a big enough project you should consider using a PHP framework like CodeIgniter, Symfony, Zend, etc. to make your life easier. It removes a lot of the drudge work and common tasks.
Dynamic Page Creation and Display
I think you want to split it into two things: the text content (basically what you put in the forms) and the HTML templating surrounding that content.
When you make a page or blog post, you would want to store the actual content (what you type into the creation form) in a database of some sort (not necessarily an RDBMS, but if you're trying to build MySQL skills it's a reasonable choice).
Then you would use a separate function to bind that content into an HTML template and present it to the user when they load a given page.
URL Routing
To get nicer-looking URLs you can use something like apache's mod_rewrite. You can use that to convert a URL like this:
posts/how-to-make-a-cms
to this:
posts.php?title=how-to-make-a-cms
Then you can have posts.php read from GET as normal. How you choose to do the conversion is pretty open-ended.
To avoid getting really complicated rewrites, people often just structure everything to go to a central routing script which figures out what class and method to call and what arguments to pass it. So it would rewrite the URL above to:
main.php?a=posts/how-to-make-a-cms
Then main.php would parse out the segments of that argument from GET and figure out where to send them. Like it might take posts/show/how-to-make-a-cms and do something like:
$o = new Posts();
$o->show("how-to-make-a-cms");
If you do it that way, I think you can avoid mod_rewrite entirely as long as you're willing to accept only slightly pretty URLs, like this:
mysite.com/main.php?/posts/show/how-to-make-a-cms
I haven't done this type of thing before (because the frameworks do it so beautifully already), so I might be missing some minor details.

You should watch some tutorials from phpacademy.org or thenewboston.org, they have best and most valuable tutorials ever made about PHP.
I think you may try to start from that course/playlist:
phpacademy.org: PHP Tutorials: Creating a Blog
If you don't understand everything, watch this:
thenewboston.org: Official Beginner PHP Tutorials Playlist!
If you have no problems with PHP itself you may try to use some simple framework with MVC support. That helps A LOT in variable handling between pages, makes work with database easier etc.
phpacademy.org: Introduction to CodeIgniter
phpacademy.org: Introduction to CodeIgniter - Basic Website

I had the same problem. You can easily do this by using the fopen function. Here is a link to a tutorial: http://www.tizag.com/phpT/filecreate.php
<?php
function wwwcopy($link,$file)
{
$fp = #fopen($link,"r");
while(!feof($fp))
{
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = #fopen($file,"w");
fwrite($fp2,$cont);
fclose($fp2);
}
//Example on using this function
wwwcopy("http://www.domain.com/list.php?member=sample", "sample.html");
//Another example
wwwcopy("http://www.domain.com/list.php?member=sample2", "sample2.html");
?>

Related

JQuery Pagination With Dynamic Loading

I'm currently creating a website for a friend of mine, and he wants a "Blog" type of system on the front page, which is fine by me, i'll be able to do that.
I'm currently using the Pagination Class slightly modified to remove the "All" option (And needless output related to it).
However, I would like to be able to Dynamically load each page instead of redirecting to each page (If a re-write of the Pagination class is required i won't mind doing it, as long as I can get a general concept of how to do it...)
Solutions like JPaginate Won't work for me, because in the end, the page could be loading huge amounts of text (The message body is stored in a LargeText table) and i don't want to have all the articles in the source code (Previous experience such as that has actually taught me loading that much text into a web browser is not the smartest thing)
A few things to note, the connection is MySQLi (The Pagination class only appends the limit code for Sql), I don't know much Javascript, and i still consider myself a noob at php.
You can use something like this:
http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html

PhoneGap & CMS integration

I'm trying to traverse the whole PhoneGap thing to get a native app up and running. I am completely fine with creating html5 markup for the actual app, what I need help with is trying to pull in dynamic content from a website. In particular, there is some content on our website that also needs to be in the app. We use a program call Expression Engine that handles all of our content. The content that I would need to pull over would be:
Sermon Videos
Sermon Series
Locations
Plain text content
The majority of the app will be local, but there are some dynamic needs as you can see. I've read a couple things that say "JSON" is the way to go, but it looks pretty complicated as I'm not quite familiar with AJAX. Is this the only way or are there any options or resources anyone can point me to that might help. I'm not even sure if that method would work for our website. I appreciate any help you can provide.
They are correct. What you need to look into is AJAX/JSON and how to present your data to your app using these technologies.
Expression Engine would actually be quite a good choice for this as its template system is quite flexible. There are even add-on modules for delivering your content as JSON if you want t go that route.
A quick google led me to: http://samcroft.co.uk/2011/updated-loading-data-in-phonegap-using-jquery-1-5/
It's a bit more than you need since you will have your content in an existing CMS instead of creating a new database to store the data, but the concepts will hold true and I am sure you will be able to use it to find more tutorials that suit you better.

CMS permalink and templating system how to?

I want to understand how to use templating system and permalinks on php websites :D !..
let me describe my self more,
1.currently i have 20 files each have its own php logic (index.php,wizard.php,search.php etc)
ALL use same class's and includes.(install.php include all the required for all class's in my project abd u require_once(install.php) in all files)
i wana remodel my website into.
Analyze URL requested ---> IDENTIFY requested page ---> GET TEMPLATE for THIS PAGE -->MODIFY header(meta) and footer(javascript) ---> add logic ---> display page :D.
can some one put me on right learning track :D !. cuz i hv coded my website fully in oop and made all its content loads dynamically from MYSQL (simple small CMS) but i have no clue how to join template and php into index without repeating my self and create different file for each page in my web ! (each file do different jobs of course like file users.php does login and registration and userprofile etc)
Hope my english wassnt too too bad and u could understand my question :D !
My current approach so far:
Mysql table : page_tbl
columns: pagename,LogicFile,templateFile,MetaTag.
index.php?pagename
will check if not already cached or not listed for chacing it will :
Mysql:SELECT * WHERE pagename='$_GET['page']'
loginfiles = cars.php,search.php (will be exploded with , and included)
Template files = will be also exploded and modified according to MetaTag.
Metatag: Serialized assoc array with ['name']=['value'];
Then i start buffer output , replace template with new descriptions and keywords(auto generated)
include logic files
include footer.php (which include the scripts)
am i near to correct rout or still far away ? or am did i lose my track and over killing
If you are unsure whether you want to write your own, or use an existing one.
It is going to be a quite possibly very rewarding experience but a very time consuming one to write your own.
If you have a task at hand that you need to be solved, use an existing one.
That being said there are plenty of templating systems, smarty being the most long-lived one. You can find a short discussion on 5 popular ones at phpbuilder.
You also have entire Frameworks that you might want to consider. They are more than just a templating system, where database stuff, ajax helpers etc are included. Zend, CakePHP, and Codeigniter being popular ones. You can find comparisons of these at phpframeworks.
The best way is to take this route:
parse request data
determine controller which will be used
in controller select layout and template [layout "includes" template, it hold the contents that are common to all pages and template has the request-specific content]
in controller also fetch the data from database [MVP way]
assign data to array and pass it to the layout, it'll pass the data futher to the template
in layout and template use data from that array and construct view
pass everything to the browser
This is the way the frameworks work, and it isn't that bad. ;]

Create Pages Automatically

Is there any method in Php by which I can create a page automatically based on a predefined template. Like if we create a new post in blogger it automatically creates page for that post with the name of that post, like this one:
http://learntoflash.blogspot.com/2009/12/exit-button-in-flash.html
Here Exit Button In Flash is the name of my post I have written and an automatic page is created for it.
Or like here on this website if we ask a question it automatically creates a page for that question. I want to know can I achieve this in Php or anything close to this ?
...here on this website if we ask a question it automatically creates a page for that question.
It sounds like you may believe an actual file is created when you post a question. My bet would be that this page is generated via the question id in the URL.
The only files created would be cached output, which may or may not resemble actual HTML pages.
You should use URL rewriting. This Apache module lets you define rules to rewrite web addresses in your desired way.
The process to make your web application ready for this, is not a short story so you should read more about it.
This article is a good starting point:
http://articles.sitepoint.com/article/guide-url-rewriting
This is acheived by using mod_rewrite. A good place to look for inspiration is the .htaccess used in Wordpress.
to something like that you have to grasp the very fundamental in php or any programming language at all, i mean the core of php is to create dynamical generated pages based on user/browser input.
You might need to take a quick tutorial about php might I suggest http://www.tizag.com/phpT/
good step for step tutorial
Edit:
if you're wondering how the websites seems to have created a html page for every question, the answer would be they're not they are probably using mod_rewrite as mentioned before to rewrite to url to print a little more user friendly url, the actual url could be something like this https://stackoverflow.com/index.php?post=4499289 in reality

How to organize a PHP blog

So, currently I'm organizing my blog based on filename: To create a post I enter the name of the file. As opposed to storing the post in the database, I store them in PHP files. So each time I create a post, A new row in the table is created with the filename and a unique ID. To reference the post (e.g. for comments) I get the name of the current file, then search the entries table for a matching file name. The post ID of the comment matches the ID of that post.
Obviously this isn't the standard way of organizing a blog, but I do it this way for a few reasons:
Clean URL's (even cleaner than mod_rewrite can provide from what I've read)
I always have a hard copy of the post on my machine
Easier to remember the URL of a specific post (kind of a part of clean URL's)
Now I know that the standard way would be storing each post in the database. I know how to do this, but the clean URL's is the main problem. So now to my questions:
Is there anything WRONG with the way I'm doing it now, or could any problems arise from it in the future?
Can the same level of clean URL's that I can get now be achieved with mod_rewrite? If so, links are appreciated
I will be hosting this on a web host. Do only certain web-hosts provide access to the necessary files for mod_rewrite, or is it generally standard on all web-hosts?
Thanks so much guys!
P.S. To be clear, I don't plan on using a blogging engine.
As cletus said, this is similar to Movable Type. There's nothing inherently wrong with storing your data in files.
One thing that comes to mind is: how much are you storing in the files? Just the post content, or does each PHP file contain a copy of the entire design of the page as opposed to using a base template? How difficult would it be to change the design later on? This may or may not be a problem.
What exactly are you looking for in terms of clean URLs? Rewrite rules are quite powerful and flexible. By using mod_rewrite in conjunction with a main PHP file that answers all requests, you can pretty much have any URL format you want, including user-friendly URLs without obscure ID numbers or even file extensions.
Edit:
Here is how it would work with mod_rewrite and a main PHP file that processes requests:
Web server passes all requests (e.g., /my-post-title) to, say, index.php
index.php parses the request path ("my-post-title")
Look up "my-post-title" in the database's "slug" or "friendly name" (whatever you want to call it) column and locates the appropriate row that way
Retrieve the post from the database
Apply a template to the post data
Return the completed page to the client
This is essentially how systems like Drupal and WordPress work.
Also, regarding how Movable Type works, it's been a while since I've used it so I might be wrong, but I believe it stores all posts in the database. When you hit the publish button, it generates plain HTML files by pulling post data from the database and inserting it into a template. This is incredibly efficient when your site is under heavy load - there are no scripts running when a visitor opens up your website, and the server can keep up with heavy visitation when it only needs to serve up static files.
So obviously you've got a lot of options when figuring out how your solution should work. The one you proposed sounds fine, though you might want to give careful consideration to how you'll maintain a large number of posts in individual files, particularly if you want to change the design of the entire site later on. You might want to consider a templating engine like Smarty, and just store post data (no layout tags) in your individual files, for instance. Or just use some basic include() statements in your post files to suck in headers, footers, nav menus, etc.
What you're describing is kind of like how Movable Type works. The issues you'll need to cover are:
Syndication: RSS/Atom;
Sitemap: for Google;
Commenting; and
Tagging and filtering content.
It's not unreasonable not to use a database. If I were to do that I'd be using a templating engine like Smarty that does a better job of caching the results than PHP will out of the box.

Categories