简体   繁体   中英

PHP Array Post Into Database with Multiple textboxes in Foreach

I am pretty darn new to PHP and I am getting a bit muddled with the Arrays and Foreach.

I am currently working on a project where we have numerous teams, and each team has a set amount of people in it, which we get have in a database.

I have a foreach set up where a user can select a team and it will display every user in the team in a row with a textbox for each day of the week.

<?php  foreach ($aTeam AS $iUserId => $aAgent){ ?>
<tbody>
    <tr class="<?php 
        echo $aAgent['iStatusId']==0?'low_light':
        'high_light';
        ?>">
        <td><?php  echo FilterOut::HTML($aAgent['sName']); ?></td>
<td style="display: none;"><input width="0px" type="hidden" name="iUserId[]" value="<?php  echo $aAgent['iUserId']; ?>" /></td>
<td><input id="iMonMins" name="iMonMins[]" type="text" value="<?php  echo $aAgent['iMon']   ?>" /></td>
<td><input id="iTuesMins" name="iTueMins[]" type="text" value="<?php  echo $aAgent['iTues']    ?>" /></td>
<td><input id="iWedMins" name="iWedMins[]" type="text" value="<?php  echo $aAgent['iWed']    ?>" /></td>
<td><input id="iThursMins" name="iThurMins[]" type="text" value="<?php  echo $aAgent['iThurs']    ?>" /></td>
<td><input id="iFriMins" name="iFriMins[]" type="text" value="<?php  echo $aAgent['iFri']    ?>" /></td>
<td><input id="iSatMins" name="iSatMins[]" type="text" value="<?php  echo $aAgent['iSat']    ?>" /></td>
<td><input id="iSunMins" name="iSunMins[]" type="text" value="<?php  echo $aAgent['iSun']    ?>" /></td>
<?php  var_dump($aForm) ?>
</tr>
</tbody>

Each person will have a specified amount in each textbox which I then want to upload to the database, but I really dont understand how they relate to each other.

So the Array looks like this for two users (the number of users is usually up to about 20 people)

'iUserId' => 
    array (size=2)
      0 => string '42' (length=2)
      1 => string '180' (length=3)

the array for the iMonMins looks like

  'iMonMins' => 
    array (size=2)
      0 => string '99' (length=2)
      1 => string '18' (length=2)

the array for iTuesMins looks like

  'iTueMins' => 
    array (size=2)
      0 => string '300' (length=1)
      1 => string '324' (length=1)

I wont print them all out, as you probably get the idea.

So I then have each of these being put into an array

$oUser->Update_AgentHours($aForm['iUserId'], $aForm['iMonMins'], $aForm['iTueMins'], $aForm['iWedMins'], $aForm['iThurMins'], $aForm['iFriMins'], $aForm['iSatMins'], $aForm['iSunMins']);

Which is then called in a function to upload to the database

function Update_AgentHours($iUserId, $iMonMins, $iTueMins, $iWedMins, $iThurMins, $iFriMins, $iSatMins, $iSunMins) {
    {
        $sSql = 'UPDATE cfg_users SET `iMon` = \''.$iMonMins.'\', `iTues` = \''.$iTueMins.'\', `iWed` = \''.$iWedMins.'\', `iThurs` = \''.$iThurMins.'\', `iFri` = \''.$iFriMins.'\', `iSat` = \''.$iSatMins.'\', `iSun` = \''.$iSunMins.'\'  WHERE `iUserId` = \''.$iUserId.'\';';
        $this->oDb->query($sSql);   
        }
    }

At first I didnt have each textbox as an Array so it was only uploading the last row of textboxes and I read that I needed each one put into an array so I can then loop through and Upload them.

I just dont understand how each array is linked to each other?

I really want to say:

UPDATE "mydatabase" SET iMon = $aForm[iMonMins[0]], iTue = $aForm[iTueMins[0]] WHERE iUserId = $aForm[iUserId[0]]
UPDATE "mydatabase" SET iMon = $aForm[iMonMins[1]], iTue = $aForm[iTueMins[1]]  WHERE iUserId = $aForm[iUserId[1]]

But I dont understand how I can put that the first userid[0] should have the first one[0] of each minutes, then the second[1] should have the second[1] of each minutes...

I hope I have explained this well enough

Thanks

UPDATE: (I couldnt work how to add [code] to the comments part so I thought I would just update this part..

I have a bit of trouble understanding the "$idx=>$iUserId" part.

I added the POST as you suggested and this is the top part (as I cant add it all, its too big)

POST ARRAYArray
(
    [iTeamId] => 0
    [iUserId] => Array
        (
            [0] => 42
            [1] => 180
        )

    [iMonMins] => Array
        (
            [0] => 0
            [1] => 0
        )

So $idx is [0], so in the post [iMonMins][$idx] = 0, that will process the value attached to that UserId?

I think i have a solution which you can try out let me know if it works for you.

In your php you loop through your users with a for each which i guess you know how to do then you have a variable out side the for loop which increments every time in the for loop with the variable you can get your user to have each row pertaining to them. Like user1 have first row of each day, user2 second row... usern nth row. Something like this.

$i = 0;
foreach($aForm[iUserId] as $user)
{
    UPDATE "mydatabase" SET iMon = $aForm[iMonMins][$i], iTue = $aForm[iTueMins][$i] WHERE iUserId = $user;
$i++;
}

Try it out hope it works, would have tried it out before posting but very busy here with a project.

Ok start by putting this code at the top of your script so you can visualise the $_POST array, then run the page and hit the submit.

echo '<pre>POST ARRAY' . print_r($_POST,true) . '</pre>';

This should dump the post array for you to view the data you are going to have to process.

Now to read all the data you can foreach over the $_POST['iUserid'] and use the index of the userid to address all the other arrays so you keep everything in order, like so.

foreach( $_POST['iUserid'] as $idx => $iUserid ) {

    //  now we will get one iUserid each time round the loop 
    //  and a idx identifying which occurance of the array we are on
    //  so now collect the day information for that user

    // First you must ensure you sanitize the data in the $_POST array
    // if its not already being done

    $oUser->Update_AgentHours(
                      $iUserId, 
                      $_POST['iMonMins'][$idx], 
                      $_POST['iTuesMins'][$idx], 
                      $_POST['iWedMins'][$idx], 
                      $_POST['iThurMins'][$idx], 
                      $_POST['iFriMins'][$idx], 
                      $_POST['iSatMins'][$idx], 
                      $_POST['iSunMins'][$idx]);

}

BIG NOTE I have not done any sanitization of the $_POST array. You must ensure that this is done before you attempt to put this data onto the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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