I usually dont have a problem with simple CRUD work, but these queries in particular are boggling my mind. looking at it now, i may have an extra foreach... anyone have any ideas how i can properly execute this?

the code populates a form based on each distinct "dealer_id" (user) and populates a text field for every column in the table (dynamic amount of columns, based on new products, etc)... trouble is, it's iterating the last "dealer_id" (id 4) when populating the data in the fields...

for instance:
id comp1 dealer_id WTFwater vms water
---------------------------------------------------------------------------
3 7.00 vms 3.00 9.00
---------------------------------------------------------------------------
4 8.00 Boston 4.00 10.00

a (not so) working example can be found @
http://dealers.vmsalarms.com/portal/cart/admin/dealerPrice/populateDealer5.php

code (starting to become spaghetti):

$resultDealer = mysql_query("select distinct `dealer_id` from $dbname.dealerPrice order by `dealer_id` asc");

$dealerArray = array();
while ($rowSet = mysql_fetch_array($resultDealer)) {
	array_push($dealerArray, $rowSet["dealer_id"]);  
}


foreach ($dealerArray as $value1) {

$sql = "SHOW COLUMNS FROM $dbname.dealerPrice";
$result = mysql_query($sql);

echo "<span style=\"color:#0066ff\">" . $value1. "</span><br />";
echo "<form action=\"processPrice.php?action=add\" method=\"post\" enctype=\"multipart/form-data\" name=\"frmAddPrice\" id=\"frmAddPrice\">";

while ($row = mysql_fetch_row($result)) {
		$equipArray = array();
		$equipArray2 = array_push($equipArray, $row[0]);
		foreach ($equipArray as $value) {
					
					$resultPrice = mysql_query("select * from $dbname.dealerPrice");
					$priceArray = array();
					while ($rowSet1 = mysql_fetch_array($resultPrice)) {
						array_push($priceArray, $rowSet1);
						foreach ($priceArray as $price){
						
							$equipPrice = $price[$value];
						}	
					}
					
				
					$equipId = "<div>" .  $value . "<input value='". $equipPrice ."' name='" . $value . "' id='" . $value . "' /></div>";
					
	}
	
	echo $equipId;
}
echo "<input name=\"btnAddUser\" type=\"button\" id=\"btnAddUser\" value=\"Add Price\" class=\"box\" />
	 </form>";
}

any insight or direction will be HUGELY appreciated....

Recommended Answers

All 2 Replies

Firstly, don't put this inside a loop

$sql = "SHOW COLUMNS FROM $dbname.dealerPrice";
$result = mysql_query($sql);

That's going to be the same for each iteration of the loop so you're wasting database time. Do it above the loop and store the result in an array.

Secondly, don't use value1 as a variable name, pick something meaningful.

Thirdly, I have no idea what your 2 inner loops are trying to do and you're tossing arround array_push like there's no tomorrow, []= exists for a reason.

ok.... solved it!
i rebuilt and realized i DID have an extra foreach that was confusing me AND the logic of the loop... lol...
the reason i needed to do it this way is because there is individual pricing on a per user basis...

here is the working code...

$resultDealer = mysql_query("select distinct `dealer_id` from $dbname.dealerPrice order by `dealer_id` asc");

$dealerArray = array();
while ($rowSet = mysql_fetch_array($resultDealer)) {
	array_push($dealerArray, $rowSet["dealer_id"]);  
}




foreach ($dealerArray as $valueDealer) {

	echo "<form action=\"processPrice.php?action=add\" method=\"post\" enctype=\"multipart/form-data\" name=\"frmAddPrice\" id=\"frmAddPrice\">";
	echo "<span style=\"color:#0066ff\">" . $valueDealer. "</span><br />";
		
	$result = mysql_query("SHOW COLUMNS FROM $dbname.dealerPrice");
	$productArray = array();
	
	while ($rowSet = mysql_fetch_array($result)) {
		array_push($productArray, $rowSet[0]);  
		$product = $rowSet[0];
		
	}	
	
		
		foreach($productArray as $productField) {
		$resultPrice = mysql_query("select $productField from $dbname.dealerPrice where dealer_id = '$valueDealer'");
		while ($rowPrice = mysql_fetch_row($resultPrice)) {
			
		
			$equipId = "<div>" .  $productField . "<input value='". $rowPrice[0] ."' name='" . $productField . "' id='" . $productField . "' /></div>";	
			echo $equipId;	
		}
		}
		
	
	echo "<input name=\"btnAddUser\" type=\"button\" id=\"btnAddUser\" value=\"Add Price\" class=\"box\" />";
	echo "</form>";

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.