I want to be able to shuffle questions on each click of the shuffle button

90 views Asked by At

HTML

<form name="form1" method="post" action="">
<table width="254" border="0" align="center">
    <tr>
        <td width="185" height="5"><p align="left">
                <label for="textfield"></label>
                <?
                $sqlretrive = mysql_query("Select * from tb_classes");
                $recordcount =  mysql_affected_rows(); 
                ?>
                <select name="classname" id="staffid2" class="form-control" required >
                    <option value="">Select Class Name</option>
                    <? while ($row = mysql_fetch_array($sqlretrive)){ ?>
                    <option value="<?= $row['id'] ?>">
                        <?= $row['classname'] ?>
                    </option>
                    <? }?>
                </select>
            </p></td>
    </tr>
    <tr>
        <td height="1">
            <?
            $sqlretrive = mysql_query("Select * from tb_subjects_reg where status =1");
            $recordcount =  mysql_affected_rows(); 
            ?>
            <select name="subjectname" id="select" class="form-control" required>
                <option value="">Select Subject</option>
                <? while ($row = mysql_fetch_array($sqlretrive)){ ?>
                <option value="<?= $row['id'] ?>">
                    <?= $row['subjectname'] ?>
                </option>
                <? }?>
                <label for="subjectname">
                </label>
                <select name="subjectname" id="subjectname">
                </select></td>
    </tr>
    <tr>
        <td height="2"><div align="right">
                <input type="submit" name="find" id="find" value="Find">
            </div></td>
    </tr>
</table>

<table  class="table toggle-square" data-filter="#table_search" width="541" border="0" align="center">
    <tr align="center" valign="bottom" bgcolor="#FBA774">
        <td colspan="4"><input type="submit" name="shuffle" id="shuffle" value="SHUFFLE QUESTIONS" class="form-control"></td>
    </tr>
    <tr align="center" valign="bottom" bgcolor="#FBA774">
        <td width="182"><div align="center">Question Number</div></td>
        <td align="center" valign="bottom"><div align="center">Question</div></td>
        <td align="center" valign="bottom"><div align="center"></div></td>
        <td align="center" valign="bottom"><div align="center"></div></td>
    </tr>
    <?
    $sqlretrive = mysql_query("Select * from tb_addquestion");
    $recordcount =  mysql_affected_rows();
    $i=0;
    while ($row = mysql_fetch_array($sqldisplayresult)){
    $i++
    ?>
    <tr>
        <td><div align="center">
                <?= $i ?>
            </div></td>
        <td width="183"><div align="center"><a href="viewquestion.php?subject=<?= $row['subject'] ?>&class=<?= $row['class'] ?>&number=<?= $row['question_number'] ?>"></a>
                <?= $row['question'] ?>
            </div></td>
        <td width="100"><div align="center"><a href="viewquestion.php?subject=<?= $row['subject'] ?>&class=<?= $row['class'] ?>&number=<?= $row['question_number'] ?>&id=<?= $row['id'] ?>">View Question</a></div></td>
        <td width="58"><div align="center"><a href="viewquestion.php?subject=<?= $row['subject'] ?>&class=<?= $row['class'] ?>&number=<?= $row['question_number'] ?>"><img src="img/edit.png" alt="" />Edit </a></div></td>

    </tr>
    <?   } ?>
    <a href="viewquestion.php?subject=<?= $row['subject'] ?>&class=<?= $row['class'] ?>&number=<?= $row['question_number'] ?>"></a></td>

    <tr>
        <td colspan="4">
            <div align="right"></div>
        </td>
    </tr>

</table>
<p>&nbsp;</p>

PHP

<? include('inc/header.php');
     ?>

    <? 
    include("connect.php"); 
    ?>
    <?
    if (isset($_POST['shuffle'])) {
        $classname = $_POST['classname'];
        $subjectname = $_POST['subjectname'];
        $sqldisplayresult = mysql_query("Select * from tb_addquestion WHERE class = '$classname' and subject='$subjectname' and status = 1 ORDER BY RAND()");
    }
    //display score for the specified class and subject in the current or specified session and term
    if (isset($_POST['find'])) {
        $classname = $_POST['classname'];
        $subjectname = $_POST['subjectname'];

    $sqldisplayresult = mysql_query("Select * from tb_addquestion WHERE class = '$classname' and subject='$subjectname' ");
    }

    ?>

After I have filtered with class and subject, it shows a list of the questions.

I want my Shuffle button to work in a way that once I click on it , it automatically reshuffles and select the questions randomly from the database.

1

There are 1 answers

0
Nat Naydenova On

So why not using ORDER BY RAND() the way you used it in the first request?

Another way could be to use the shuffle() method in PHP after you've requested your data from the database.

In any case, if you want the shuffle to be dynamic you'll have to use Ajax, maybe with jQuery's .ajax() method it'll be easier