Solved

Warning: Invalid argument supplied for foreach()

Posted on 2012-12-30
3
508 Views
Last Modified: 2013-01-05
Not sure what I'm doing wrong, but I've been up all night trying to catch up on some work.  I'm sure this is just a simple oversight, but the error is occurring on Line 20, which is where I have foreach($_POST[$field] as $key => $line)

<?php

$programid = "2064";

require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/expFunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header-lightbox.php");

$workrecordnum = safeData($_GET['id']);

if(isset($submit))
	{
	$fields = array('partnumber','partdescription','qty','cost');
	$worknumid = $_POST['workrecordnum'];
	
	
	foreach ($fields as $field) 
		{
		foreach ($_POST[$field] as $key=>$line) 
			{
			$returnArray[$key][$field] = $line;								
			}
		}
						
		foreach ($returnArray as $lineItem) 
			{
			if (!empty($lineItem['partnumber']) && !empty($lineItem['partdescription']) && !empty($lineItem['cost'])) 
				{
				// Dump into DB.
				$partnum = safeDataMySQL($lineItem['partnum']);
				$partdesc = safeDataMySQL($lineItem['partdesc']);								
				$qty = strtotime($lineItem['qty']);
				$amount = strtotime($lineItem['amount']);

				$query = "INSERT INTO `cr_memos_lines` (
													items_id,
													partNo,
													description,
													qty,
													cost)
							VALUES ('".$worknumid."',
									'".$partnum."',
									'".$partdesc."',
									'".$qty."',
									'".$amount."'
									)	";
							//or die(mysql_error());
							
				echo $query . '<br /><br />';
				}
			}
	}
?>

<style type="text/css">
.required {
	background-color:#b6c0b2;
}
.errors {
	color:#F7473D;
	font-weight:bold;
	font-size:15px;
}
.formBox {
	border: 1px solid #2D2D2D;
    color: #000000;
    font-family: arial;
    font-size: 13px;
    font-weight: bold;
    padding: 2px;
	width:295px;
	height:80px;
}
</style>

<form id="addLines" method="POST" action="<?=$_SERVER['PHP_SELF']?>">

	<table border="0" cellspacing="0" cellpadding="0" class="lineTbl" width="100%">
				<thead>
				<tr>
					<th class="columnheader" align="left">Part  #</th>
					<th class="columnheader" align="left">Description of Part</th>
					<th class="columnheader" align="left">Quantity</th>
					<th class="columnheader" align="left">Cost</th>
				</tr>
				</thead>
				<tbody class="lineBdy">
				<?php for ($x=1; $x<=1; $x++) { 
				echo "<tr id='line_".$x."' class='lineRow'>";
				
				$color_A = 'class="alt2_1"'; 
				$color_B = 'class="alt2_2"';

				$row_color = ($x % 2) ? $color_A : $color_B;
				
				?>				

					<td <?=$row_color?>><input type="text" class="serial required" name="partnum[]" size="10" /></td>
					<td <?=$row_color?>><input type="text" class="serial required" name="partdesc[]" size="75" /></td>
					<td <?=$row_color?>>
						<select name="qty[]" class="serial required">
						<?php
							for ($i=1; $i<=100; $i++) {
								echo "<option value='".$i."'>".$i."</option>";
							}
						?>
						</select>
					</td>
					<td <?=$row_color?>><input type="text" class="credit_amt serial cost required" name="amount[]" size="10" /></td>
				</tr>				
				<?php } ?>
				</tbody>
			</table>
			<div align="center"><br /><input type="button" value="Add Line" class="add"/> | <input type="submit" name="submit" value = "Finalize Order"></div>
			<!--Hidden Variables-->
			<input type="hidden" name="workrecordnum" value = "<?=$workrecordnum?>">
			</form>
			
<script type="text/javascript">
	$("document").ready(function(){
		
		$('.cost').blur(function () {
			var sum = 0;
			$('.cost').each(function() {
				num = $(this).val().replace('$', '');
				sum += Number(num);
			});
			
			$('#grandtotal').html(Math.round(sum*100)/100);
		});
				
		$('.start:first').change(function(){
			var start = $(this).val();
			$('.start').val(start);
		});
		$('.end:first').change(function(){
			var end = $(this).val();
			$('.end').val(end);
		});
		
		<?php
			if (!empty($fullChecked)) {
				echo "$('#fullInvoice').show();";
				echo "$('#indivLines').hide();";
			} elseif (!empty($portionChecked)) {
				echo "$('#fullInvoice').hide();";
				echo "$('#indivLines').show();";
			}
			
			
			if (!empty($cloneData['memoMailing'])) {
				echo "$('.mail_to').show();";
			} 
		?>
});
	$(function() {
		$('.amount').blur(function() {
			$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2 });
		})
		.keyup(function(e) {
			var e = window.event || e;
			var keyUnicode = e.charCode || e.keyCode;
			if (e !== undefined) {
				switch (keyUnicode) {
					case 16: break; // Shift
					case 17: break; // Ctrl
					case 18: break; // Alt
					case 27: this.value = ''; break; // Esc: clear entry
					case 35: break; // End
					case 36: break; // Home
					case 37: break; // cursor left
					case 38: break; // cursor up
					case 39: break; // cursor right
					case 40: break; // cursor down
					case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
					case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
					case 190: break; // .
					default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
				}
			}
		});
	});
	</script>

Open in new window

0
Comment
Question by:t3chguy
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38731088
$_POST is an array, but $_POST[$field] is you indexing an array. Unless you put an array into $_POST[$field], then that line doesn't make much sense.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38731096
PHP var_dump() is your friend.  Try var_dump($_POST) just before line 12 and see what you get.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 38732940
greetings   t3chguy, , you do not have "Matching" _POST Key strings in your code you have the name as

name="partnum[]"  and  name="partdesc[]"

BUT you have the fields as =
$fields = array('partnumber','partdescription','qty','cost');

I would think it should be something like =
$fields = array('partnum','partdesc','qty','amount');

but I am not sure about all you intend to do in your code?
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question