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

1 Answer

+1 vote
by
selected by
 
Best answer

I assume that you keep overriding the q_list function in your theme. I won't mess up with that function again. And suppose you only want the latest answer if any.

        public function q_item_content($q_item)
    {
                if (!empty($q_item['content'])) {
            $this->output('<div class="qa-q-item-content">');
            $this->output_raw($q_item['content']);
            $this->output('</div>');
        }
         //answers              
$zeeshan = $q_item['raw']['postid'];
$answers = qa_db_read_one_assoc(qa_db_query_sub(
        'SELECT postid, content, userid FROM ^posts WHERE parentid=$ AND type = "A" ORDER BY postid DESC LIMIT 1',
        $zeeshan
    ), true);
if (!empty($answers)){   
$this->output('Last answer: <br/>');

$shortanswer = qa_shorten_string_line(strip_tags($answers['content']) ,300);

$this->output(qa_html($shortanswer));
}
//answers

    }

If you want more information, you may customize the MySQL query or may need to do an extra one.

by
How do you guys write this type of code. I already done php course but still I can't write this type of php code. Thanks by the way.
by
edited by
+4
Q2A software is a collection of functions that arranges data nested in arrays.

WHERE: So when you say you wanted to show on question lists, I know you may need to modify q_list function or any sub-functions of its.

WHAT: You want to show answer(s), so I know I have to pull the data in qa_posts table where the type of post is Answer (A).

HOW:  Technically, the connexion between a question and its answers is that the question's postid is the parentid of the the answers.  So, first, I get the postid ($zeeshan = $q_item['raw']['postid']; ), and then do the MySQL.

RESOURCES: Normally, you have to write pure PHP code to connect the database, etc. You have to bear all the risks of script injection or SQL injection, so instead take advantage of public Q2A functions. qa_db_read_one_assoc is one of the Q2A functions to prepare data in an array  after doing MySQL queries, qa_db_query_sub is to create a database connection and input the parameterized MySQL query, which helps prevent sql injection. qa_shorten_string_line is a Q2A function to shorten strings. strip_tags is a PHP function to remove <> tags, for fear that some assholes may insert <script> tags. Because we SELECT postid, content, userid, the qa_db_read_one_assoc function returns the needed info in $answers['postid'], $answers['content'], $answers['userid']. When in doubt, use print_r($answers);
...