简体   繁体   English

将值推入PHP中的数组

[英]Pushing values into array of in php

Hi I am trying to push values from the input into the array. 嗨,我正在尝试将值从输入推入数组。 I am a beginner in php and this is my assignment. 我是php的初学者,这是我的任务。 I trying to make a schedule Like 我试图制定一个时间表

Mondays = this amount of hours Tuesday = this amount of hours and etc 星期一=该小时数周二=该小时数等

But I gotten confused during the process. 但是我在此过程中感到困惑。

<?php


if (isset($_POST['submit'])){


$hours =array();
$hours[] =$_POST['mod1'];
$hours[] =$_POST['mod2'];
$hours[] =$_POST['mod3'];
$hours[] =$_POST['tues1'];
$hours[] =$_POST['tues2'];
$hours[] =$_POST['tues3'];
$hours[] =$_POST['wed1'];
$hours[] =$_POST['wed2'];
$hours[] =$_POST['wed3'];

$wkday = array("Monday" => 0, "Tuesday" => 0, "Wednesday" => 0);
$i = 0;

foreach ($hours as $value){
    if(i<3){
    $wkday[Monday] = $wkday[Monday] + $value;
    } else if(i<6){
        $wkday[Tuesday] = $wkday[Tuesday] + $value;
    } else if(i<9){
        $wkday[Wednesday] = $wkday[Wednesday] + $value;
    }

    $i++;
    }

foreach($wkday as $key => $value) {
    echo "Day: $key; Hours Worked: $value <br /> \n";
    }
}    

?>

A quicker way would be to do the following: 一种更快的方法是执行以下操作:

<?php
if (isset($_POST['submit'])){
    $hours =array();
    $hours['Monday'] += (float)$_POST['mod1'];
    $hours['Monday'] += (float)$_POST['mod2'];
    $hours['Monday'] += (float)$_POST['mod3'];
    $hours['Tuesday'] += (float)$_POST['tues1'];
    $hours['Tuesday'] += (float)$_POST['tues2'];
    $hours['Tuesday'] += (float)$_POST['tues3'];
    $hours['Wednesday'] += (float)$_POST['wed1'];
    $hours['Wednesday'] += (float)$_POST['wed2'];
    $hours['Wednesday'] += (float)$_POST['wed3'];
    foreach($hours as $key => $value) 
        echo "Day: $key; Hours Worked: $value <br /> \n";
}    
?>

Just adding based off the $_POST float vals then echoing like you did before. 只是基于$_POST浮动val进行添加,然后像以前一样回显。

One way to do this is to define an array to map day names to the abbreviations you are using for your input names. 一种方法是定义一个数组,将日期名称映射到您用于输入名称的缩写。

$days = array('mod' => 'Monday', 'tues' => 'Tuesday', 'wed' => 'Wednesday');

Then you can fill your $wkday array with a nested loop like this: 然后,您可以使用如下嵌套循环来填充$wkday数组:

foreach ($days as $key => $day) {          // outer loop iterates over 'mod', 'tues', 'wed'
    for ($i=1; $i <= 3; $i++) {            // inner loop iterates over 1, 2, 3
        $wkday[$day] += $_POST["$key$i"];  // $key$i gives 'mod1' etc.
    }
    // You can echo as you create the array so you don't have to loop again.
    echo "Day: $day; Hours Worked: $wkday[day]<br>\n";
}

If you can modify the HTML form, I suggest you to change the input names in this way: 如果可以修改HTML表单,建议您以这种方式更改输入名称:

<input name="hours[Monday][]">
<input name="hours[Monday][]">
<input name="hours[Monday][]">
<input name="hours[Tuesday][]">
<input name="hours[Tuesday][]">
<input name="hours[Tuesday][]">
(...)

Then, in your php code, you can simply write this code: 然后,在您的php代码中,您可以简单地编写以下代码:

if( isset( $_POST['submit'] ) )
{
    $wkday = array( 'Monday' => 0, 'Tuesday' => 0, ... );

    foreach( $wkday as $key => $val )
    {
        $wkday[$key] = array_sum( $_POST['hours'][$key] );
        echo "Day: $key; Hours Worked: {$wkday[$key]} <br /> \n";
    }
}

