-2

I've been working on my husband's website for a while now. I'm currently redesigning the site, and I want to make it more future-proof. Currently, his blog posts just display all in a row on his page (with some pagination to break it up), which means if someone wanted to add a post to their favorites, it wouldn't work as more posts get added. So, I want to create little excerpt posts that then link to the full post.

My question is, how does one do this? I've searched many sites trying to find an answer, but all I've been able to find is how to set up your own database (which I already have) and how to create log-ins (which I don't need), and echo the posts out the way I already have them.

Basically, I want an excerpt from the post (first few lines or something), and then a link to read the full article. I'm not really sure how to do it without making separate html pages for each post rather than getting them out of my database somehow. I.e., I don't know how to automatically generate the URL (and code) necessary to display the full post.

I'm sorry if I'm not asking this in the best way. The answer is probably out there, I just can't seem to search for the right thing. :(

Edit: The website is SpidersInAJar.com The database is mySql, set up very simply with ID, Title, Body, and Date.

SetsunaMH
  • 23
  • 7
  • 1
    Please keep the explanation of your question as brief as needed, we don't need someones life story to answer to a question :). Also you may link to the actual page or show an example. To me it is currently not clear what exactly you are asking and what the problem is. – Juru Oct 08 '14 at 17:21
  • In order to help you, we'd need to know more about your setup. How are posts laid out in the database? What language is the site written in? – Frank Riccobono Oct 08 '14 at 17:21
  • I took a look at your site and I would very strongly encourage you to do this in WordPress (or some other existing content management system) as opposed to rolling your own. The problem you are trying to solve has been solved hundreds of times over by various CMS's. The difference in effort between retooling this site to function as you imagine vs setting up an existing CMS that already does what you want...door number two will be a lot faster, and less prone to error. – Evan Volgas Oct 08 '14 at 17:35
  • Is each

    CONTENT

    blog post a separate entry in a blogpost table?
    – JNevill Oct 08 '14 at 17:35
  • @evanv I'm really trying to learn how to do this from scratch for myself, if for nothing else to say I know how to do it. – SetsunaMH Oct 08 '14 at 17:44

1 Answers1

1

Working off the assumption that your blog posts are in a table called blogpost in your database. I'm also assuming that currently you are doing something like SELECT ID, Title, Body, Date FROM blogpost ORDER BY ID DESC, then looping through the results, sticking a

around them and calling it done.

Further assuming, what you want is to have the same page load with a list of snippets, each with a link that when pressed will reload the page and show only the blog post.

To get your snippets you'll need to change your SQL a bit:

SELECT ID, Title, CONCAT(Substring(Body FROM 1 for 300), "...") as Body, Date FROM blogpost

This will give you the first 300 characters from each blog post, for instance.

In your PHP where you loop through the results and wrap your <p></p> around everything you'll need to add a new link tag... something like

echo '<a href="index.php?blogpost=" & <This blogposts ID> & '">read more</a>'

This will send the ID of the blog post as a variable back to the index.php page. So the browser will refresh and you will be able to blogPost=$_GET['blogpost'] at the top of your php page and grab what was sent and act on it. Obviously this makes your code a bit more complicated because now you have to determine if there was a variable sent and either display all the snippets (if the blogPost variable is null) or just display the blog post (selecting only that ID from the table and displaying it).

This also introduces a big fat security risk via SQL injection. Anyone can now send arbitrary SQL into your database by manipulating the URL like: http://spidersinajar.com/index.php?id=1 or a=a;DROP TABLE blogpost; and whoosh goes your table. You'll want to read up on how to properly prepare the sql statement before sending it to the database to avoid heartache down the road.

There's obviously much more code that needs to be written to make this work, but this is the high level and should give you some direction to head in.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Thank you! This is very helpful. I saw something on another website talking about only taking an int from the url to avoid the sql injection. Would that work? – SetsunaMH Oct 08 '14 at 18:22
  • 1
    Using an int will limit injection to an extent, but if you're trying to learn best practices, you should read this question and the top answer: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Frank Riccobono Oct 08 '14 at 18:27