0

I am working on building a small php file. I need to select random check boxes. check boxes dynamic as they are pulled from a table with some conditions.

Please let me know if i can randomly select few check boxes say if i get 100 results from the mysql table, 10 random check box needs to be selected.

<?php
    $n1=0;
    $rambo = strip_tags(@$_POST["assoocc"]); 
    $r11 = strip_tags(@$_POST["ccdate"]);
    $r12 = strip_tags(@$_POST["rrdate"]);
    $submit = @$_POST["submit22"];
    ?>
    <body><div>
    <form action="selectedcc.php" method="post">
    <center>    <table width="600" border="1" cellpadding="1" cellspacing="1">
        <tr>
            <th colspan="3"> Total cases for QA</th>
            <th colspan="2"><?php
            $ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");

            $m1=mysql_num_rows($ticket);
            echo "$m1"; 
            ?></th>
            <th colspan="2"><select   name="assoocc1"  class="form-control" id="sel1" style="width: 150px;" >
                <?php echo "<option value=$rambo selected>".$rambo."</option>";
                ?>
            </select></th>
           <th colspan="1"><input type="submit" name="sub" value="Submit For QA "/></th>

        </tr>
        <tr>
            <th>Select Here</th>
            <th>Case Id</th>
            <th>Task</th>
            <th>Number of ASINs</th>
            <th>SLA Details</th>
            <th>Create date</th>
            <th>Resolved date</th>
            <th>Rootcause</th></tr>
        <?php
        if ($submit)
    {
    if ($rambo&&$r11&&$r12)
    {
        if ($r11<$r12)
        {
            $ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");
            $m1=mysql_num_rows($ticket);
            for($k=0; $k<$m1; $k++)
            {
                $caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
                ${"caseid".$k}=mysql_result($ticket,$k, "case_id");
                ${"task".$k}=mysql_result($ticket,$k, "sub_issue");
                ${"asin".$k}=mysql_result($ticket,$k, "asin");
                ${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
                ${"create_date".$k}=mysql_result($ticket,$k, "create_date");
                ${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
                ${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
                ${"associate".$k}=mysql_result($ticket,$k, "associate");
echo "<tr>"."<th>"."<input type='checkbox' value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>";
            }
        }
        else
        {
            echo "<script>alert('Error:START DATE should be less than END DATE. Go back to QA Home page and enter all fields')</script>";
        }
    }
    else
    {
        echo "<script>alert('Error:Go back to QA Home page and enter all fields')</script>";
        }
    }
        ?>
    </table></center></form></div></body></html>
Subin Thomas
  • 1,408
  • 10
  • 19
DJ RAXITH
  • 13
  • 2

1 Answers1

0

Took the random function UniqueRandomNumbersWithinRange from Generating UNIQUE Random Numbers within a range - PHP

<?php

function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    $arr  = array_slice($numbers, 0, $quantity);
    sort($arr);
    return $arr;
}

$chkArr = array();
$rand = UniqueRandomNumbersWithinRange(0, 100, 10);
$rIdx = 0;

for($k=0; $k<100; $k++){
    $caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
    ${"caseid".$k}=mysql_result($ticket,$k, "case_id");
    ${"task".$k}=mysql_result($ticket,$k, "sub_issue");
    ${"asin".$k}=mysql_result($ticket,$k, "asin");
    ${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
    ${"create_date".$k}=mysql_result($ticket,$k, "create_date");
    ${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
    ${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
    ${"associate".$k}=mysql_result($ticket,$k, "associate");

    $chk = '';
    if( in_array($k, $rand) )
        $chk = "checked";

    array_push($chkArr, "<tr>"."<th>"."<input type='checkbox' ".$chk ." value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>");
}
echo implode($chkArr);

?>
Community
  • 1
  • 1
Lauwrentius
  • 503
  • 5
  • 12