Jump to content

Requesting certain post issue with GET


travisco87
Go to solution Solved by Ch0cu3r,

Recommended Posts

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. 

Link to comment
Share on other sites

The problem is when I run it it gives me this error 


( ! ) Notice: Undefined index: tagId in C:\wamp\www\Rachel\blog_tags.php on line 78
Call Stack
#	Time	Memory	Function	Location
1	0.0003	683256	{main}( )	..\blog_tags.php:0
I apologize about there does not seem to be any post with that related tag.
( ! ) Notice: Undefined variable: blogstmt in C:\wamp\www\Rachel\includes.php on line 93
Call Stack
#	Time	Memory	Function	Location
1	0.0003	683256	{main}( )	..\blog_tags.php:0
2	0.0022	771608	GetTaggedBlogPosts( )	..\blog_tags.php:78

( ! ) Fatal error: Call to a member function fetchAll() on a non-object in C:\wamp\www\Rachel\includes.php on line 93
Call Stack
#	Time	Memory	Function	Location
1	0.0003	683256	{main}( )	..\blog_tags.php:0
2	0.0022	771608	GetTaggedBlogPosts( )	..\blog_tags.php:78

I do not understand how it is getting the undefined index. I thought I was passing the url get method to this value, where am I going wrong?

Link to comment
Share on other sites

  • Solution

The uri is wrong for the tag link

echo '<li><a href="blog_tags.php?=tagId=' . $tagId . '"title="' . $tagName . '">' . $tagName . '(' . $tagCount . ')</a></li></form>';

Remove the = after blog_tags.php?

 

Correct format of query string is filename.php?varX=valueX&varY=valueY&... etc

Edited by Ch0cu3r
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.