用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4720|回复: 7
收起左侧

[HELP] [php] post接收数组处理

[复制链接]
发表于 2011-11-15 22:56:45 | 显示全部楼层 |阅读模式
新手,所以问的问题比较低级。望解答。谢谢了。

HTML复制代码
<form action="" method="post"><input type="text" value="" name="text[]">
 <select name="cate[]">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select><br>
 
  <input type="text" value="" name="text[]">
  <select name="cate[]">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select><br>
 
  <input type="text" value="" name="text[]">
  <select name="cate[]">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select><br>
 
  <input type="text" value="" name="text[]">
  <select name="cate[]">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select>
  <input type="submit" name="submit" value="提交">
</form>
复制代码




希望输出(three没有,就不显示。同时 text归属到各自的cate下面):


 楼主| 发表于 2011-11-16 11:28:22 | 显示全部楼层

.....
发表于 2011-11-16 12:49:29 | 显示全部楼层
写了一大堆,我愣是没怎么看懂到底说的什么和要做什么……
发表于 2011-11-16 12:54:34 | 显示全部楼层
终于看明白了,我试试,成功的话告诉你
 楼主| 发表于 2011-11-16 13:02:30 | 显示全部楼层
好的。谢谢。cate 的数量是从数据库取出来的,不一定固定是3个
发表于 2011-11-16 13:42:00 | 显示全部楼层
由于我也是新手,所以写的麻烦了很多……大概方法就是这样的,代码你再优化下

php提交页面代码:
PHP复制代码
 
        $data = $_POST;
        $one = '';
        $two = '';
        $three = '';
        for ($i=0;$i<4;$i++)
        {
                if ($data['cate'][$i] == 'one')
                {
                        $one = $one.$i.',';
                }
                if ($data['cate'][$i] == 'two')
                {
                        $two = $two.$i.',';
                }
                if ($data['cate'][$i] == 'three')
                {
                        $three = $three.$i.',';
                }
        }
        $one = substr($one,0,strlen($one)-1);
        $two = substr($two,0,strlen($two)-1);
        $three = substr($three,0,strlen($three)-1);
        //echo $one."<br />".$two."<br />".$three."<br />";
        $one = explode(",",$one);
        $two = explode(",",$two);
        $three = explode(",",$three);
 
复制代码


HTML输出页面代码:
HTML复制代码
 
<table border="solid,1px">
    <tr>
        <td>one</td>
        <td>two</td>
        <td>three</td>
    </tr>
    <tr>
        <td><?=$data['text'][$one[0]];?></td>
        <td><?=$data['text'][$two[0]];?></td>
        <td><?=$data['text'][$three[0]];?></td>
    </tr>
    <tr>
        <td><?=$data['text'][$one[1]];?></td>
        <td><?=$data['text'][$two[1]];?></td>
        <td><?=$data['text'][$three[1]];?></td>
    </tr>
    <tr>
        <td><?=$data['text'][$one[2]];?></td>
        <td><?=$data['text'][$two[2]];?></td>
        <td><?=$data['text'][$three[2]];?></td>
    </tr>
    <tr>
        <td><?=$data['text'][$one[3]];?></td>
        <td><?=$data['text'][$two[3]];?></td>
        <td><?=$data['text'][$three[3]];?></td>
    </tr>
</table>
 
复制代码


发表于 2011-11-16 13:42:59 | 显示全部楼层
实现效果

实现效果

实现效果
 楼主| 发表于 2011-11-16 14:05:35 | 显示全部楼层
hehe

PHP复制代码
for($i=0;$i<=3;$i++){
        $postdata[$_POST['cate'][$i]][]=$_POST['text'][$i];
}
print_r($postdata);
复制代码

本版积分规则