开发者

nested foreach loops with a many to many relationship

开发者 https://www.devze.com 2022-12-19 15:20 出处:网络
I have two tables (groups and parties) in a many to many relationship using a lookup table called groupparty. I\'d like to be able to assign prices to each party, such that a party that belongs to two

I have two tables (groups and parties) in a many to many relationship using a lookup table called groupparty. I'd like to be able to assign prices to each party, such that a party that belongs to two groups might have one price in one group and a different price in another group. In the table groupparty, I have three columns: groupid, partyid and totalprice. So, to assign prices, I have the following form:

<form action="" method="post">
<?php foreach ($groups as $group): ?>
 <input type="hidden" name="groupids[]" 
        value="<?php echo $group['id']; ?>"/>
 <?php htmlout($group['groupname']); ?>
 <label for="totalprice">Price: 
 <input type="text" name="totalprices[]" id="totalprice" 
        value="<?php htmlout($totalprice); ?>"/></label><br />
 <?php endforeach; ?>
<input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
<input type="submit" name="action" value="Set"/>
</form>

On the mysql side, I've come as far as the following script. It almost works, except that it inserts the last totalprice value entered in the form above into all the associated groups. Any other totalprice values are lost - I'm left with only one value assigned:

 if (isset($_POST['action']) and $_POST['action'] == 'Set')
 {
  include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';
  $id = mysqli_real_esca开发者_高级运维pe_string($link, $_POST['id']);

  foreach($_POST['groupids'] as $groupid) 
  foreach($_POST['totalprices'] as $totalprice)
  {
   $sql = "UPDATE groupparty SET 
     totalprice = '$totalprice'
     WHERE groupid = '$groupid'
     AND partyid = '$id'";
   mysqli_query($link, $sql);
  }
 }

Any suggestions would be welcome. Thanks.


You can adjust your structure to make the updating a lot easier – just pass the group id as an key to the totalprices[] array in your HTML:

<form action="" method="post">
    <?php foreach ($groups as $group): ?>
        <?php echo($group['groupname']); ?>
        <label for="totalprice">Price:</label> 
            <input type="text" name="totalprices[<?php echo $group['id']; ?>]" id="totalprice" value="<?php echo($totalprice); ?>"/></label><br />
    <?php endforeach; ?>
    <input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
    <input type="submit" name="action" value="Set"/>
</form>

Then, you can just loop through that one array:

if (isset($_POST['action']) and $_POST['action'] == 'Set')
{
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';
    $id = mysqli_real_escape_string($link, $_POST['id']);

    foreach($_POST['totalprices'] as $groupid => $totalprice)
    {
        // Remember to also escape these
        $groupid = mysqli_real_escape_string($link, $groupid );
        $totalprice = mysqli_real_escape_string($link, $totalprice);

        $sql = "UPDATE
                    groupparty
                SET 
                    totalprice = '$totalprice'
                WHERE
                    groupid = '$groupid'
                AND
                    partyid = '$id'";

        mysqli_query($link, $sql);
    }
}

The problem with your current structure is that for every group id, you loop through all of the prices, so obviously when the last iteration is reached, the last value is overwriting the previous.

Given that you have groups 1, 2 and 3 and prices 20, 25 and 30. Your loop currently loops through the values like this:

Group   Price
1       20
1       25
1       30    # This is the last price Group 1 gets
2       20
2       25
2       30    # This is the last price Group 2 gets
3       20
3       25
3       30    # This is the last price Group 3 gets

When you actually wanted this:

Group   Price
1       20    # This is the last price Group 1 gets
2       25    # This is the last price Group 2 gets
3       30    # This is the last price Group 3 gets

As you can see from the upper table, all groups get the price 30 as that is the last price each of those get.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号