duanan1946 2010-12-07 17:36
浏览 23
已采纳

解析在PHP中生成$ _post的循环?

I am sending the results of a loop by post to a php file for processing (insertion into db). But I'm not sure how to get the results out in the most efficient way. right now it's either 1. updating $_POST['name-id'] and $_POST['occupation-id] or 2. creating $_POST['new_name-#'] and $_POST['new_occupation-#] with a variable # starting at 1 and counting up.

I am stuck on an efficient way of pulling all that data out and transferring it to variables + how to separate the names from the new names

in php document one there is basically a 2 column table in a form that is method post and on submit goes to the other php file. code is a bit convoluted so wanted to hold off on putting it all up"

while ( $tracks = mysql_fetch_array($trackstring) ){
    echo "<li class=\"tracklist\"><input type=\"text\" name=\"tracknum-".$tracks['song_id']."\" id=\"tracknum-".$tracks['song_id']."\" value=\"".$tracks['song_tracknumber']."\"/>
    <input type=\"text\" name=\"trackname-".$tracks['song_id']."\" id=\"trackname-".$tracks['song_id']."\" value=\"".$tracks['song_title']."\"/></li>";
    }

for ($i = 0; $i < ( (mysql_num_rows($trackstring) < 1) ? 20 : 5 ); $i++){
//for($i=1; $i< $numtracks; $i++){
    $count++;
        echo "<li class=\"tracklist\"><input type=\"text\" name=\"newtracknum-".$count."\" id=\"newtracknum-".$count."\" value=\"\"/><input type=\"text\" name=\"newtrackname-".$count."\" id=\"newtrackname=".$count."\" value=\"\"/></li>";
        }

and I need to pull that out in the other php file and sort it so that I can take the trackname-# and tracknumber-# in the value to update the id# of the db. I feel like I might be able to get it working by passing some invisible things into the array

  • 写回答

1条回答 默认 最新

  • doushenyi9104 2010-12-07 17:45
    关注

    Is each loop iteration doing a seperate post (e.g. a fresh curl_exec() or whatever?) In that case, each request will be independent of all the others and you could just re-use the same field names for each one.

    If it's all within a request, then use a sort-of-feature of PHP and force it to create an array from the post data. If you append [] to the end of a field name, PHP will treat that field as an array and save the data as such while it's processing the post.

    e.g.:

    <input type="text" name="arr[]" value="hi" />
    <input type="text" name="arr[]" value="there" />
    

    when processed by PHP will result in:

    $_POST = array(
       'arr' => array(
            0 => 'hi',
            1 => 'there'
       )
    )
    

    You can even force subscripts if you so choose, just put them inside the [], e.g. name="arr[12]" and name="arr[42]" will give you

    $_POST = array(
       'arr' => array(
            12 => 'hi',
            42 => 'there'
       )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?