The run away script crashed the server... Recursive loop runs indefinitely.

I have this:

[php]

                <tbody>
                <?php 
					$tot_cost = 0;
					$tot_vat = 0;
					$tot_price = 0;
					echo count($item);
					//for($l=0; $l<count($item,COUNT_RECURSIVE); $l++){
				?>
                	<tr>                            
						<td><?php echo $item[$l]['code']; ?> <input type="hidden" name="item[<?php echo $l; ?>][code]" value="<?php echo $item[$l]['code']; ?>" /></td>
                        
						<td><?php echo $item[$l]['desc']; ?> <input type="hidden" name="item[<?php echo $l; ?>][desc]" value="<?php echo $item[$l]['desc']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].$item[$l]['price']; ?> <input type="hidden" name="item[<?php echo $l; ?>][price]" value="<?php echo $item[$l]['price']; ?>" /></td>
                        
						<td><?php echo $item[$l]['q']; ?> <input type="hidden" name="item[<?php echo $l; ?>][q]" value="<?php echo $item[$l]['q']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].$item[$l]['bv']; ?> <input type="hidden" name="item[<?php echo $l; ?>][bv]" value="<?php echo $item[$l]['bv']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].$item[$l]['vat']; ?> <input type="hidden" name="item[<?php echo $l; ?>][vat]" value="<?php echo $item[$l]['vat']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].$item[$l]['subtotal']; ?> <input type="hidden" name="item[<?php echo $l; ?>][subtotal]" value="<?php echo $item[$l]['subtotal']; ?>" /></td>
                        
					</tr>
                <?php
				
					$item[$l]['bv'] = $item[$l]['price'] * $item[$l]['q'];
					
					$item[$l]['vat'] = $item[$l]['bv']*($vat_at*100);
					
					$item[$l]['subtotal'] = $item[$l]['bv'] +$item[$l]['vat']; 
					
					

					$linetotal += $item[$l]['bv'];
					
					$totalvat += $linetotal - ($linetotal*($vat_at*100));
					
					$grandtotal += $item[$l]['subtotal'];
					 
					//}
				?>
                </tbody>
                
                <tfoot>
                <tr>
                    	<td><input type="text" name="item[<?php echo count($item);?>][code]" id="code" class="form-text-small" value="" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][desc]" id="desc" class="form-text" value="" /></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][price]" id="price" class="form-text-price" value="0.00" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][quantity]" id="q" class="form-text-q" value="0" /></td>
                        
                    	<td></td>
                    	<td></td>
                    	<td><input name="action" type="submit" value="Update Invoice" class="form-submit"></td>
                    </tr>
                    <tr>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                       	<td></td>

                    </tr>
                    <tr>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
						<td></td>
                    </tr>
                    <tr>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                        <td></td>
                   		<td><strong>Subtotal:</strong></td>
                   		<td><?php echo $site_settings['currency'].number_format($linetotal,2); ?></td>
                    </tr>
                    <tr>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                        <td></td>
                   		<td><strong>Vat total:</strong></td>
                   		<td><?php echo $site_settings['currency'].number_format($totalvat,2); ?></td>
                    </tr>
                    <tr>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                    	<td></td>
                        <td></td>
                   		<td><strong>Grand total:</strong></td>
                   		<td><?php echo $site_settings['currency'].number_format($grandtotal,2); ?></td>
                    </tr>
                    
                </tfoot>
                
                </table>
                
</td>
Item ID Description Cost Qty Nett VAT Gross
[/php]

Which is part of a larger form.

The PHP part:

[php]
if ($_POST[‘action’] == “Update Invoice”){

$n_order = $_POST[‘n_order’];
$n_price = $_POST[‘n_price’];
$vat_at = $_POST[‘vat_at’];
$customer_id = $_POST[‘customer_id’];
$customer_address = $_POST[‘customer_address’];
$customer_name = $_POST[‘customer_name’];
$item = $_POST[‘item’];
$notes = $_POST[‘notes’];

$customername = $_POST[‘customer’];
$customerid = $_POST[‘customerid’];
$address1 = $_POST[‘address1’];
$address2 = $_POST[‘address2’];
$address3 = $_POST[‘address3’];
$postcode = $_POST[‘postcode’];
$jobid = $_POST[‘jid’];
$from = $_POST[‘from’];
$to = $_POST[‘to’];

}

[/php]

What should happen: http://www.stevedawson.com/phpinvoiceit-demo.php

