webguync Posted January 7, 2011 Share Posted January 7, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/ Share on other sites More sharing options...
webguync Posted January 8, 2011 Author Share Posted January 8, 2011 any ideas on how I would accomplish what I described? Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/#findComment-1156714 Share on other sites More sharing options...
QuickOldCar Posted January 8, 2011 Share Posted January 8, 2011 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/#findComment-1156782 Share on other sites More sharing options...
webguync Posted January 8, 2011 Author Share Posted January 8, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/#findComment-1156844 Share on other sites More sharing options...
QuickOldCar Posted January 9, 2011 Share Posted January 9, 2011 Have an index or a main display page? You could just make a single.php page and include what you would like. Then just make a link to it using unique id's from the main pages. Is this some type of template system you designed on your own? Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/#findComment-1156948 Share on other sites More sharing options...
webguync Posted January 9, 2011 Author Share Posted January 9, 2011 yea, I do have a main page. Linking to an individual post would do the trick, but I am not sure how I would do that. I adapted the script from a tutorial I found online. Quote Link to comment https://forums.phpfreaks.com/topic/223718-make-a-blog-post-clickable-to-display-on-its-own-page/#findComment-1157085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.