php - 随机选择动态复选框

标签 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>

最佳答案

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);

?>

关于php - 随机选择动态复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177083/

相关文章:

php - 多个页面上的多个按钮几乎执行相同的操作

javascript - 棘轮 push.js 与 phonegap (cordova)

javascript - 为什么 jQuery 事件在我使用由 jQuery 生成的类时不起作用

javascript - div 的关闭按钮,使用 Jquery 访问 ID

c# - 将 Ajax 数组发送到 Controller

php - 转义 mysql 搜索字符串 php

html - 为什么某些浏览器将格式化字符(如 CRLF)呈现为空格?

php - 错误的整数值 :error

javascript - 在 PHP 中的所有页面上包含 header

php - 使用 Codeigniter 和 MySQL 从同一个表获取对等列值