3

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!

6
  • 1
    Without passing anything , how will the content show up on single post page. You have to pass atleast a post id via get or post. Then you can change the url to whatever you like using permalinks . Commented Oct 27, 2012 at 5:04
  • @jasminder, that's the question i've been tossing around in my head and kinda the only way i can see it happening but wanted to be sure before i spend time coding it. Commented Oct 27, 2012 at 5:08
  • 2
    Don't think that way: "I want to create individual post pages for each blog post I write.". You should want to create one page called "post.php" that display dynamic (variable) content, depending on parameter in link for example, like this: "post.php?postid=1". Commented Oct 27, 2012 at 5:20
  • what's funny is that just going to thenewboston.org helped me sort of understand what we're discussing. they have all of their tutorial videos on a template called watch.php that then has an id added to the url. funny how that clicked in my mind. Commented Oct 27, 2012 at 5:24
  • 1
    When you master that dynamic page idea with variable content - you can try to use htaccess to rewrite URL to put post title in link instead of ID, or you can use some PHP Framework. CodeIgniter is easiest to learn and really saves a lot of time on "fight" with POST/GET variables between pages. I learned CI in 1 hour... Commented Oct 27, 2012 at 5:27

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Im not sure, that MVC is for someone who is not experienced in PHP.
@Kamil, MVC isn't specific to PHP so I don't know why you would say that. It's a broad design pattern that's important to understand for anyone at the level of designing something like a CMS.
@FoolishSeth, you said "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." i know i need to put my title, subhead, content, etc ... in a template but how do i make each blog url be for example "www.sitename.com/this-is-post-1" or www.sitename.com/about.php" instead of www.sitename.com/?post_id=12 ? Is that possible or should i create every page with that url structure and then use a url rewrite to make the url actually be "pretty"?
well, the frameworks will make that sort of routing really easy, but I'll update the answer with some info on URL routing as well.
@FoolishSeth ok, you may be right. Shall we show him CodeIgniter? :)
|
1

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

2 Comments

i know about phpacademy and that exact link is on my to-do list for this coming week so it's funny that you mention that.
You should start watching now instead of wasting time on asking about something that was professionally explained in these tutorials :)
1

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"); 

?>

1 Comment

Your code is perfect with some modification: on line 7 you should have some like " $cont = " not " $cont . =" (without dot)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.