Sadly it runs and runs and runs! :frowning: I echo out the array and it comes up with more than one entry even if only one line is input! Like so:


                             array(8) {
  [0]=>
  array(1) {
    ["code"]=>
    string(0) ""
  }
  [1]=>
  array(1) {
    ["desc"]=>
    string(0) ""
  }
  [2]=>
  array(1) {
    ["price"]=>
    string(0) ""
  }
  [3]=>
  array(1) {
    ["q"]=>
    string(0) ""
  }
  [4]=>
  array(5) {
    ["bv"]=>
    string(0) ""
    ["code"]=>
    string(4) "0001"
    ["desc"]=>
    string(15) "SM58 Microphone"
    ["price"]=>
    string(2) "10"
    ["quantity"]=>
    string(1) "3"
  }
  [5]=>
  array(1) {
    ["vat"]=>
    string(0) ""
  }
  [6]=>
  array(1) {
    ["subtotal"]=>
    string(0) ""
  }
  [""]=>
  array(3) {
    ["bv"]=>
    int(0)
    ["vat"]=>
    int(0)
    ["subtotal"]=>
    int(0)
  }
}

What could be the problem? As you can see I have moved away from the JS idea for this into something more robust!

Also if you want to see the script in action ask and I will PM you the login details for where it is can also send you the whole file if needed.

It looks like you are using a recursive count when you shouldn’t be.

Lets say that you have the following array: $item=array('joe'=>array('sara','jim','bob'), 'sue'=>array('sally','mark','heather'))

Given this array, a count would indicate 2, but by setting the recursive count flag, it will return 8.

