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

this plugin show only one question but I needed multiple questions (about 6) so I have changed the code like this

edited code 

<?php

class q2a_random_question {

    function allow_template($template) {

        $allow = false;

        switch ($template) {

            case 'account':

            case'activity':

            case'admin':

            case'ask' :

            case'categories' :

            case'custom' :

            case'favorites':

            case'feedback' :

            case'hot' :

            case'ip' :

            case'login':

            case'message':

            case'qa' :

            case'question':

            case'questions':

            case'register' :

            case'search' :

            case'tag' :

            case'tags' :

            case'unanswered':

            case'updates' :

            case'user' :

            case'users' :

                $allow = true;

                break;

        }

        return $allow;

    }

    function allow_region($region) {

        return true;

    }

    function output_widget() {

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );

echo "<h2 style='text-align: left;'>Can you answer this questions ?</h2>";

//replace yourwebsite with your website

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );           

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );           

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";           

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );           

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";

$random_question = qa_db_read_one_assoc( 

qa_db_query_sub('SELECT * FROM ^posts WHERE type=$ 

ORDER BY rand() 

LIMIT 1', 

'Q'), 

true );           

          echo "<h4 style='text-align: left;'><a href='https://electronics2electrical.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></h4>";           

}

}

But I think by doing this speed of my site became slightly slow..

this code is working but correct me if I did wrong ?

Q2A version: 1.8
by
I am not sure but did you try
SELECT * FROM ^posts WHERE type=$

ORDER BY rand()

LIMIT 6
by
yes.. it didn't work

1 Answer

+1 vote
by
It’s slow for two reasons. The first is because you’re doing 6 queries when you only need 1. Put “LIMIT 6” instead of “LIMIT 1” then use qa_db_read_all_assoc and loop through the results.

The second reason is that “ORDER BY rand()” itself is quite slow, because it has to fetch every question in the database in order to randomise them, before taking just 6 of them. It may be fast enough if you’re just doing it once per page.

There are various ways to speed up random queries. If you were only selecting one question like the original code then you could use PHP to pick a random number between 1 and the latest question, and do

SELECT * FROM ^posts WHERE postid > $ LIMIT 1

Another option would be to generate a list of all the question IDs and put an array in your code like $questions = array(1, 4, 9, 123);

Then choose 6 at random in PHP and select those from the DB.

Or, if it doesn’t have to be completely random on every page load, cache the output of the widget for some time, maybe a day.
by
scott I am not expert like you.
from your optimisation
can you please give me that code ? I will paste it.
...