Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

for loop not incrementing

I have  a table in which I'm having jQuery add a table row each time the 'Add Row' button is pushed.

If my loop looks like "for(x=0; $x<1; ++$x)" the addRow does not work...if my loop looks like "for(x=1; $x < 2; ++$x)", then the program adds a row but does not actually increment x with each row addition.


	case "distributions":

	$poNumber = safeData($_GET['poNum']);
	
	echo '<span class="sectitleblk">Creating Header Distributions for PO # ' . $poNumber . ' for '. ucwords(strtolower(getVendorName($vendorid))) . '</span><br /><br />';
	
	if (isset($Errors)) 
		{
		echo "<div align='center'><span class='errormessagered'><ul class='errors'>";
		foreach ($Errors as $Error) 
			{
			echo "<li>".$Error."</li>";
			}
		echo "</ul></span></div><br />";
		}
	
	?>
	<form id="createDistribution" name="createDistribution" method="POST" enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>?kdaccount=<?=$vendorid?>&action=poinfo">
		<input type="hidden" name="vendoracct" value = "<?=$vendorid?>">
		<div id="container" class="shadow">
			<div id="row">
				<div id="left">
					<strong>Description:</strong>
				</div>
				<div id="defaultdiv">
					<div id="right">
						<input type="text" class="fieldclasssm" name="linedescr[]" value = "<? if (!empty($_POST['custRef'])) { echo "value='".safeData($_POST['custRef'])."'"; }?>" size="113" />
					</div>
				</div>
			</div>
			<div id="row">
				<div id="left">
					<strong>Distributions:</strong>
				</div>
				<div id="defaultdiv">
					<div id="right">
						<table border="1" class="lineTbl">
							<thead>
							<tr>
								<th>Qty</th>
								<th>nominal #</th>
								<th>depot<font color='red'>*</font></th>
								<th>amount<font color='red'>*</font></th>
								<th><input type="button" value="Add Line" class="add"/></th>
							</tr>
							</thead>
							<tbody class="lineBdy">
							<?php for ($x=0; $x<1; ++$x) { 
							echo "<tr id='line_".$x."' class='lineRow'>";
							?>				
								<td><?php echo $x; ?> &nbsp;<input type="text" class="serial" name="qty[]" size="10" /></td>
								<td><input type="text" class="date start" name="nominal[]" size="10" /></td>
								<td><input type="text" class="date end" name="depot[]" size="10" /></td>
								<td><input type="text" class="credit_amt" name="amount[]" size="10" /></td>
								<!-- <td align="center"><img src="./img/close.png" class="delRow" border="0"></td> -->
							</tr>				
							<?php 

							} ?>
							</tbody>
							<tr>
								<td colspan='5' align='right'>Total: $ <span id="grandtotal"></span><br /><font color='red'>(*)</font> Column names with asterisks are required. </td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>
	</form>
<?php
	break;
	}
?>

<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () { 
    $("#usedefaultbill").click(function () {
        $("#newdivbill").hide();
     });
    $("#usealternatebill").click(function () {
        $("#newdivbill").show();
    });
	
    $("#usedefaultship").click(function () {
        $("#newdivship").hide();
     });
    $("#usealternateship").click(function () {
        $("#newdivship").show();
    });
	
$("#poType").change(function(){

$("#showcustref").hide();

 var chosenValue = $("#poType").val();
if (chosenValue == "5" || chosenValue == "3" ) 
  
  {
    $("#showcustref").show();

}
  
});
	
	$("#compname").autocomplete({
		source: "autocomplete.php",
		minLength: 3,
		select: function(event, ui) {
			event.preventDefault();
			$("#compid").val(ui.item.id);
			$("#compname").val(ui.item.label);
			$(':text[name=shipCompName]').val(ui.item.label);
			$(':text[name=shipAddress]').val(ui.item.address);
			$(':text[name=shipCity]').val(ui.item.town);
			$(':text[name=shipState]').val(ui.item.state);
			$(':text[name=shipZip]').val(ui.item.postcode);
		}
	});
		
$("#compid").change( function () {
  $("#submit").trigger('click');
}); 

					// Add new table rows
					$(function() {
						var lineCnt = 1;
						
						$(".add").click(function(){					
							$("#line_1").clone(false).attr('id', 'line_'+lineCnt+'').appendTo(".lineBdy");
							$('#line_'+lineCnt+'').find('.serial, .credit_amt').val('');
							lineCnt++;
							$(".date").datepicker();
							$(".credit_amt").unbind().formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2 });
						});
						
					});

					
});
</script>

