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]."'>→ 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]."'>→ in comment: ".$existsInPost."</a>";
}
else {
// default: comment on question
$existsInPost = "<a href='".$imageInPost[2]."?show=".$imageInPost[0]."#c".$imageInPost[0]."'>→ in comment: ".$existsInPost."</a>";
}
}
else {
// default: question
$existsInPost = "<a href='".$existsInPost."'>→ 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>';
}