Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
156 views
in Q2A Core by
edited by

When starting the development of plugins, I ran often into problems how to get the link to a comment or answer, or even the correct link to the question.

As there is still no core function - at least I never found it - this is what I am using to get all correct links set up:

while ( ($datarow = qa_db_read_one_assoc($mySqlQuery,true)) !== null ) {
    // query recent user
    $currentUser = $datarow['userid'];
    $userrow = qa_db_select_with_pending( qa_db_user_account_selectspec($currentUser, true) );

    // get link to post from postid
    $linkToPost = "";
    $postid = $datarow['postid'];
    $getPostType = mysql_fetch_array( qa_db_query_sub("SELECT type,parentid FROM `^posts` WHERE `postid` = ".$postid) );
    $postType = $getPostType[0]; // type, and $getPostType[1] is parentid
    if($postType=="A") {
        $getQtitle = mysql_fetch_array( qa_db_query_sub("SELECT title FROM `^posts` WHERE `postid` = ".$getPostType[1]." LIMIT 1") );
        $qTitle = (isset($getQtitle[0])) ? $getQtitle[0] : "";
        // get correct public URL
        $activity_url = qa_path_html(qa_q_request($getPostType[1], $qTitle), null, qa_opt('site_url'), null, null);
        $linkToPost = $activity_url."?show=".$postid."#a".$postid;
    }
    else if($postType=="C") {
        // get question link from answer
        $getQlink = mysql_fetch_array( qa_db_query_sub("SELECT parentid,type FROM `^posts` WHERE `postid` = '".$getPostType[1]."' LIMIT 1") );
        $linkToQuestion = $getQlink[0];
        if($getQlink[1]=="A") {
            $getQtitle = mysql_fetch_array( qa_db_query_sub("SELECT title FROM `^posts` WHERE `postid` = '".$getQlink[0]."' LIMIT 1") );
            $qTitle = (isset($getQtitle[0])) ? $getQtitle[0] : "";
            // get correct public URL
            $activity_url = qa_path_html(qa_q_request($linkToQuestion, $qTitle), null, qa_opt('site_url'), null, null);
            $linkToPost = $activity_url."?show=".$postid."#c".$postid;
        }
        else {
            // default: comment on question
            $getQtitle = mysql_fetch_array( qa_db_query_sub("SELECT title FROM `^posts` WHERE `postid` = '".$getPostType[1]."' LIMIT 1") );
            $qTitle = (isset($getQtitle[0])) ? $getQtitle[0] : "";
            // get correct public URL
            $activity_url = qa_path_html(qa_q_request($getPostType[1], $qTitle), null, qa_opt('site_url'), null, null);
            $linkToPost = $activity_url."?show=".$postid."#c".$postid;
        }
    }
    else {
        // question has correct postid to link
        $getQtitle = mysql_fetch_array( qa_db_query_sub("SELECT title FROM `^posts` WHERE `postid` = ".$postid." LIMIT 1") );
        $qTitle = (isset($getQtitle[0])) ? $getQtitle[0] : "";
        // get correct public URL
        $activity_url = qa_path_html(qa_q_request($postid, $qTitle), null, qa_opt('site_url'), null, null);
        $linkToPost = $activity_url;
    }
}

I would love to see one or two core functions that reduce this bunch of code to only a few lines!

Something simple that I only pass two information to:  get_link_to_post($postid,$postType)

Greeetings,
Kai

 

Q2A version: 1.5.3

Please log in or register to answer this question.

...