1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

BY RAND()

Discussion in 'MySQL' started by deferrari, Dec 19, 2016.

  1. #2
    I have a great proofing system that I downloaded from the internet. And it's working perfectly here, but when the query is made to run the test, it always returns in the same order, could I change the query to ORDER BY RAND ()? If so where could I change it in the code? Thank you !!

    Project files with data Sql: (name of bank "simple-quiz")
    SEMrush


    <?php
    
    namespaceSimpleQuiz\Utils;
    
    useSimpleQuiz\Utils\Base\User;
    useSlim\Helper\Set;
    
    classQuizimplementsBase\IQuiz {
    
    protected$_id;
    protected$_name;
    protected$_description;
    protected$_category;
    protected$_active;
    protected$_answers=array();
    protected$_questions;
    protected$_question;
    protected$_users;
    protected$_leaderboard;
    
    /**
    *@paramSet $container
    */
    public function__construct(Set$container)
    {
    $this->_leaderboard=$container->leaderboard;
    $this->_questions=newQuestionStorage();
    }
    
    /**
    *@param$id
    *@returnbool
    */
    public functionsetId($id)
    {
    $quizobj= \ORM::for_table('quizzes')->join('categories',array('quizzes.category','=','categories.id'))->select_many('quizzes.name','quizzes.description',array('category'=>'categories.name'),'quizzes.active')->find_one($id);
    
    if($quizobj) {
    $this->_id=$id;
    $this->_name=$quizobj->name;
    $this->_description=$quizobj->description;
    $this->_category=$quizobj->category;
    $this->_active=$quizobj->active;
    
    return true;
    }
    
    return false;
    }
    
    /**
    *@returnint
    */
    public functiongetId()
    {
    return(int)$this->_id;
    }
    
    /**
    *@returnstring
    */
    public functiongetName()
    {
    returnucwords($this->_name);
    }
    
    /**
    *@returnmixed
    */
    public functiongetDescription()
    {
    return$this->_description;
    }
    
    /**
    *@returnbool
    */
    public functionisActive()
    {
    return$this->_active==1?true:false;
    }
    
    
    /**
    *@param$questionid
    *@returnbool
    */
    public functiongetAnswers($questionid)
    {
    if($questionid)
    {
    return$this->getQuestion($questionid)->getAnswers();
    }
    else{
    throw new\InvalidArgumentException("You must supply a question id");
    }
    
    return false;
    }
    
    /**
    *@returnarray
    */
    public functiongetAllAnswersGroupedByQuestion()
    {
    //pull all answers from db grouped by question
    $obj= \ORM::for_table('answers')->raw_query("SELECT group_concat( a.text ORDER BY a.correct DESC SEPARATOR '~' ) as grouped FROM answers a where a.quiz_id = :quizid GROUP BY a.question_num",array('quizid'=>$this->_id) )->find_array();
    foreach($objas$answers)
    {
    $answerarray=explode('~',$answers['grouped']);
    array_push($this->_answers,$answerarray);
    }
    
    return$this->_answers;
    }
    
    /**
    *@paramarray $answers
    *@param$questionid
    *@returnbool
    */
    public functionupdateAnswers(Array$answers,$questionid)
    {
    $this->getQuestion($questionid)->updateAnswers($answers);
    
    return true;
    }
    
    /**
    *@param$questionid
    *@returnbool
    */
    public functiondeleteAnswers($questionid)
    {
    $this->getQuestion($questionid)->deleteAnswers();
    return true;
    }
    
    /**
    *@param$questionid
    *@paramarray $answers
    *@returnbool
    */
    public functionaddAnswers($questionid,Array$answers)
    {
    $this->getQuestion($questionid)->addAnswers($answers);
    }
    
    /**
    *@param$text
    *@param$type
    *@paramarray $answers
    *@returnbool
    */
    public functionaddQuestion($text,$type,Array$answers)
    {
    $max= \ORM::for_table('questions')->where('quiz_id',$this->_id)->max('num');
    $num=$max+1;
    
    //insert new question
    $newquestion= \ORM::for_table('questions')->create(
    array(
    'num'=>$num,
    'quiz_id'=>$this->_id,
    'text'=>$text
    )
    );
    //save the new question in db then add to the question storage
    if($newquestion->save())
    {
    //create a question of desired type
    $questionType=__NAMESPACE__.'\\'.ucfirst($type) .'Question';
    //create a new Question instance
    $this->_question=new$questionType($newquestion->id(),$num,$this->_id,$text);
    $this->_question->addAnswers($answers);
    $this->_questions->attach($this->_question);
    
    return true;
    }
    
    return false;
    }
    
    /**
    *@param$questionid
    *@param$text
    *@returnbool
    */
    public functionupdateQuestion($questionid,$text)
    {
    $this->getQuestion($questionid)->update($text);
    
    return true;
    }
    
    /**
    *@param$questionid
    *@returnbool
    */
    public functiondeleteQuestion($questionid)
    {
    //foreign_key constraints take care of deleting related answers
    $q= \ORM::for_table('questions')->where('quiz_id',$this->_id)->where('num',$questionid)->find_one();
    $q->delete();
    
    //reorder the num column in questions table
    //foreign_key constraints take care of updating related answers
    $toupdate= \ORM::for_table('questions')->where('quiz_id',$this->_id)->where_gt('num',$questionid)->find_many();
    foreach($toupdateas$question) {
    $question->num=$question->num-1;
    }
    
    return$toupdate->save();
    }
    
    /**
    *@param$questionid
    *@returnbool|mixed|object
    */
    public functiongetQuestion($questionid)
    {
    return$this->_questions->getById($questionid);
    }
    
    public functiongetQuestions()
    {
    return$this->_questions;
    }
    
    /**
    *@returnint
    */
    public functioncountQuestions()
    {
    returncount($this->_questions);
    }
    
    /**
    *@returnmixed
    */
    public functiongetCategory()
    {
    return$this->_category;
    }
    
    
    /**
    *@return$this
    */
    public functionpopulateQuestions()
    {
    $quizquestions= \ORM::for_table('questions')->where('quiz_id',$this->_id)->order_by_asc('num')->find_array();
    
    foreach($quizquestionsas$question)
    {
    /**
    *@todo make the instance name dynamic
    */
    $questionObject=newRadioQuestion($question['id'],$question['num'],$this->_id,$question['text']);
    $this->_questions->attach($questionObject);
    }
    
    return$this;
    }
    
    //following 2 methods to be combined
    /**
    *
    */
    public functionpopulateUsers()
    {
    $this->_users=$this->_leaderboard->getMembers($this->_id);
    }
    
    /**
    *@returnmixed
    */
    public functiongetUsers()
    {
    return$this->_users;
    }
    
    /**
    *@param$num
    *@returnmixed
    */
    public functiongetLeaders($num)
    {
    return$this->_leaderboard->getMembers($this->_id,$num);
    }
    
    /**
    *@paramUser $user
    *@param$score
    *@param$start
    *@param$end
    *@param$timetaken
    *@returnbool
    */
    public functionaddQuizTaker(User$user,$score,$start,$end,$timetaken)
    {
    return$this->_leaderboard->addMember($this->_id,$user,$score,$start,$end,$timetaken);
    }
    }
    Code (markup):
     
    Last edited by a moderator: Dec 29, 2016
    deferrari, Dec 19, 2016 IP
    SEMrush
  2. SpacePhoenix

    SpacePhoenix Active Member

    Messages:
    58
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    60
    #3
    Try and avoid the use of ORDER BY RAND() if there's going to be lots of entries as it doesn't scale to well. If you're going to use all questions every time, consider the use of PHP's shuffle function on the array returned by the results set.
     
    SpacePhoenix, Dec 29, 2016 IP