随机选择动态复选框

时间:2015-10-16 18:17:01

标签: php jquery html mysql algorithm

我正在构建一个小的php文件。我需要选择随机复选框。复选框是动态的,因为它们是从具有某些条件的表中拉出来的。

如果我可以随机选择几个复选框,请告诉我如果我从mysql表中获得100个结果,则需要选择10个随机复选框。

<?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>

1 个答案:

答案 0 :(得分:0)

Generating UNIQUE Random Numbers within a range - PHP

获取随机函数 UniqueRandomNumbersWithinRange
<?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);

?>