Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multiple array into function

    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.

    PHP Code:
    $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($resultMYSQL_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.

    PHP Code:
    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($gross1);
        return 
    $handicap;

    Any suggestions?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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:
    PHP Code:
    $array[] = array($row['gross_score'], $row['course_rating'], $row['course_slope']) 
    This would create:
    Code:
    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:
    PHP Code:

    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sure.
    Here:
    PHP Code:

    $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($resultMYSQL_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:
    PHP Code:
    $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.
    PHP Code:
    $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:
    PHP Code:
    $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!
    Last edited by Fou-Lu; 03-15-2007 at 05:49 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP 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?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:

    PHP Code:
    $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:
    PHP Code:
    $row['calcScore'] = (($row['score'] - $row['rating']) * 113) / $row['slope'];
    $array[] = $row
    Um yeah I think that will work now. Sorry about the confusion.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •