Jump to content

[SOLVED] foreach and multiple pairs


sculpy

Recommended Posts

I would like to step through 3 different arrays passed through a multi-line form. For example:

 

Item # Discount from drop down list apply discount

Item # Discount from drop down list apply discount

Item # Discount from drop down list apply discount

Item # Discount from drop down list apply discount

submit

 

The submit button would then process all lines within the table. I could make the form deal with one row at a time but when the table grows it becomes tedious. My goal is to be able to update the entire table at once.

 

I'm not sure how I would accomplish this without the possibility of losing control through a loop. Cryptic, I know...

 

What I'm trying to do is something like:

 

foreach ($_POST['applyDisc'] as $apply, $_POST['discount'] as $discount, $_POST['itemid'] as $iid)

 

and then do a sql update like:

 

update catalog set is_discounted='$apply', discount_amount='$discount' where item_id='$iid'

 

I know the foreach example above won't work. Is there another way to design this? Suggestions appreciated.

 

Forgive the lame explanation, I'm getting tired.

Link to comment
https://forums.phpfreaks.com/topic/82432-solved-foreach-and-multiple-pairs/
Share on other sites

Are you sure thats not what you need?

foreach ($_POST['applyDisc'] as $key1 => $apply)
{
  foreach ($_POST['discount'] as $key2 => $discount)
   {
     $str = "update catalog set is_discounted='$apply', discount_amount='$discount' where item_id='$iid'";
   }
}

?

My first example was just a generic example.

I cheesed my way through passing a counter and using for loops. This actually worked.

 


<?php
    $conn = @mysql_connect('HOST', 'USER', 'PASSWORD')	or die('Could not connect: ' . mysql_error());
    mysql_select_db('DATABASETABLE') or die("Coult not select database!");
    if (isset($_POST['Submit']) && ($_POST['Submit'] == 'Submit')) 
    {
        $number = $_POST['idxctr'];
   	for ($i = 0; $i < $number; $i++) 
        {
    $apply = $_POST['applyDisc'][$i];
    $discount = $_POST['discount'][$i];
    $iid = $_POST['itemid'][$i];
    if ($apply == '') 
            {
        // do nothing 
    } else { 
	$apply = 1;
	$query = "update catalog set on_sale='$apply', discount_id='$discount' where item_id='$iid'";
	$result = mysql_query($query);
    }			
}
echo '<p class="directions">Items placed on sale in the database.</p>'; 
    }	
    $query = "select distinct catalog.item_id as iid, catalog.img as img, catalog.style_num as item, 
                  catalog.category_id as ccid, catalog.discount_id as discount, catalog.on_sale as onsale, 
                  category.category_id as cid, category.category as category from catalog, category where                 
                  catalog.category_id=category.category_id";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);

    echo '<div align="center">';
    echo '<table width="90%" cellspacing="0" cellpadding="2">';
    echo '<tr bgcolor="#000000"><th> </th><th><span style="color:#FFFFFF">Image</span></th><th>
             <span style="color:#FFFFFF">Item #</span></th><th><span style="color:#FFFFFF">Category</span></th>
            <th><span style="color:#FFFFFF">Discount</span></th><th><span style="color:#FFFFFF">Apply</span></th></tr>';
    $cnt = 0;
    echo '<form action="saleItem.php" method="post" enctype="multipart/form-data">';
    for ($i = 0; $i < $num; $i++) 
    {
$iid = mysql_result( $result, $cnt, 'iid');
$img = mysql_result( $result, $cnt, 'img' );
$item = mysql_result( $result, $cnt, 'item' );
$ccid = mysql_result( $result, $cnt, 'ccid' );
$discount = mysql_result( $result, $cnt, 'discount' );
$onsale = mysql_result( $result, $cnt, 'onsale' );
$cid = mysql_result( $result, $cnt, 'cid' );
$category = mysql_result( $result, $cnt, 'category' );

         if (($cnt % 2) == 0) 
        {
    echo '<tr bgcolor="#c0c0c0">';
} else {
    echo '<tr>';
}
echo '<td valign="top" align="center">' . ($cnt + 1) . '</td>';
echo '<td valign="top" align="center"><img src="../images/' . $img . '" width="10%" height="10%" /></td>';
echo '<td valign="top" align="center">' . $item . '</td>';
echo '<td valign="top" align="center">' . $category . '</td>';
echo '<td valign="top" align="center"><input name="itemid['.$i.']" type="hidden" value="'.$iid.'" /><select name="discount['.$i.']">';
$query2 = "select * from discounts";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_array($result2)) 
        {
    if ($row2['discount_id'] == $discount) 
            {
        $selected = ' selected ';
    } else {
	$selected = '';
    }
            echo '<option '.$selected.' value="' . $row2['discount_id'] . '">' . $row2['percentage'] . '</option>';
}
echo '</select></td>';
echo '<td valign="top" align="center"><input name="applyDisc['.$i.']" type="checkbox" value="' . $onsale . '" /></td>';	
echo '</tr>';
$cnt++;
    }
    echo '<tr><td colspan="6" align="right"><input type="hidden" name="idxctr" value="'.$num.'" /><input name="Submit" type="submit" value="Submit" /></td></tr>';
    echo '</form></table></div>';	
?>

 

Yeah, go figure...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.