Foreach Loop Problems

Hi, I have a for each loop, I’m wanting to search the database to see how many rows there are of that persons order (which I have done), then display and number of input fields using a range (which I have done). I then want to put into the value of each of the input fields whatever is in the database…at the moment the same product is being echoed out in all of the range fields. See my code below, apologies for sounding complicated.

<div class="headerTitles">Edit Items in Order</div>
<form action="<?php echo $_SERVER['PHP_SELF']."?section=addtoorder&".$urlCarry ?>" method="post">

<?php
$rowNums = mysql_query("SELECT *,count(*) as total_record FROM order_products WHERE `receipt_id` LIKE '%$ref%'");
	$rows = mysql_fetch_array($rowNums);
foreach(range(1,$rows['total_record']) as $num){
include('includes/ordermanage/fields.php');
}
?>
<div class="headerTitles"><input type="submit" value="Submit" name="submit" id="subBtn" /></div>
</form>

fields.php


<!-- BOF Product Info -->
<div class="productInfo" style="width: 540px;">

<div class="prodContain">
<span class="fontField bold">Quant</span>
<input type="text" name="products[<?=$num?>][quantity]" <?=$disabled?> value="<?php echo ($rows) ? $rows['product_quant']:$_POST['products'][$num]['quantity'] ?>" class="prodName" style="width: 20px;" />
</div>

<div class="prodContain">
<span class="fontField bold">Product Description</span>
<input type="text" name="products[<?=$num?>][name]" <?=$disabled?> value="<?php echo ($rows) ? $rows['product_name']:$_POST['products'][$num]['name'] ?>" class="prodName" style="width: 200px" />
</div>

<div class="prodContain">
<span class="fontField bold">Serial Number</span>
<input type="text" name="products[<?=$num?>][serial]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['serial'] ?>" class="prodName" style="width: 70px" />
</div>

<div class="prodContain">
<span class="fontField bold">Stock Number</span>
<input type="text" name="products[<?=$num?>][stock]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['stock'] ?>" class="prodName" style="width: 70px" />
</div>

<div class="prodContain">
<span class="fontField bold">Item Price</span>
<input type="text" name="products[<?=$num?>][price]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['price'] ?>" class="prodName" style="width: 70px;" />
</div>
<!-- BOF New/Used -->
<div style="float: left; width: 650px; margin-top: 5px; color: #444444;"><b>Product Condition</b> 
<input type="radio" name="products[<?=$num?>][cond]" value="New" <?php if ($_POST['products'][$num]['cond']=="New") echo "checked"; ?> /> New
<input type="radio" name="products[<?=$num?>][cond]" value="Used" <?php if ($_POST['products'][$num]['cond']=="Used") echo "checked"; ?> /> Used
</div>
<!-- EOF New/Used -->
</div>
<!-- EOF Product Info -->

So in short, think I need to incorporate a while loop in there but unsure of the best way to do this.

Why don’t you test your mysql queries before assuming you got errors with your php code? I’ll just advise that you should:

  1. Get a proper GUI (SQLYog for example) for working with MySQL.
  2. Test your queries inside it.
  3. Ask yourself why do you use LIKE %% when you got invoice reference number which can produce exact match.

I’ll take a look into a GUI, I just use Coda, I find it works pretty well. Good point regarding use of the %%, completely unnecessary, hence why I’m still learning. Still doesn’t fix my problem. I need to implement a while loop (I think) inside the foreach… :confused:

So did you try to implement it before asking for help? It’s not like you’ll crash the universe if you learn by trial and error :slight_smile:

Hey dude, yeah I placed a while loop just below the foreach but didn’t do anything, tried setting the variables for each field for it to be echoed into but to no avail. Think I’ll forget attempting to do this particular feature :slight_smile: Cheers for the help anyhow!

Hi codxdabd,

I think your problem is include the fields.php file, php don’t allow you use include() to include a file more than once, if you do that, you will get the error except you use include_once. But the include_once in this context won’t work.

So I suggest you should assign all your html code in fields.php to a variable and include that file out of foreach scope or any of your loop.