Hello All!
I am having an issue with requesting some information for certain blog post. I have written a blog in PHP and have come accross an issue when trying to look up certain articles with certain "Tags". Right now the system pulls up and counts the tag's correctly but when I am trying to click the name of the tag to view post only related to that tag, it keeps giving me trouble. Here is the code I have been using for sending and getting the information. First is the function I am using to count the tags and create a link to use
function getTagCount($DBH) { //Make the connection and grab all the tag's TAG TABLE HAS TWO FIELDS id and name $stmt = $DBH->query("SELECT * FROM tags"); $stmt->execute(); //For each row pulled do the following foreach ($stmt->fetchAll() as $row){ //set the tagId and tagName to the id and name fields from the tags table $tagId = $row['id']; $tagName = ucfirst($row['name']); //Next grab the list of used tags BLOG_POST_TAGS TABLE HAS TWO FILEDS blog_post_id and tag_id $stmt2 = $DBH->query("SELECT count(*) FROM blog_post_tags WHERE tag_id = " . $tagId); $stmt2->execute(); $tagCount = $stmt2->fetchColumn(); //Print the following list echo '<li><a href="blog_tags.php?=tagId=' . $tagId . '"title="' . $tagName . '">' . $tagName . '(' . $tagCount . ')</a></li></form>'; //End of loop - start again } }
Now here is the code I am using on the page it is being directed too(blog_tags.php)
<?php include "includes.php"; $blogPosts = GetTaggedBlogPosts($_GET['tagId'],$DBH); foreach ($blogPosts as $post) { echo "<div class='post'>"; echo "<h2>" . $post->title . "</h2>"; $body = substr($post->post, 0, 300); echo "<p>" . nl2br($body) . "... <a href='post_view.php?id=" . $post->id . "'>View Full Post</a><br /></p>"; echo "<span class='footer'><strong>Posted By:</strong> " . $post->author . " <strong>Posted On:</strong> " . $post->datePosted . " <strong>Tags:</strong> " . $post->tags . "</span><br />"; echo "</div>"; } ?>
Here is also the function I am using
function GetTaggedBlogPosts($postTags, $DBH) { if (!empty($postTags)) { $postTagsIDsql = "SELECT blog_post_id FROM blog_post_tags WHERE tag_id = :postTagID"; $postTagIDparam = array( ':postTagID' => $postTags ); $postTagIDstmt = $DBH->prepare($postTagsIDsql); $postTagIDstmt->execute($postTagIDparam); $blogstmt = $DBH->prepare("SELECT * FROM blog_post WHERE id = :blog_id ORDER BY id DESC"); $blogstmt->bindParam(":blog_id", $postTagsIDstmt, PDO::PARAM_INT); $blogstmt->execute(); } else { echo 'I apologize about there does not seem to be any post with that related tag.'; } $postArray = array(); $results = $blogstmt->fetchAll(PDO::FETCH_ASSOC); foreach($results as $row){ $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['author_id'], $row['date_posted'], $DBH); array_push($postArray, $myPost); } return $postArray; }
I am just looking for a point in the right direction and to point out where my break down is happening. Thanks in advanced for your time, learning PHP on my own has been a chore but you all have helped make learning it so much easier.