一個checkbox 和一個 text 
是循環出來的數組,checkbox的name是txtpid[],text的name是txtaddress[]如果選擇checkbox會取到正确的值,但是填寫的text的值總是從第一個值取,並不是我選哪個checkbox它就會把相對應的text值取出來!
  以下是代码:
        這個是a.php        <form name=form1 method="post" action='b.php'>         <?php         $mysql5="select * from product where state!=1 ";
$myrs5=mysql_query($mysql5);
while($myrow5=mysql_fetch_array($myrs5))
         {         ?>
          <input type="checkbox" name="txtpid[]"      value=<? echo $myrow5['id'];?> />
          <input type="text"     name="txtaddress[]"  value=<? echo $myrow5['address'];?>  />
         <?
    
         }         ?>
           <input type="submit" value="提交">
        </form>////////////////////////////////////////////////////////
         b.php       <?php      
        $pid=$_POST['txtpid']; 
        $address=$_POST['txtaddress']; 
         $counts=count($pid);         for($i=0;$i<$counts;$i++)
{          echo  $pid[$i].":".$address[$i]."<br>";         }        ?>問題描述:如果全選checkbox,那麽遍歷出來的txtaddress是正確显示的;
         如果只选择几个checkbox,那么相对应的txtaddress就没取到,比如选择的checkbox是第2个,第5个和第8个;而txtaddress只取的是第1个,第2个和第3个,我现在就是想选择的checkbox把相对应的txtaddress也取到!不知讲明白没有!

解决方案 »

  1.   

    不好意思,再加一個題!
    php横向排列,就是说从数据库循环取出的数据可不可以按一行3个数据排,就像商品排列的效果一样,每行可以放3个图片,比如数据库有30条记录,就按10行,然后每行3个数据排列,多谢,会加分的!
      

  2.   


    <form name=form1 method="post" action='b.php'>
    <?php
        $mysql5="select * from product where state!=1 ";
        $myrs5=mysql_query($mysql5);
        $i=0;
        while($myrow5=mysql_fetch_array($myrs5)):?>
              <input type="checkbox" name="txtpid[<?php echo $i?>]" value=<? echo $myrow5['id'];?> />
              <input type="text"     name="txtaddress[<?php echo $i++ ?>]"  value=<? echo $myrow5['address'];?>  />
    <?php endwhile; ?>
               <input type="submit" value="提交">
    </form>////////////////////////////////////////////////////////
    b.php<?php
         $pid=$_POST['txtpid']; 
         $address=$_POST['txtaddress']; 
         while(list($key,$val)=each($pid)){
              echo  $val,":",$address[$key],"<br />";
         }
    ?>关键是使用下标将checkbox 和text关联起来,然后访问同一下标的的checkbox 和text,
    这样就不会 出现错误的情况了。
    第二个<?php for($i=1;$i<31;$i++){
    echo $i;
    echo $i%3==0?'<br />':',';//如果对3取模==0 输入换行,否则输出逗号
    }
    ?>
      

  3.   

    楼主正解
    第二个问题还可以这样:for ($i=0;$i<31;$i=$i+3)
    {
        echo $i;
        echo $i+1;
        echo $i+2;
        echo "<br />";    
    }
      

  4.   


    假如多加一個text呢,
    比如<input type="text" name="txtre[]" value=<? echo $myrow5['re']; ?>
      

  5.   

    <form name=form1 method="post" action='b.php'>
    <?php
        $mysql5="select * from product where state!=1 ";
        $myrs5=mysql_query($mysql5);
        $i=0;
        while($myrow5=mysql_fetch_array($myrs5))
        {  $i++;
        ?>
         <input type="checkbox" name="txtpid[<?=$i?>]" value=<? echo $myrow5['id'];?> />
         <input type="text"     name="txtaddress[<?=$i?>]"  value=<? echo $myrow5['address'];?>  />
         <input type="text"     name="txtre[<?=$i?>]"   value=<? echo $myrow5['re'];?>  />    <? } ?>
               <input type="submit" value="提交">
    </form>////////////////////////////////////////////////////////
    b.php<?php
         $pid=$_POST['txtpid']; 
         $address=$_POST['txtaddress']; 
         $re=$_POST['txtre'];     while(list($key,$val)=each($pid))
         {
              echo  $val.":".$address[$key].$re[$key]."<br />";
         }
    ?>改了下,多謝,己得到正確結果!