PHP While loop inside another while lopp connecting to SQLSRV bug

Hello all,

I am trying to get the $totalBinTare to work, but always get a zero for the returned value. Please help!

Georgia

[php]<?php
$growerWeight = 0;
while($field = sqlsrv_fetch_array($fields))
{
$fieldWeight = 0;
$field[‘FieldDescription’] = ucwords(strtolower($field[‘FieldDescription’]));
echo “

”.$field[‘FieldDescription’]."
";
$gradesquery = “SELECT WeightReciept, Date, GrossWieght, TareWieght, NetWeight, BinsOrBulk, TruckManifest, Variety FROM dbo.Grades WHERE FeildCode = ‘{$field[‘FieldID’]}’”;
$grades = sqlsrv_query($conn, $gradesquery);
if ($grades)
{
$empty = sqlsrv_has_rows($grades);
if ($empty == 1)
{
?>





<?php
while($row = sqlsrv_fetch_array($grades))
{
				if ($row['BinsOrBulk'] == "Bulk")
				{
					$finalNetWeight = $row['GrossWieght'] - $row['TareWieght'];
				}
				elseif ($row['BinsOrBulk'] == "Bins")
				{
					$totalBinTare = 0;
					$tareMatchQuery = "SELECT BinType, WeightReciept, Quantity FROM dbo.BinGradesTies WHERE WeightReciept = '{$row['WeightReciept']}'";
					$tareMatcher = sqlsrv_query($conn, $tareMatchQuery);
					while($tareMatch = sqlsrv_fetch_array($tareMatcher))
					{
						$quantity = $tareMatch['Quantity'];
						$tareWeightQuery = "SELECT BinType, BinTare FROM dbo.BinTypes WHERE BinType = '{$tareMatch['$binType']}'";
						$tareWeight = sqlsrv_query($conn, $tareWeightQuery);
						while($tare = sqlsrv_fetch_array($tareWeight))
						{
							$binTare = $tare['BinTare'];
							$rowTare = $binTare * $quantity;
							$totalBinTare = $totalBinTare + $rowTare;
						}
					}
				}

			
				?>
				<tr>
					<td width="130"><a href="gradedetails.php?id=<?php echo $row['WeightReciept']; ?>"><?php echo $row['WeightReciept']; ?> </a><td/>	
					<td width="155"><?php echo $row['TruckManifest'];?></td>
					<td width="90"><?php if($row['Date'] == NULL){echo " ";}else {echo $row['Date']->format("m/d/y");} ?> <td/>
					<td width="110"><?php echo $row['Variety']; ?> </td>
					<td width="110" align="right"><?php echo number_format($row['GrossWieght'],0,".",",")." lbs"; ?> <td/>
					<td width="110" align="right"><?php echo number_format($row['TareWieght'],0,".",",")." lbs"; ?> <td/>
                  <td width="110" align="right"><?php echo number_format($totalBinTare,0,".",",")." lbs"; ?> <td/>  
					<td width="110" align="right"><?php echo number_format($finalNetWeight,0,".",",")." lbs"; ?> <td/>
				</tr>
				<?php	
				$fieldWeight = $fieldWeight + $row['NetWeight'];
				$growerWeight = $growerWeight + $row['NetWeight'];
			}    
			?>
		</table>
		<table>
			<tr>
				<td class="edibleyield" width="863" align="right"> <?php echo "Total ".number_format($fieldWeight, 0, '.', ',')." lbs"; ?></td>
			</tr>
		</table>
		<?php
			$fieldWeight = 0;
			echo "<br/><br/>";	
		}
		if($empty === false)
		{
			echo "<h7>No product received from this field.</h7> <br/> <br/> <br/>";
		}
	}
}

echo “

Total Weight Received: “.number_format($growerWeight, 0, ‘.’, ‘,’).” lbs
”;
sqlsrv_close($conn);
?>
[/php]
Modified::astonecipher:: Please use code tags...
Weight Receipt #
Trucking Manifest # Date
Variety Gross Weight
Tare Weight
Bin Tare
Net Weight

First, prepared statements.

I am going to go with this is a typo. What are you expecting $totalBinTare to display? Why all the loops? I have a feeling, this could be accomplished simpler.

You think you are retrieving a value from the database based on the last select query using this as the where clause value: $tareMatch[’$binType’], What do you think that value is, and what should it return?

Sponsor our Newsletter | Privacy Policy | Terms of Service