0

Hi I am trying to push values from the input into the array. I am a beginner in php and this is my assignment. 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";
    }
}    

?>
Kashad
  • 19
  • 5
  • That `Monday` key would show a `E_NOTICE : type 8 -- Use of undefined constant Monday - assumed 'Monday' -- at line x`. I don't know if there is something else in your code, can you check if you have error reporting enabled? [How do I get PHP Errors to display?](http://stackoverflow.com/q/1053424/4577762) – FirstOne Mar 04 '16 at 23:16
  • @FirstOne Pretty sure it'll still work as legacy applications used to use it. – Matt Mar 04 '16 at 23:16
  • @FirstOne I am chilled :P – Matt Mar 04 '16 at 23:21
  • i<3... i is not a constant, should be $i – Gavin Mar 04 '16 at 23:35
  • 1
    `if (i < 3)` should be `if($i < 3)`, and the same for the other two. If you fix that, your code should work. – Barmar Mar 04 '16 at 23:43

5 Answers5

0

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.

Matt
  • 2,838
  • 1
  • 11
  • 27
0

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:

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";
}
Don't Panic
  • 37,589
  • 9
  • 55
  • 71
0

If you can modify the HTML form, I suggest you to change the input names in this way:

<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:

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;
  • $wkday[Monday] (and similar in your code) is not correct: Monday is interpreted as constant. In your case, this will work because there are not a Monday constant, but if a constant exists, you will have undesired result. Use $wkday['Monday'] instead.
fusion3k
  • 11,012
  • 4
  • 21
  • 42
0

This is my 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

$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";
    }
}
?>
Kashad
  • 19
  • 5
0

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.

Make sure you accessing individual array elements in indexed and associative arrays

Kashad
  • 19
  • 5