Jump to content

New to PHP & exploring the language, hit a snag with multidimentional arrays!


father time

Recommended Posts

Good evening all,

 

I am trying to expand my experience with PHP by doing a small project for a personal website. I am working from scratch using notepad and so far have been having great sucess. My experience with C# is helping greatly but sadly ive hit a snag and can't seem to free myself. I have an array that is populated with items, and the prices for those items if you are buying/selling them. What I will have is a form with a drop list that is populated by this array list (already working) and a text entry that you can enter a numerical quantity value, then another drop list that will have buy / sell as an option. What the page will do is take the values from the form and calculate the "total" from the supplied values. Sadly some reason I cant set a $Variable to the value of the array values. I can echo them and get the correct value, but can't set the Variable to the arrays value!

 

<?php
$ResBuySell = array(
"Iron ingot" 		=> array("Buy" => 6, "Sell" => 7),
"Shadow iron ingot" 	=> array("Buy" => 14, "Sell" => 16),
"Dull copper ingot" 	=> array("Buy" => 14, "Sell" => 16),
"Copper ingot" 		=> array("Buy" => 6, "Sell" => 7),
"Bronze ingot" 		=> array("Buy" => 6, "Sell" => 7),
"Gold ingot" 		=> array("Buy" => 6, "Sell" => 7),
"Agapite ingot" 	=> array("Buy" => 6, "Sell" => 7),
"Verite ingot" 		=> array("Buy" => 6, "Sell" => 7),
"Valorite ingot" 	=> array("Buy" => 6, "Sell" => 7),
"Feathers" 		=> array("Buy" => 6, "Sell" => 7),
"Arrows" 		=> array("Buy" => 6, "Sell" => 7),
"Bolts" 		=> array("Buy" => 6, "Sell" => 7),
"Boards" 		=> array("Buy" => 4, "Sell" => 6),
"Spined leather" 	=> array("Buy" => 16, "Sell" => 18),
"Horned leather" 	=> array("Buy" => 6, "Sell" => 7),
"Barbed leather" 	=> array("Buy" => 6, "Sell" => 7),
"Cloth" 		=> array("Buy" => 6, "Sell" => 7),
"Ribs" 			=> array("Buy" => 6, "Sell" => 7),
"Fish steaks" 		=> array("Buy" => 6, "Sell" => 7),
"Orange petals" 	=> array("Buy" => 6, "Sell" => 7),
"Red leaves" 		=> array("Buy" => 6, "Sell" => 7),
"Green thorns" 		=> array("Buy" => 6, "Sell" => 7)
	);

function GetPrice($ResType = "Iron ingot", $TranType = "Buy")
{
return $ResBuySell[$ResType][$ResType];
}

function GetQuantity()
{
return 5;
}

function GetTotal()
{
$ResPrice = GetPrice();
$ResQuanity = GetQuantity();
$ResTotal = $ResPrice * $ResQuanity;
return $ResTotal;
}

echo GetTotal();
?>

 

0

 

The problem resides here:

 

function GetPrice($ResType = "Iron ingot", $TranType = "Buy")
{
return $ResBuySell[$ResType][$ResType];
}

 

Some reason when I echo this:

 

$ResType = "Iron ingot";
$TranType = "Buy";
echo $ResBuySell[$ResType][$ResType];

 

it will return;

 

6

 

The proper value, but when I put it into a function and try to retrieve this info via the function it returns zero. I am a beginner with php and just trying to learn by trial and error, but after failing at this for 5 days I figured it was time to call in help from the big guns! Thanks in advance to anyone who can offer my any assistance with this problem!

 

The scope of $ResBuySell is outside of your function. You could either pass a reference to the array or use is as a global variable:

GetPrice($ResbuySell);
function GetPrice(&$array, $ResType = "Iron ingot", $TranType = "Buy")
{
   return $array[$ResType][$ResType];
}

 

or

 

function GetPrice($ResType = "Iron ingot", $TranType = "Buy")
{
   global $ResBuySell;
   return $ResBuySell[$ResType][$ResType];
}

Thank's a bunch, the second method worked perfectly. My next problem is obtaining information from a text entry field. I want a field to return only numeric values so I have this:

 

<script type="text/javascript">

function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;

if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

return true;
}

 

Which is called when a key is depressed in the text entry.

 

<input type="text" name="Quantity" id="Quantity" accesskey="2" tabindex="2" onkeypress="return onlyNumbers();" value="1" />

 

This works well and only allows backspace and integers to be used. However I then have a function that tries to access this text field, grab its value and return it; however, it seems I am formatting it wrong.

 

<?php
function GetQuantity()
{
$Quantity = document.CalcForm.Quantity.value;
// Add check here, if value isnt 1 or greater have it converted to 1
return $Quantity;
}
?>

 

this instead of returning the numeric values of the text entry field it keeps returning

 

documentCalcFormQuantityvalue

 

Any suggestions?

With regards to the onlyNumbers routine, try this function:

function onlyNumbers(that) {
  that.value=that.value.replace(/[^0-9]/g,"");
}

 

Apply it to an input box like this:

<input type="text" name="age" onkeyup="javascript:onlyNumbers(this);">

 

Now, with regards to your PHP function shouldn't it read:

function GetPrice($ResType = "Iron ingot", $TranType = "Buy") {
   global $ResBuySell;
   return $ResBuySell[$ResType][$TranType];
}

 

The way you had it the array wasn't referencing the TranType - only ResType - twice!

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.