dongmeiran609914 2012-08-17 17:34
浏览 25
已采纳

为什么我的数组只返回最后一行

I have searched the site, but can not find the answer or how it would relate to how I have my query set up. Here is my file that processing my post data and passes to db. I have form that is wrapped around a table. The cells in the table are populated by an array. When I leave this page I want the values in the table cells written to a new table in the db. Each row in the table represents a separate row in the db.

Here is my form where all my post data is coming from.

    <form action="post_movement.php?class_id=<?php echo $class_id; ?>&pagenum=<?php echo $pagenum; ?>" method="post">
     <table border=1 width=800px>
 <tr>
 <th width=300px>Client</th>
 <th width=50px>Order</th>
 <th width=200px>Movements</th>
 <th>Sets/Reps/Seconds</th>  
 <th width=150px>Rest</th>
 <th>X Completed</th>
 </tr>

<?php

// Get workout class and output in table format -------------------------------------------------

if(empty($workout_class) === false)
{   
foreach($workout_class as $wc){
if ($wc['pagenum'] !== $pagenum) continue 1;

    echo '<tr>
        <td><input type="hidden" name="first_name[]" value="'.($wc['first_name']).'">'. ($wc['first_name']).'
        <span><input type="hidden" name="nickname[]" value="'.($wc['nickname']).'">('. ($wc['nickname']).')</span>
        <span><input type="hidden" name="last_name[]" value="'.($wc['last_name']).'">'. ($wc['last_name']).'</span>
        </td>
              <td><input type="hidden" name="order[]" value="'.($wc['order']).'">'. ($wc['order']). '</td>
              <td>
              <select name="movement[]" width=200>
              <option>'. ($wc['mv_00']). '</option>
              <option>'. ($wc['mv_01']). '</option>
              <option>'. ($wc['mv_02']). '</option>
              <option>'. ($wc['mv_03']). '</option>
              <option>'. ($wc['mv_04']). '</option>
        </select></td>
        <td><input type="hidden" name="rep_set_sec[]" value="'.($wc['rep_set_sec']).'">'. ($wc['rep_set_sec']). '</td>
        <td><input type="hidden" name="rest[]" value="'.($wc['rest']).'">'. ($wc['rest']). '</td>
        <td>00</td>
        </tr>';
  } // foreach($data_array
    //print_r($data_array);
} // if(!empty

?>

<! End Table ---------------------------------------------------------------------------------->
</table>
<input type="submit" value="Complete Movement">

</form>

Here is my php that process the post data from the form and submits it to the query.

    if (empty($_POST)=== false){
$movement_data = array('user_id', 'class_id', 'class_name', 'first_name', 'last_name', 'nickname', 'order', 'movement', 'rep_set_sec', 'rest', 'date');
foreach($movement_data as $key => $value){
     $movement_data = array(
        'user_id'   => $session_user_id,
        'class_id'  => $_GET['class_id'],
        'class_name'    => $class_name,
        'first_name'    => $_POST['first_name'],
        'last_name'     => $_POST['last_name'],
        'nickname'  => $_POST['nickname'],
        'order'     => $_POST['order'],
        'movement'  => $_POST['movement'],
        'rep_set_sec'   => $_POST['rep_set_sec'],
        'rest'      => $_POST['rest'],
        'date'      => $today
        );
    }

    completed_movement($movement_data);
    //header('Location: new_client.php');
    //exit ();

    }

Here is my query that inserts the POST data into db.

    function completed_movement($movement_data){
array_walk($movement_data, 'array_sanitize');

$fields = '`' . implode('`, `', array_keys($movement_data)) . '`';
$data = '\'' . implode('\', \'', $movement_data) . '\'';


$query = mysql_query ("INSERT INTO `completed_movements` ($fields) VALUES ($data)");

}

HTML form page has two table rows of data and looks like this.

    Colby (Big Cheese) Dodson | 1A | Key Movement | Sets/Reps/Seconds | Rest|
    -------------------------------------------------------------------------
    Mike (Big Mac) Mckenzie   | 1A | Key Movement | Sets/Reps/Seconds | Rest|

A var_dump ($_POST) produces this. I can see that value in there just not sure if it looks like its supposed to.

    array(7) { 
           ["first_name"]=> array(2) { 
               [0]=> string(5) "Colby" 
               [1]=> string(4) "Mike" } 
           ["nickname"]=> array(2) { 
               [0]=> string(10) "Big Cheese" 
               [1]=> string(7) "Big Mac" } 
           ["last_name"]=> array(2) { 
               [0]=> string(6) "Dodson" 
               [1]=> string(8) "McKenzie" } 
           ["order"]=> array(2) { 
               [0]=> string(2) "1A" 
               [1]=> string(2) "1A" } 
           ["movement"]=> array(2) { 
               [0]=> string(12) "Key Movement" 
               [1]=> string(12) "Key Movement" } 
           ["rep_set_sec"]=> array(2) { 
               [0]=> string(17) "Sets/Reps/Seconds" 
               [1]=> string(17) "Sets/Reps/Seconds" } 
           ["rest"]=> array(2) { 
               [0]=> string(4) "Rest" 
               [1]=> string(4) "Rest" } 
           }

Is this what its supposed to look like when it does the var_dump?

I need each row of my array to look like this. The first three values are the user_id, class_id, & class_name and are not part of my html form on the previous page. These vales are picked up on the parsing script page and passed along with each row of my results.

    $row = array(29, 48, Class 1, Colby, Big Cheese, Dodson, 1A, Key Movement, Sets/Reps/Secons, Rest)

I need a row like this for each entry from my form.

  • 写回答

1条回答 默认 最新

  • duanqiu2064 2012-08-17 17:38
    关注
    while($row = mysql_fetch_assoc($query));
    return $movement_data;
    
    print_r($movement_data);
    

    Your while loop goes through the whole array - there's no code evaluated. When it finishes, it returns the last value, because that's the only item in the variable. Your print_r is never going to be evaluated.

    Editted to add:

    foreach($movement_data as $key => $value);
    

    There's also a trailing semicolon on your foreach statement. That will have the same result.

    Editted, again:

    Each form value is being passed as an array of values; so what you need to do is step through the arrays, and process them one by one. The code below should work, but I've not tested it.

    It's looping on the $_POST["first_name"] array - for each of those values, it's populating a temporary array, and passing that to your function. Some of those values are hard-wired, and some are coming from the equivalent array index - the first run through uses the index elements with a value of 0, and so on.

    $i = 0;
    while (isset($_POST["first_name"][$i])) {
        $movement_data = array(   
            'user_id'   => $session_user_id,   
            'class_id'  => $_GET['class_id'],   
            'class_name'    => $class_name,   
            'first_name'    => $_POST['first_name'][$i],   
            'last_name'     => $_POST['last_name'][$i],   
            'nickname'  => $_POST['nickname'][$i],   
            'order'     => $_POST['order'][$i],   
            'movement'  => $_POST['movement'][$i],   
            'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
            'rest'      => $_POST['rest'][$i],   
            'date'      => $today   
            );   
    
        completed_movement($movement_data);
    
        $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题