Jump to content

[SOLVED] Break, Rebuild Array


scottybwoy

Recommended Posts

This one is proving a bit too tough for me, hope some of you clever ones out there can show me the light.

 

I have a big form that lists all our products.  Each product has 5 values : Cost, Dist, Special, Resel, & RRP.

 

The form for updating the values easily as prices can change on a monthly basis.

 

However when the form is sent as just one big array.  What I need to do is break this down into an array for each product.

 

There are two ways I have tried to break this down, but I just can't get it to work.

 

The first way I tried was by sending the model Id with the column name separated by a ',' and then exploding it, but I couldn't quite work out the grouping.

 

The second way I tried was to send the model Id as it's own readonly input field, but again couldn't work out how to do the count of 6 fields to group them together.  Here is some code for you to get to grips with it.  Thanks for you time.

This generates the form

<?php
function priceList() {
global $sql;
$sql = mssql_query("SELECT * FROM proType");

echo "<table style='width:100%; border:solid 1px navy; border-collapse:collapse;'><tr><th>Model Id</th><th>Cost</th><th>Distrib</th><th>Special</th><th>Resell</th><th>RRP</th></tr>";
while ($row = mssql_fetch_assoc($sql)) {
	$tpId = $row['pTypeId'];
	$type = $this->selectRecord('pType', 'proType', 'pTypeId', $tpId);
	echo "<tr><th colspan='6' bgcolor='navy'><font  color='white'>$type</font></th></tr>";

	$qry = "SELECT modelId, cost, distPrice, specialPrice, reselPrice, rrp FROM products WHERE endOfLine = 'False' AND pTypeId = $tpId";
	$iSql = mssql_query($qry);

	while ($iRow = mssql_fetch_assoc($iSql)) {
		$i++;

		if ($this->isodd($i)) {
			echo "<tr class='row1'>";
		} else {
			echo "<tr class='row2'>";
		}

		foreach ($iRow as $k => $v) {
/*				if ($k == 'modelId') {
				$modelId = $v;
			} --// Method 1  */

			echo "<td padding='1.5px' align='right'>";
			if (is_float($v)) {
//					echo "<center><input type='text' value='" . $v . "' name='" . $modelId . "," . $k . "' style='width:45px; text-align:center;' /></center>"; --// Method 1
				echo "<center><input type='text' value='" . $v . "' name='" . $k . "' style='width:45px; text-align:center;' /></center>";

			} else {
//					echo $modelId; --// Method 1
				echo "<input type='text' value='" . $v . "' name='modelId' style='width:100%; text-align:right;' readonly/>";
			}

			echo "</td>";
		}

		echo "</a></tr>";
	}

}

echo "</table>";
}
?>

 

And this is what tries to break it down and build the new array.  Method 1.

<?php

function updatePrice($update) {

unset($update['content']);
unset($update['update']);

$products = array();

function makeArray($val, $key) {
	global $products;

	$arr = explode(',', $key);
	$modelId = $arr[0];
	$header = $arr[1];

	if (!is_array($products[$modelId])) {
		$modelId = array();
		array_push($products, $modelId);
	} else {
		$products[$modelId][$header] = $val;
	}

}		

array_walk_recursive($update, 'makeArray');
print_r($products);

}

Method 2.

<?php
function updatePrice($update) {

unset($update['content']);
unset($update['update']);

foreach ($update as $k => $v) {
	for ($i = 0; $i < 6; $i++) {
		if ($k = 'modelId') {
			$modelId = $v;
			$modelId = array();
		} else {
			array_push($modelId, $v);
		}
	}
}
}
?>

 

As you can probably see neither of these work.  The closest I have got is by having lots of little arrays of two key => value pairs and have not been able to push them together.  Any help would be greatly received, I've been working on this for over a day now.

Link to comment
https://forums.phpfreaks.com/topic/55068-solved-break-rebuild-array/
Share on other sites

i am not quite sure that i understand what you want to do here but i 'll give it a try...

 

i guess you want to select each product in its own array right?

 

i guess then that the way you create the form is incorrect... It should be like name="products[product category here][product]"

then you could easily access all the products under a certain category using a simple foreach loop ex.:

 

foreach ($_POST['products']['product category here'] as $product)
{
    // actions here
}

 

i hope i understood what you were trying to do here...

Yes that sounds like a viable option.  I was not aware that you could post arrays via post.

 

However it appears that you can't unless I am doing it wrong.  My form creates the input fields like so :

<?php
echo "<input type='text' value='$v' name='" . $modelId . "['" . $k . "']' style='width:45px; text-align:center;' />";
?>

 

Which produces the table correctly and the input fields look like so :

 

<input type='text' value='0' name='KVM1.8['cost']' style='width:45px; text-align:center;' />

<input type='text' value='1' name='KVM1.8['distPrice']' style='width:45px; text-align:center;' />

<input type='text' value='2' name='KVM1.8['specialPrice']' style='width:45px; text-align:center;' />

<input type='text' value='3' name='KVM1.8['reselPrice']' style='width:45px; text-align:center;' />

<input type='text' value='4' name='KVM1.8['rrp']' style='width:45px; text-align:center;' />

 

However the a print_f on the returned array seems to only hold the last value (rrp) for the whole array like so :

 

Array ( [KVM1_8_] => 4 [KVM3_0_] => 6 ...  ... )

 

Any ideas, how to get around this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.