Open in new window

0
t3chguy
Asked:
t3chguy
  • 6
  • 4
  • 4
  • +2
1 Solution
 
user_nCommented:
for(x=0; $x<1; ++$x)
Is executed only one time for x= 0. And i think that it should be
for($x=0; $x<1; ++$x)

for(x=1; $x < 2; ++$x) I suppose that by default the value of $x is 0. You do not give it a value by x= 1, you should write $x = 1. So first time $x is 0 and we have one execution of the loop , then $x is 2 and the $x>2 so the for loop stops its execution
0
 
Paul MacDonaldDirector, Information SystemsCommented:
Correct me if I'm wrong, but in both cases the loop condition will only fire once.

That is in for(x=0; $x<1; ++$x) will x will only be less than 1 the first time through.

In for(x=1; $x < 2; ++$x), x will only be less than 2 the first time through.
0
 
t3chguyAuthor Commented:
So then, if I wanted one row to show by default, and have the ability to add unlimited number of rows, what would that sequence be?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gr8gonzoConsultantCommented:
for(x=
should be:
for($x=
0
 
Paul MacDonaldDirector, Information SystemsCommented:
I'm not a PHP person so I'm not authoritative, but in the loop syntax you're using you'd need to know how many rows there are.  There may be a different way to loop through until there's no data to show, but I don't know what that is.
0
 
user_nCommented:
you can use while loop
while(True) this means that the loop will never end
{
if(end condition) the condition that satisfies your ending condition
   break;
}


http://php.net/manual/en/control-structures.break.php
0
 
t3chguyAuthor Commented:
I'm not quite sure I know how to implement that in this case.
0
 
user_nCommented:
the idea is that while loop will never end
it will end only when break is executed
so you make an endless loop
that is doing what you whant
end you should know what is the condition this loop to end
When this condition is reached the break should be executed and it will stop the execution of while statement at this point
0
 
user_nCommented:
What exactly is your goal?
0
 
t3chguyAuthor Commented:
Essentially what it's doing is running the loop ONE time, and showing all 10 rows by default, then anything more than ten, the row id, or value of x is just one, which isn't what it should be doing.


What I'm trying to accomplish is showing 1 row first, and then adding a row which each time the 'Add Line' button is pressed and having the row id increment all the way up until I would say 10 rows max.



									$counter = 1;
								WHILE ($counter <= 1) 
										{
										for ($x=0; $x<=10; $x++)
										{ 
										echo "<tr id='line_".$x."' class='lineRow'>";
									?>				
										<td><?php echo $x; ?> &nbsp;<input type="text" class="serial" name="qty[]" size="10" /></td>
										<td><input type="text" class="date start" name="nominal[]" size="10" /></td>
										<td><input type="text" class="date end" name="depot[]" size="10" /></td>
										<td><input type="text" class="credit_amt" name="amount[]" size="10" /></td>
										<!-- <td align="center"><img src="./img/close.png" class="delRow" border="0"></td> -->
									</tr>				
									<?php 

									} $counter++;} ?>

Open in new window

0
 
Richard QuadlingSenior Software DeverloperCommented:
Do you know how many times you should execute the loop?

foreach(range(1, $i_Count) as $x) { ... }

Open in new window

for($x = 0; $x < $i_Count; ++$x) { ... }

Open in new window

$x = 0;
while ($x < $i_Count) {
  ++$x;
}

Open in new window


All provide the same sort of functionality.
0
 
t3chguyAuthor Commented:
I would think a max of 10 times would be best.
0
 
Richard QuadlingSenior Software DeverloperCommented:
I would also STRONGLY recommend that you put the index in the <input> tags name.

Think of it as defensive programming.

e.g.

<?php
foreach(range(0, 9) as $i_Index)
 {
 echo <<< END_TR
<tr id="line_{$i_Index}" class="lineRow">
  <td>{$i_Index} &nbsp;<input type="text" class="serial" name="qty[{$i_Index}]" size="10" /></td>
  <td><input type="text" class="date start" name="nominal[{$i_Index}]" size="10" /></td>
  <td><input type="text" class="date end" name="depot[{$i_Index}]" size="10" /></td>
  <td><input type="text" class="credit_amt" name="amount[{$i_Index}]" size="10" /></td>
  <!-- <td align="center"><img src="./img/close.png" class="delRow" border="0"></td> -->
</tr>				
END_TR;
 }

Open in new window

0
 
t3chguyAuthor Commented:
RQuadling,

That still displays all 10 rows on load, I was hoping to have one row load by default , and have the 'Add Row' button display a new row each time the button is pressed, for a maximum of 10 rows.
0
 
Richard QuadlingSenior Software DeverloperCommented:
I would think about using jQuery to add a new row to the form, rather than round tripping it to the server.
If you want a button, then maybe the button is a submit button and in your PHP code, you increment ++$_SESSION['RequiredRows']; and use that as the upper limit to your range?
0
 
t3chguyAuthor Commented:
I actually think I've got it down...

	<tr> 
		<td>
			<input type="text" id="quantity_1" name="quantity_1" class="fieldclasssm" size="5" />
		</td> 
		<td> 
			<input type="text" id="nominal_1" name="nominal_1" class="fieldclasssm" size="35" /> 
		</td> 
		<td> 
			<input type="text" id="depot_1" name="depot_1" class="fieldclasssm" size="35" /> 
		</td> 
		<td> 
			<input type="text" id="amount_1" name="amount_1" class="fieldclasssm" size="10" /> 
		</td> 
		<td><input type="button" id="addnew" name="addnew" value="Add Distribution" /> </td>
	</tr> 

Open in new window


jQuery:

		var currentItem = 1;
		$('#addnew').click(function(){
			currentItem++;
			$('#items').val(currentItem);
			var strToAdd = '<tr><td><input type="text" id="quantity_'+currentItem+'" name="quantity_'+currentItem+'" class="fieldclasssm" size="5" /></td><td><input type="text" id="nominal_'+currentItem+'" name="nominal_'+currentItem+'" class="fieldclasssm" size="35" /></td><td><input type="text" id="depot_'+currentItem+'" name="depot_'+currentItem+'" class="fieldclasssm" size="35" /></td><td><input type="text" id="amount_'+currentItem+'" name="amount_'+currentItem+'" class="fieldclasssm" size="10" /></td></tr>';
			$('#distributions').append(strToAdd);
			
		});

Open in new window

0
 
Richard QuadlingSenior Software DeverloperCommented:
I would use
'<tr><td><input type="text" id="quantity['+currentItem+']" name="quantity_'+currentItem+'" class="fieldclasssm" size="5" />';

Open in new window


as this will translate nicely in PHP to an array $_POST['quantity'][n], where n is the index.

Otherwise you have $_POST['quantity_n'] and have to hunt for every entry.

And you can go further ...

'<tr><td><input type="text" id="rows['+currentItem+'][quantity]" name="quantity_'+currentItem+'" class="fieldclasssm" size="5" />';

Open in new window


Now you can iterate $_POST['rows'] and each 'row' will have a 'quantity'. This now looks like a result set from an SQL query.

So, names are currently "quantity_n" => "quantity[n]" => "rows[n][quantity]"

I hope that makes some sense.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now