Solved

Warning: Invalid argument supplied for foreach()

Posted on 2012-12-30
3
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

691 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