使用Checkbox循环PHP

时间:2014-02-18 04:47:21

标签: php for-loop

请帮助我。

我有form=myform喜欢这个

<form name='myform' method='post' action='update.php'>  
                  <div id='add-new-button' style='padding:9px;'>

                  <input type='submit' name='pilih' value='Update' ></div>
                  <div class='tbl_sp'> 

          <table cellspacing='0' cellpadding='0' id='table-detail'><thead >
          <tr>
          <th>No</th>
          <th>Custname</th>
          </tr></thead><tbody>";
          $counter = 1;
$query= mssql_query("SELECT top 10 * from Customer ");

$f=1;
while ($r=mssql_fetch_array($query))
{
    if (is_float($counter/2)) {$class = "class='alt'"; } else { $class = ""; };

    echo"<tr $class>
<td><input type='hidden' name='data".$f."' value='".$r['No']."' />$r[tNo]</td>
<td>$r[CustName]</td>
</tr>


    "; $f++;
          $counter++;  
          }
echo"</tbody></table><input type='hidden' name='m' value='".$f."' /></div><div class='center'>
          <table cellspacing='0' cellpadding='0' id='table-detail'><thead >
          <tr>
          <th>Check</th>
          <th>Tools</th>
          </tr></thead><tbody>";
          $counter = 1;
$query= mssql_query("SELECT  * from tools");

$g=1;
while ($r=mssql_fetch_array($query))
{
    if (is_float($counter/2)) {$class = "class='alt'"; } else { $class = ""; };

    echo"<tr $class>
<td><input type='checkbox' name='id".$g."' value='".$r['tools']."' /></td>
<td>$r[tools]</td>
</tr>


    "; $g++;
          $counter++;  
          }";

</div>";
echo"</tbody></table></form><input type='hidden' name='n' value='".$g."' /> </div>";
}}

结果 enter image description here

表单操作update.php之后的代码如下

    $tools_total = 0;
$customer_total = 0;
foreach ($_POST as $key => $value) {

  if (strpos($key, 'data') === 0) {
    $customer_total++;            //getting total number of checked customers
  }
  if (strpos($key, 'id') === 0) {
    $tools_total++;               //getting total number of tools
  }
}

$k=1;
for($i=1; $i<=$customer_total; $i++)
{

    ($k>$tools_total)?$k=1:''; //reset tools counter

    //This if statement is not necessary, since I'm guessing
    //your $_POST['m'] and $_POST['n'] are giving you the total set values.
    if (isset($_POST['id'.$k]) && isset($_POST['data'.$i])){ 
        $data=$_POST['data'.$i];
        $ini=$_POST['id'.$k];
        echo " tools ='$ini'  and customer='$data' <br>\n";
    }

    $k++;
}

如何使用这样的结果进行编码。

tools ='AB'and Customer='1'     
tools ='AC'and Customer='2'     
tools ='AB'and Customer='3'

注意:

$_POST['m'] = count customer
$_POST['n'] = count tools

表客户(计数= 10)表工具(计数= 20) 描述: 如果我检查表工具 然后表客户将设置为检查的表工具。 例: 我检查工具AB和AC 结果必须是:

tools AB and customer 1 
tools AC and customer 2 
tools AB and customer 3 
tools AC and customer 4 
tools AB and customer 5 
tools AC and customer 6
tools AB and customer 7
tools AC and customer 8 
tools AB and customer 9 
tools AC and customer 10 

1 个答案:

答案 0 :(得分:0)

尝试:

Check Edited Post Below
/*
$tools_total = 0;
$customer_total = 0;

foreach ($_POST as $key => $value) {
  if (strpos($key, 'data') === 0) {
    $customer_total++;            //getting total number of checked customers
  }
  if (strpos($key, 'id') === 0) {
    $tools_total++;               //getting total number of checked tools
  }
}

$k=1;
for($i=1; $i<=$customer_total; $i++)
{

    ($k>$tools_total)?$k=1:''; //reset tools counter

    if (isset($_POST['id'.$k]) && isset($_POST['data'.$i])){ 
        $data=$_POST['data'.$i];
        $ini=$_POST['id'.$k];
        echo " tools ='$ini'  and customer='$data' <br>\n";
    }

    $k++;
}
*/

编辑后:

将您的数据输入标记的HTML更改为:

<input type='hidden' name='data[]' value='".$r['No']."' />

将您的id输入标记的HTML更改为:

<input type='checkbox' name='id[]' value='".$r['tools']."' />

现在您的PHP代码:

$tools_total = count($_POST['id']);

$k=0;
foreach($_POST['data'] as $key=>$val){
    ($k>($tools_total-1))?$k=0:''; //reset tools counter
    $data=$val;
    $ini=$_POST['id'][$k];
    echo " tools ='$ini'  and customer='$data' <br>\n";
    $k++;
}