Now lets use this with your for statement:[php]for($l=0; $l<count($item,COUNT_RECURSIVE); $l++){[/php]

Given the above array, this will iterate 8 times. The problem is that inside this for statement you have a number of lines like this one:[php]td><?php echo $item[$l]['code']; ?> [/php]

In particular, look at the references to your item array. For example …[php]$item[$l][‘code’][/php]

$item is a multidimensional array that only has two direct children: joe and sue. This means that you will be iterating through six non-existing elements: $item[2][‘code’] , $item[3][‘code’] , etc.

I would be more than happy to help you resolve the issue you are having. Since the file is too big to post, you are more than welcome to email it to me (my email is available from my profile). But you might try this first: Change your for to use a normal count:[php]for($l=0; $l<count($item); $l++){[/php]

I would also add an exit to the loop for debugging purposes. I would add something like this on the first line following the for statement:[php]if($looptest++ > count($item)) die(“For Count exceeded: $looptest Loops were executed”); [/php]

Then see if it executes without hitting the die.

Hope this makes sense. Please let me know if you have any questions or if it doesn’t execute properly with the change to your for loop.

jay
[/code]

That worked perfectly removing the recursive part. Although its not adding anything up. :frowning:

malasho, who was helping me with this is busy and im wondering if anyone else can help me here. This is the full file.

[php]

<?php include 'dbc.php'; page_protect(); $err = array(); $jobid = ''; $vat_at = $site_settings['taxrate']; //Add job if(isset($_POST["submit"]) && $_POST["submit"] == "Add Job"){ $customername = filter($_POST["customer"]); $add1 = filter($_POST["address1"]); $add2 = filter($_POST["address2"]); $add3 = filter($_POST["address3"]); $pc = filter($_POST["postcode"]); $to = filter(esec($_POST['to'])); $from = filter(esec($_POST['from'])); $items = filter($_POST['item']); $del = filter($_POST['delivery']); $jobid = filter($_POST["jid"]); $customer_id = $_POST['customerid']; $quote = $_POST['quote']; $totalvat = $_POST['totalvat']; $grandtotal = $_POST['grandtotal']; $linetotal = $_POST['linetotal']; if (!IsPostcode($pc)){ $err[] = "Please enter a valid postcode!"; } if ($pc == ''){ $err[] = "Please enter postcode!"; } if ($add1 == ''){ $err[] = "Please enter an address!"; } if ($to == ''){ $err[] = "Please enter a to date!"; } if ($from == ''){ $err[] = "Please enter a from date!"; } if ($customer_id == ''){ $err[] = "Please enter a customer!"; } if(empty($err)){ $sql2 = "INSERT INTO jobs (job_id, contact_name, phone, address1, address2, address3, postcd, customer_id, mobile, comments, dateadded, email, customer_id, quote, before_vat, vat, full_total) VALUES ('$jobid', '$customername', '$phone', '$add1', '$add2', '$add3', '$pc', '$name', '$mobile', '$comments', '$date', '$email', '$customer_id', '$quote', '$linetotal', '$totalvat', '$grandtotal')"; foreach($item as $value){ $i = ltrim($value['code'], "0"); $q = $value['q']; $d = $value['desc']; $p = $value['price']; $bv = $value['bv']; $vat = $value['vat']; $subtotal = $value['bv']; $sql = "INSERT INTO job_items (job_id, quantity, product_id, unit_price, description, before_vat, vat, subtotal) VALUES ('$jobid', '$q', '$i', '$p', '$d', '$bv', '$vat', '$subtotal');"; $result = mysql_query($sql); } $result2 = mysql_query($sql2); $sql3 = "UPDATE customers SET jobid = CONCAT_WS(',',jobid,'$jobid') WHERE customer_id = '$customer_id'"; $result3 = mysql_query($sql3); if($result2) { $success = "Job added with ID: $jobid"; } else { $err[] = 'Could not add job: ' . mysql_error(); } } } if(isset($_POST['action']) && $_POST['action'] == "Update Invoice"){ $item = $_POST['item']; $delivery = $_POST['delivery']; $customername = $_POST['customer']; $customerid = $_POST['customerid']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $address3 = $_POST['address3']; $postcode = $_POST['postcode']; $jobid = $_POST['jid']; $from = $_POST['from']; $to = $_POST['to']; } ?> <?php echo $site_settings['site_name'];?> Management System
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
	<script type='text/javascript' src='js/jquery.autocomplete.js'></script>
</head>
<body>
	<div id="wrapper">
		<div id="header">
			<div id="logo">
				<h1><a href=""><?php echo $site_settings['site_name'];?> Management System</a></h1>
			</div>
            
            <!-- Menu start -->
			<div id="menu">
				<ul>
					<?php include('inc/menu.php');?>
				</ul>
				<br class="clearfix" />
			</div>
            <!-- menu end -->

		</div>
        
		<div id="page">
        
        
            <!-- Content start -->
			<div id="content">
				<div class="post">
					<h2>Welcome <?php echo $_SESSION['user_name'];?></h2>
					
                    <h3>Job Add</h3>
                    
                    <?php 	
						if(!empty($err))  {
   							echo "<div class=\"err\">";
  								foreach ($err as $e) {
    								echo "$e <br/>";
    							}
  							echo "</div><br/><br/>";	
   						}
   
   						if($success != ''){   
	 						echo "<div class=\"msg\">$success <br></div><br/><br/>";	
   						}
					?>

						<form action="add_job.php" method="post">
                            <p><label for="customer">Contact Name </label><input name="customer" type="text" size="30" value="<?php echo $customername;?>" class="form-text" id="customer" />
							<input name="customerid" type="hidden" value="<?php echo $customerid;?>"  class="customerid" id="customerid"/>                  </p>
                              
                             <p><label for="jobid">Job ID / Quote ID</label>
                             <input name="jid" type="text" value="<?php if($jobid == ''){ echo jobnum();} else {echo $jobid;} ?>" class="form-text" />
                             </p>
                              
                   			<p><label for="address">Venu Address </label><input name="address1" type="text" class="form-text" value="<?php echo $address1; ?>"/>
                    		<label for="address2">&nbsp;</label><input name="address2" type="text" class="form-text" value="<?php echo $address2; ?>"/>
                    		<label for="address3">&nbsp;</label><input name="address3" type="text" class="form-text" value="<?php echo $address3; ?>"/>
                    		<label for="postcode">Postcode </label><input name="postcode" type="text" class="form-text" value="<?php echo $postcode; ?>"/>
							</p>
                            
                            <p><label for="from">Dates Hired From:</label><input name="from" id="from" type="text" class="form-text" value="<?php echo $from; ?>"/><label for="to">To:</label><input name="to" id="to"type="text" class="form-text" value="<?php echo $to; ?>"/></p>
                            
                            <p><label for="equipment">Equipment</label>
                            
                <table id="invoiceitems" class="gridtable">
                <thead>
                    <tr>
                    <th><strong>Item ID</strong></th>
                    <th><strong>Description</strong></th>
                    <th><strong>Cost</strong></th>
                    <th><strong>Qty</strong></th>
                    <th><strong>Nett</strong></th>
                    <th><strong>VAT</strong></th>
                    <th><strong>Gross</strong></th>
                    </tr>
                </thead>
               
                <tbody>
                <tr class="noborder">
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                <?php 
					$linetotal = 0;
					$totalvat = 0;
					$grandtotal = 0;
					
					echo count($item);
					for($l=0; $l<count($item); $l++){
				?>
                	<tr>                            
						<td><?php echo $item[$l]['code']; ?> <input type="hidden" name="item[<?php echo $l; ?>][code]" value="<?php echo $item[$l]['code']; ?>" /></td>
                        
						<td><?php echo $item[$l]['desc']; ?> <input type="hidden" name="item[<?php echo $l; ?>][desc]" value="<?php echo $item[$l]['desc']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].number_format($item[$l]['price'],2); ?> <input type="hidden" name="item[<?php echo $l; ?>][price]" value="<?php echo $item[$l]['price']; ?>" /></td>
                        
						<td><?php echo $item[$l]['q']; ?> <input type="hidden" name="item[<?php echo $l; ?>][q]" value="<?php echo $item[$l]['q']; ?>" /></td>
                        
                        
                        <?php $bfvat = $item[$l]['price']*$item[$l]['q']; ?>
						<td><?php echo $site_settings['currency'].number_format($bfvat,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][bv]" value="<?php echo $bfvat; ?>" /></td>
                        
                        
                        <?php $vat = $bfvat*($vat_at/100); ?>
						<td><?php echo $site_settings['currency'].number_format($vat,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][vat]" value="<?php echo $vat; ?>" /></td>
                        
                        
                        <?php $subtotal = $vat + $bfvat; ?>
						<td><?php echo $site_settings['currency'].number_format($subtotal,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][subtotal]" value="<?php echo $subtotal; ?>" /></td>
                        
					</tr>
                <?php	

					$linetotal += $item[$l]['bv']+$delivery['price'];
					
					$totalvat += ($linetotal+$delivery['price'])*($vat_at/100);
					
					$grandtotal = $linetotal + $totalvat;
					 
					}
				?>
                </tbody>
                
                <tfoot>
                <tr>
                    	<td><input type="text" name="item[<?php echo count($item);?>][code]" id="code" class="form-text-price" value="" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][desc]" id="desc" class="form-text" value="" /></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][price]" id="price" class="form-text-price" value="0.00" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][q]" id="q" class="form-text-q" value="0" /></td>
                        
                    	<td>&nbsp;</td><td>&nbsp;</td><td><input name="action" type="submit" value="Update Invoice" class="form-submit"></td>
                    </tr>
                    <tr>
                    	<td><strong>Delivery</strong></td>
                    	<td><input type="text" name="delivery[desc]" id="del-desc" class="form-text" value="" /></td>
                    	<td><input type="text" name="delivery[price]" id="del-price" class="form-text-price" value="0.00" readonly/></td>
                        <td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                    	<td>&nbsp;</td>
                        <td><strong>Delivery Cost:</strong><?php echo $delivery['desc']; ?> <input type="hidden" name="delivery[desc]" value="<?php echo $delivery['desc']; ?>" /></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><?php echo $site_settings['currency'].number_format($delivery['price'],2); ?> <input type="hidden" name="delivery[price]" value="<?php echo $delivery['price']; ?>" /></td>
                    </tr>
                    <tr>
                    	<td>&nbsp;</td>
                        <td><strong>Subtotal:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($linetotal,2); ?><input type="hidden" name="linttotal" value="<?php echo $linetotal; ?>" /></td>
                    </tr>
                    <tr>
                    	<td>&nbsp;</td>
                        <td><strong>Vat total:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($totalvat,2); ?><input type="hidden" name="totalvat" value="<?php echo $totalvat; ?>" /></td>
                    </tr>
                    <tr>
                    	<td>&nbsp;</td>
                        <td><strong>Grand total:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($grandtotal,2); ?> <input type="hidden" name="grandtotal" value="<?php echo $grandtotal; ?>" /></td>
                    </tr>
                    
                </tfoot>
                
                </table>
                            
                            </p>
                            
                            <p><label for="quote">Quote or Job</label>
                            <select name="quote">
                            	<option value="1">Quote</option>
                                <option value="0">Job</option>
                            </select>
                            
                            </p>
                            
            				<p align="center"><input name="submit" class="form-submit" type="submit" value="Add Job" /></p>
		
								
  </form>
                         <?php var_dump($delivery); ?>
				</div>
	                    
				<br class="clearfix" />
			</div>
            <!-- Content end -->
            
            
            <!-- Side Bar start -->
			<div id="sidebar">
				
				<div class="post">
					<h3>Up and coming jobs</h3>
					<p>
						Dignissim cum iaculis malesuada dolor egestas nec nascetur. Praesent montes laoreet augue duis posuere. Magnis mus pellentesque sollicitudin nisl libero.
					</p>
				</div>
			</div>
           <!-- Side Bar end -->
           
           
			<br class="clearfix" />
		</div>
	</div>
    
    
   <!-- Footer start -->
	<div id="footer">
		&copy; <?php echo date('Y '); echo $site_settings['site_name']; ?> | System by <a href="http://www.kalaxhosting.co.uk/">Kalax Hosting and Design</a>
	</div>
	<!-- Footer end -->
    
    
</body>
[/php]

THe problems are the delivery details dont save anything but it sets the array though, it doesn’t count the first row of the form when you add a row for the items. If anyone wants access to the script PM me and I will send over the login details for you to see. [/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service