samedi 2 mai 2015

How to use random for non-duplicate results of a query

I'm using CodeIgniter. I have a query in model and I'm using:

 $this->db->order_by('question_id', 'RANDOM');

But my query returns duplicate question_id. I want to show me all questions and they have to be random and not duplicate. I tried with:

  $this->db->where_not_in('question_id', $already_used);

But how to get $already_used - these questions that user has already answered, not to show again using random? My model is:

 public function getSurvey($survey, $question = 0,$already_used = array(0))
    {

        $this->db->select('*');
        $this->db->from('survey_questions');
        $this->db->where('(deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL) ');
        $this->db->where('survey_id', $survey);
        if ($question) {
            $this->db->where('question_id', $question);
        }
        $this->db->order_by('question_id', 'RANDOM');
        $this->db->where_not_in('question_id', $already_used);
        $this->db->limit(1);
        return $this->db->get();
    }

My controller is:

 public function survey_show()
    {

        $data['dynamic_view'] = 'survey_show';        
       $query = $this->user_model->getSurvey($this->uri->segment(3),        $this->uri->segment(4),$already);
        $data['question'] = $query->result();
       
        $this->load->view('templates/main', $data);
}

My view is:

$(function () {
        $( document ).ready(hideAllRows);
         $( document ).ready(showFirst);        
    });

function hideAllRows(){
        $('#questionsTable tr').each(function() {
            $(this).hide();

        });
}

$(document).ready(function() {
    $('.answer').click(function() {
        $(this).attr('checked', true);
    })
    
    var current = 1;
    $('#next').click(function(e) {

        var valid = null;

        $('#' + current).find('input[type="radio"]').each(function(i, v) {
            if ($(v).attr('checked') !== undefined) {
                // Validation passed
                $('#' + current).css({ display: 'none' });
                 //already_used += $('#' + current); 
                current++;
                valid++;

                $('#' + current).css({ display: 'block' });
            }
        });

        if (!valid) {
            alert('Please, answer!');
        }
        if (current == $('input[name="questions_count"]').val()) {
                $('#next').attr('value', 'Submit');
                }
        if (current <= $('input[name="questions_count"]').val()) {
            e.preventDefault();
        }
    })
    
})
function showFirst(){
        $(function () {
        $('#1').show();
});
}
<?php
$survey_id = $this->uri->segment(3);
echo form_open();
echo validation_errors();
$index = 1;
        foreach ($question as $row)
        {
                echo "<tr id='$index'>";
                $index++;
        ?>
                
           <?php echo        "<td>";
           
                 echo "$row->question"; ?><br/>
                <?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
                <?php 
                 
                $data=array(
                        'name' =>  'answer['.$row->question_id.']',
                        'value' => '5',
                        'class' => 'answer'

                );

                echo "<input type='hidden' name='survey_id' value='$row->survey_id'>"; 
                
                echo form_radio($data); 
                
                $data=array(
                        'name' =>  'answer['.$row->question_id.']',
                        'value' => '4',
                        'class' => 'answer'
                );
                echo form_radio($data);
 <?php  echo '<input type="submit" id="next" name = "submit" value="Next" class="btn btn-success">'; ?>
</form>



Aucun commentaire:

Enregistrer un commentaire