Jump to content

make a blog post clickable to display on it's own page


webguync

Recommended Posts

Hi, I have a blog page I am working on and I am trying to make some enhancements that I need some help with. In MySQL I have a table with id, title and post. In my page display I want to enable the user to click on the title and display a post on it's own so the URL would look like www.mysite.com/blog/?id=1 or something like that. Here is the PHP function to display the post from MySQL. all on one page.

 

function GetBlogPosts($inId=null, $inTagId =null)
{
if (!empty($inId))
{
	$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC LIMIT 2"); 
}
else if (!empty($inTagId))
{
	$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
}
else
{
	$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
}

$postArray = array();
while ($row = mysql_fetch_assoc($query))
{
	$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
	array_push($postArray, $myPost);
}
return $postArray;
}

 

 

Link to comment
Share on other sites

Maybe this will help, I made a simple sample demo emulating a website and post links.

 

I have no idea what the rest of your code is, but the normal is to query the database, find how many results and paginate them. From the paginated links you display a content area. Depending on the query it would display different results.

 

As for yours it seems you made this a function and have preset rules there and a pile of joins. I guess it's possible to just include a different function with different select queries in the content area.

 

 

You may view this exact code here:

http://get.blogdns.com/dynaindex/post-variable.php

My home link is different because I'm running this in a subdirectory.

 

<?php
//dummy post post data
$post_id =  range(1,20);
$title = "Link to a post";
$description = "This is a demonstration of linking to a post all using the same file. Depending if you use by id or a specific value would not matter, you just have to set it to those.";
$comment = "blah blah blah is my comment";                   
//end of dummy post data                    

$post = mysql_real_escape_string($_GET['post']);
$home_url = "http://".filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING);
$about_page = "$home_url/about/";
$url = "http://".filter_var($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'], FILTER_SANITIZE_STRING);

if (!empty($_SERVER["QUERY_STRING"])){
$url .= "?".filter_var($_SERVER['QUERY_STRING'], FILTER_SANITIZE_STRING);
}
if ($url == "$home_url") {
$url = "$url?$post";
}

echo "<a href='$home_url'>Home</a><br />";
echo "<a href='$url'>$url</a><br />";
echo "<a href='$about_page'>About</a><br />";
echo "<br />";
?>

<form name="input" action="<?php echo $url; ?>" method="get">
ID Number or Post Name:<input size="30"type="text" name="post" style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="<?php echo $post; ?>">
<input type="submit" style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="Go to Post" />
</form>

<?php
if (isset($_GET['post'])){
echo "<h2>Post Content</h2";
echo "<hr>";

$post_link = "<a href='$url$post_number'>Permalink</a><br />";
echo $post_link;
echo "$post<br />";
echo "<p>".$title."<br />".$description."<br />".$comment."</p><br />";
echo "<hr>";
} elseif ($url == "$about_page"){
echo "<h2>About Page</h2";
echo "My way of displaying different content using a single php script";
} else {
echo "<h2>Main Content</h2<br />";
echo "<hr>";
foreach ($post_id as $post_number) {
echo $post_number;
$post_link = "<a href='$url?post=$post_number'>View post</a>";
echo "<p>$post_link</p><br />";
echo "<hr>";
}

}

echo "<br />";



?>

<?php
echo "Compliments of Quick";
?>

Link to comment
Share on other sites

thanks for the reply. I think if I did it the with the code you posted I would have to re-do a lot of what I have, and I was hoping for a simpler solution. I realized it will probably help if I post all of the code, so I am doing that now.

 

blogpost.php...

<?php

class BlogPost
{

public $id;
public $title;
public $post;
public $author;
public $tags;
public $datePosted;

function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
{
if (!empty($inId))
{
	$this->id = $inId;
}
if (!empty($inTitle))
{
	$this->title = $inTitle;
}
if (!empty($inPost))
{
	$this->post = $inPost;
}

if (!empty($inDatePosted))
{
	$splitDate = explode("-", $inDatePosted);
	$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
}

if (!empty($inAuthorId))
{
	$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
	$row = mysql_fetch_assoc($query);
	$this->author = $row["first_name"] . " " . $row["last_name"];
}

$postTags = "No Tags";
if (!empty($inId))
{
	$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
	$tagArray = array();
	$tagIDArray = array();
	while($row = mysql_fetch_assoc($query))
	{
		array_push($tagArray, $row["name"]);
		array_push($tagIDArray, $row["id"]);
	}
	if (sizeof($tagArray) > 0)
	{
		foreach ($tagArray as $tag)
		{
			if ($postTags == "No Tags")
			{
				$postTags = $tag;
			}
			else
			{
				$postTags = $postTags . ", " . $tag;
			}
		}
	}
}
$this->tags = $postTags;
}

}

?>

 

includes.php

 

<?php
include 'blogpost.php';
// Change this info so that it works with your system.
$connection = mysql_connect('localhost', 'username', 'pw') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");
$database = "DBName";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");


function GetBlogPosts($inId=null, $inTagId =null)
{
if (!empty($inId))
{
	$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC LIMIT 2"); 
}
else if (!empty($inTagId))
{
	$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
}
else
{
	$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
}

$postArray = array();
while ($row = mysql_fetch_assoc($query))
{
	$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
	array_push($postArray, $myPost);
}
return $postArray;
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.