...

View Full Version : multiple array into function



rschoenbach
03-15-2007, 03:45 AM
This is the code i am using to populate my $values array. I believe the way I am trying to populate this array is wrong.



$values = array();

$query = 'Select * from round_info natural join tee where player_id = 1 limit 10';
$result = mysql_query($query);
if (!$result){
die ("Could not query the database: <br />" . mysql_error());
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$values[] = $row['gross_score'] => $row['course_rating'] => $row['course_slope'];
}

$handicap_index[] = handicap_differential($values);


The function handicap_differential is also wrong i believe. I am passing in an array I want it to do some math with that array values and pass me back another array. Then with that array complete the last foreach statement and give me a handicap back.



function handicap_differential($values){
$value = array();
foreach ($values as $gross_score => $course_rating => $slope_rating){
value[] = ($gross_score - $course_rating) * 113 / $slope_rating;
}
$x = 0;
foreach ($value as $y){
$gross += $y;
$x += 1;
}
$gross = ($gross / $x) * .96;
$handicap = round($gross, 1);
return $handicap;
}


Any suggestions?

Fou-Lu
03-15-2007, 04:28 AM
mysql_fetch_array() returns a datatype of array. So technically you do not iterate through a loop to populate another array if you do not want to.
However, this appears to be a multi-dimensional array you are creating, and in that case, it is incorrect.


<?php

$array = array(
'FirstKey' => array('firstKey' => 'firstValue', 'secondKey' => 'secondValue'),
'secondKey' => array('firstKey' => 'firstValue', 'secondKey' => 'secondValue'),
'secondKey' => array('firstKey' => 'firstValue', 'secondKey' => 'secondValue'),

);

is the idea of multi-dimensions.
arr[] provides access to adding an array element, much like array_push. You cannot create an array with more than one key => value pairs:
$row['gross_score'] => $row['course_rating'] => $row['course_slope']
for example. This is trying to say that the key is $row['gross_score'] with the values $row['course_rating'] and $row['course_slope']. Depending on what the value is supposed to be, these could simply be an array:


$array[] = array($row['gross_score'], $row['course_rating'], $row['course_slope'])


This would create:


array
{
[0] => array{
'Gross_score_value',
'Course_Rating_value',
'Course_slope_value'
}
}

If this is what you want, a two dimensional array, you can alter your query to grab only the values you want (ie, gross_score, course_rating and course_slope) and dump the fetch to your array:



while ($row = mysql_fetch_array($data))
// Changes to:
$values[] = mysql_fetch_array($data)


Same concept on the foreach iterations, only one key => value pair at a time. Value may be an array, and if it is, must be reiterated through (or using array functionality)
Does this help explain how to solve what you are doing?

rschoenbach
03-15-2007, 04:46 AM
to be completely honest, i dont really know what i am trying to do.

these are the two formulas that I am working with and i might have coded them wrong.

handicap_diff = (gross_score - course_rating) x 113 / (slope_rating)

handicap = avg(best 10 of last 20 handicap_diffs) * .96

course_handicap = handicap * slope_rating /113

so if u have any clue howto code this let me know, but at this point i am pretty confused on howto accomplish this. my website can do everything perfect until i need to figure avg(best 10).

I was also thinking about doing the calculation in a query which i think would be a lot easier. then load it into an array and do a foreach statement with it.

What do you think?

Fou-Lu
03-15-2007, 05:46 AM
Sure.
Here:



$query = 'Select * from round_info natural join tee where player_id = 1 limit 10';
$result = mysql_query($query);
if (!$result){
die ("Could not query the database: <br />" . mysql_error());
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$values[] = $row['gross_score'] => $row['course_rating'] => $row['course_slope'];
}

// Change this to:
$query = "SELECT `gross_score`, `course_rating`, `course_slope`
FROM `round_info` NATURAL JOIN `tee` WHERE `player_id` = 1 LIMIT 0, 10
if (!$result = mysql_query($query))
{
die("Could not query the database: <br />\n" . mysql_error());
}
$values[] = mysql_fetch_assoc($result);

This simply creates a two dimensional array of all entries found in both the round_info and tee where playerid is 1. Now, quick note, you will probably need to control your field selects, that is, the `gross_score`, `course_rating`, etc, by prefixing which table it is from: round_info.`gross_score` for example, you will need to determine those.

As for your differential function:


$value = array();
if (is_array($values)) // Not necessary, but recommended to avoid warnings
{
foreach ($values AS $key => $value) // Key value pair only retained so you can see how it is used, not using the key:
{
// We'll split them up so we can see the names:
list ($gross_score, $course_rating, $slope_rating) = $value;
$value[] = ($gross_score - $course_rating) * 113 / $slope_rating;
}
}

The remainder of the function should be as is, but you will need to watch your variable names, $gross is not defined at this level. I'm assuming that $gross is initially zero, but should be declared before the loop.


$gross = 0;
foreach ($value AS $y)
{
...

On second thought, it appears you are mearly running a sumation and divison for an avearge on that. In that case, bypass that code completely and use:


$gross = round(((array_sum($value) / count($value)) * 0.96), 1);
return $gross;


I haven't tested any of this code (and I'll admit I've been hanging around java for the last 10 months, not php), but methinks it will work.
Let me know!

rschoenbach
03-20-2007, 07:00 AM
This is my table layout

player_id course_id tee_id score rating slope
1 1 2 123 71.40 132.00
1 5 1 142 68.20 114.00

this is my code


session_start();
include('includes/db_login.php');
include('includes/functions.php');

$query = "SELECT scorecard.score, course.rating, course.slope FROM course, scorecard WHERE course.course_id = scorecard.course_id AND course.tee_id = scorecard.tee_id AND scorecard.player_id =1";

$result = mysql_query($query);

$values[] = mysql_fetch_assoc($result);

$value = array();
if (is_array($values)) // Not necessary, but recommended to avoid warnings
{
foreach ($values AS $key => $value) // Key value pair only retained so you can see how it is used, not using the key:
{
// We'll split them up so we can see the names:
list ($score, $rating, $slope) = $value;
var_dump($value);
$value[] = (($score - $rating) * 113) / $slope;
}
}


I get an error division by zero.

Anybody have a clue?

Fou-Lu
03-20-2007, 03:39 PM
Slope has been undefined by this point (or zero)
To start with, I pointed you in the wrong direction too, the query does need a loop in it. Without the loop it will simply overwrite the last entry into the array. So we will put that back in.
With this one, you can generate the information directly from the query iteration, since you are not using a function and its player specific:



$query = ...

$values = array();
while ($row = mysql_fetch_assoc($query))
{
$values[] = (($row['score'] - $row['rating']) * 113) / $row['slope'];
}


That will return an array with each of the scores in it. If you need any of the other corresponding information (name, course, etc) you can generate a new array within the loop to create them and simply add the value to it:


$row['calcScore'] = (($row['score'] - $row['rating']) * 113) / $row['slope'];
$array[] = $row;

Um yeah I think that will work now. Sorry about the confusion.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum