Incremement Value in Foreach Loop

Hi all, I’m trying to echo a field in a foreach loop and make the field name increment each time so I would have fields ‘name1’, ‘name2’, etc. Tried having a go but can’t seem to make it work. Think I’m pretty close but I must be doing something not quite correct.

while ($row = mysql_fetch_array($search)){
					echo '<div class="searchRes">
					<span class="searchRow" style="width: 20px;">'.$row['customer_ref'].'</span>
					<span class="searchRow" style="width: 65px;">'.$row['c_title'].' '.$row['c_firstname'].' '.$row['c_lastname'].'</span>
					<span class="searchAddress" style="width: 100px;">'.str_replace(",",",<br/>", $row['c_address']).'</span>
					<span class="searchRow" style="width: 70px;">'.$row['c_number'].'</span>
					<span class="searchRow" style="width: 115px;">'.$row['c_email'].'</span>
					<span class="searchRow" style="width: 60px;">'.$row['c_dob'].'</span>
					<span class="searchRow" style="width: 100px;">'.$row['c_regdate'].'</span>
					<span class="searchRow" style="width: 70px;">'.$row['c_notes'].'</span>
					<span class="searchRow" style="width: 700px; margin-top: 10px;">
					<span class="searchRow"><a href="createorder.php?id='.$row['customer_ref'].'">Add Order</a></span>
					<span class="searchRow"><a href="editcust.php?id='.$row['customer_ref'].'">Edit Customer</a></span>
					<span class="searchRow"><a href="deletecust.php?id='.$row['customer_ref'].'">Delete Customer</a></span>
					<span class="searchRow"><a href="orderhistory.php?id='.$row['customer_ref'].'">View Order History</a></span>
					</span>
					</div>';
					
					foreach (range(0, $row['customer_ref']) as $key) {
					echo '<input type="hidden" value="" name=""><input type=\\'submit\\' name=\\'delete'.$key.'\\' onclick="return confirm(\\'Do you want to make these changes ?\\');"></span>';
					}
				}

What output are you getting with the current code? What does $row[‘customer_ref’] contain?

Hey dude, sorry, think I’ve scored a hatric for posts today! $row[‘customer_ref’] contains the customers id number such 1, 2, 3, 4, etc. Don’t think I need to use this? I’ve tweaked it slightly and it’s now echoing the right amount of fields (one) for each record instead of 50 or so when using the $row.

Trouble now is, each fields name is delete1. Instead of incrementing, but guessing this is because of the while loop the foreach is contained in. So all in all, I’m guess I’m snookered. Apologies, this must sounds really confusing!

What about this?


foreach (range(0, $row['customer_ref']) as $key => $value) {
...

$key will now increment by one every loop.

Hi Immerse, that’s perfect, exactly what I was looking for. Thanks for your time, greatly appreciated as always dude :smiley: