Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
2.0k views
in Q2A Core by
edited by

I had this problem couple of times and have not found a good solution yet:

I have the postid and can get the type (Q | A | C) from the database (is there a handy q2a-function for this as well?).

Now I need the direct link to question, answer or comment.

To do the question is easy: mysite.com/postid → done

But how to get the links to answer and comment only from postid?
I.e. I search for the function that accesses the database, gets parentid, and builds the correct link.

by
I just had this exact question myself! For questions it's quite easy - get the postid and the title and call:
    $q_url = qa_q_path_html( $post['postid'], $post['title'] );

I'm trying to figure out how it's done for answers/comments in the Q2A code but it's hard following it...
by
exactly, as arrays are passed. I checked c_list ... in qa-theme-base.php.

Would be great to have two q2a-functions available:
qa_get_link_to_comment($postid_c);
qa_get_link_to_answer($postid_a);

1 Answer

+1 vote
by
edited by

This code is part of my list image uploads plugin, v0.3, and shows how I am doing it now:

$imageExistsQuery = qa_db_query_sub("SELECT postid,type,parentid FROM `^posts` WHERE `content` LIKE '%".$blobrow['blobid']."%' LIMIT 1");
$imageInPost = mysql_fetch_array($imageExistsQuery);
$existsInPost = $imageInPost[0];
// $existsInPost = ($existsInPost=="") ? $notFoundString : "";

// set link to question, answer, comment that contains the image
if($existsInPost=="") {
    $existsInPost =  $notFoundString;
}
else if($imageInPost[1]=="A") {
    $existsInPost = "<a href='".$imageInPost[2]."?show=".$imageInPost[0]."#a".$imageInPost[0]."'>&rarr; in answer: ".$existsInPost."</a>";
}
else if($imageInPost[1]=="C") {
    // get question link from answer
    $getQlink = mysql_fetch_array( qa_db_query_sub("SELECT parentid,type FROM `^posts` WHERE `postid` = ".$imageInPost[2]." LIMIT 1") );
    $linkToQuestion = $getQlink[0];
    if($getQlink[1]=="A") {
        $existsInPost = "<a href='".$linkToQuestion."?show=".$imageInPost[0]."#c".$imageInPost[0]."'>&rarr; in comment: ".$existsInPost."</a>";
    }
    else {
        // default: comment on question
        $existsInPost = "<a href='".$imageInPost[2]."?show=".$imageInPost[0]."#c".$imageInPost[0]."'>&rarr; in comment: ".$existsInPost."</a>";
    }
}
else {
    // default: question
    $existsInPost = "<a href='".$existsInPost."'>&rarr; in question: ".$imageInPost[0]."</a>";
}

I am sure this could be done much easier with q2a functions!

 

-- Here is how NoahY is doing it in his q2a-history-plugin:

if(strpos($post['type'],'Q') !== 0) {
    $anchor = qa_anchor((strpos($post['type'],'A') === 0 ?'A':'C'), $params['postid']);
    $parent = qa_db_read_one_assoc(
        qa_db_query_sub(
            'SELECT parentid,type,BINARY title as title,postid FROM ^posts WHERE postid=#',
            $post['parentid']
        ),
        true
    );
    if($parent['type'] == 'A') {
        $parent = qa_db_read_one_assoc(
            qa_db_query_sub(
                'SELECT BINARY title as title,postid FROM ^posts WHERE postid=#',
                $parent['parentid']
            ),
            true
        );                    
    }                        
    $activity_url = qa_path_html(qa_q_request($parent['postid'], $parent['title']), null, qa_opt('site_url'),null,$anchor);
    $link = '<a href="'.$activity_url.'">'.$parent['title'].'</a>';                                    
}
else {
    $activity_url = qa_path_html(qa_q_request($params['postid'], $post['title']), null, qa_opt('site_url'),null,null);
    $link = '<a href="'.$activity_url.'">'.$post['title'].'</a>';                                    
}
 

...