Also please note — referring to your original code: 请注意 -参考您的原始代码:

  • You have to refer to variables using $ : so, $i , not i ; 您必须使用$来引用变量:因此, $i而不是i
  • $wkday[Monday] (and similar in your code) is not correct: Monday is interpreted as constant. $wkday[Monday] (和代码中的类似内容)不正确: Monday被解释为常量。 In your case, this will work because there are not a Monday constant, but if a constant exists, you will have undesired result. 在您的情况下,这将起作用,因为没有Monday常量,但是如果存在常量,那么您将得到不希望的结果。 Use $wkday['Monday'] instead. 使用$wkday['Monday']代替。

This is my html: 这是我的html:

<body>
     <h1 class="center">Scheduling</h1>

    <div class="table" style="border: solid;">

    <form class='center' method='POST' action='test2.php'>

    <h1> Employee Schedule</h1>

    Name: <input type="text" name="Nam"  required=" " />

    Department: <input type="text" name="Dept"  required=" " />

    Title: <input type="text" name="title" required=" " />

    <h2> Weekdays </h2>

   Monday: <input type="number" name="hours[Monday][]" value="0" require="<8" />
            <input type="number" name="hours[Monday][]" value="0" require="<8" />
          <input type="number" name="hours[Monday][]" value="0" require="<8" />


    Tuesday: <input type="number" name="hours[Tuesday][]" value="0" require="<8" />
            <input type="number" name="hours[Tuesday][]" value="0" require="<8" />
            <input type="number" name="hours[Tuesday][]" value="0" require="<8" />

    Wednesday: <input type="number" name="hours[Wednesday][]" value="0" require="<8" />
            <input type="number" name="hours[Wednesday][]" value="0" require="<8" />
            <input type="number" name="hours[Wednesday][]" value="0" require="<8" />

    Thursday: <input type="number" name="hours[Thursday][]" value="0" require="<8" />
            <input type="number" name="hours[Thursday][]" value="0" require="<8" />
            <input type="number" name="hours[Thursday][]" value="0" require="<8" />

    Friday: <input type="number" name="hours[Friday][]" value="0" require="<8" />
            <input type="number" name="hours[Friday][]" value="0" require="<8" />
            <input type="number" name="hours[Friday][]" value="0" require="<8" />

    Saturday: <input type="number" name="hours[Saturday][]" value="0" require="<8" />
            <input type="number" name="hours[Saturday][]" value="0" require="<8" />
            <input type="number" name="hours[Saturday][]" value="0" require="<8" />

    Sunday: <input type="number" name="hours[Sunday][]" value="0" require="<8" />
            <input type="number" name="hours[Sunday][]" value="0" require="<8" />
            <input type="number" name="hours[Sunday][]" value="0" require="<8" />




             <input type="submit" name="submit" value="Calculate" />

    </form>

Here my php: 这是我的PHP:

<?php

$Nam = $_POST['Nam'];
$Dept = $_POST['Dept'];
$Title = $_POST['title'];

if( isset( $_POST['submit'] ) )
{
    $wkday = array( 'Monday' => 0, 'Tuesday' => 0, 'Wednesday' => 0, 'Thursday' => 0, 'Friday' => 0, 'Saturday' => 0, 'Sunday' => 0);
    echo "Employee name: $Nam <br /> \n";
        echo "Department: $Dept <br /> \n";
        echo "Title: $Title <br /> \n";

    foreach( $wkday as $key => $val )
    {
        $wkday[$key] = array_sum( $_POST['hours'][$key] );
        echo "Day: $key; Hours Worked: {$wkday[$key]} <br /> \n";
    }
}
?>

I dont think i completed my assisgnment: 我不认为我完成了自己的承诺:

Creating the form containing checkboxes and other form elements to create arrays. 创建包含复选框和其他表单元素的表单以创建数组。 AND the array() function to create indexed and associative arrays. AND array()函数创建索引数组和关联数组。

Make sure you accessing individual array elements in indexed and associative arrays 确保访问索引数组和关联数组中的单个数组元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM