#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Houston, TX, US
    Posts
    75
    Rep Power
    17

    Looping through an array and setting variables


    It's late and I'm not thinking straight, I'm hoping one of you awake folks can assist...

    I've created a joined table about 10 columns long and 10 rows deep. I want to scroll through these rows, do a little calculation based on the values, and then assign the result to a variable. I need to do this ten times, which at the end of the loop I'll have created ten variables.

    Here is what my join looks like:
    PHP Code:
    $result_tdetails mysql_query ("
        SELECT results.event_id, 
        results.score_qualifying, 
        results.score_semis, 
        events.event_id, 
        events.name, 
        events.date_start, 
        events.games_qualifying, 
        events.games_semis 
        FROM results, events 
        WHERE results.player_id = 4 
        AND results.event_id = events.event_id 
        ORDER BY events.event_id DESC"
    );

    $row mysql_fetch_array($result_tdetails); 
    I want to begin a loop at this point, do a little calculation, and then create two variables "event_name1" and "event_avg1", continue the loop through the array... then create "event_name2", "event_name3", etc..

    Upon completion of the loop, I want to then put those created variables into another array, like this:

    PHP Code:
    $bar_data=array(
        
    $event_name1 => $event_avg1,
        
    $event_name2 => $event_avg3,
        
    $event_name3 => $event_avg3,
                   ); 
    I'm stuck. And yawning.

    Can someone smack me upside my head and shed some light on how I can go about this? I'll even e-praise you for your efforts!

    Thanks in advance.
    Tony Melendez
    tony dizat amerigo dizot com
    http://www.amerigo.com
  2. #2
  3. This is only a test
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Off the air
    Posts
    2,884
    Rep Power
    156
    You need to start your loop with your mysql_fetch_array() line and go from there:

    PHP Code:
    while($row mysql_fetch_array($result_tdetails))
    {
      
    //set variables to put into array
      
    $event_name $row['name'];
      
    $event_avg //do your calculations here ;

      //put vars into array
      
    $bar_data[$event_name] = $event_avg;

    And that should do it.
    "Not to offend our Swedish listeners ... if we have any, that isó"
    "óBut your team's rubbish."
    (Sun webcast, Sweden vs. Paraguay)

    Who needs corporate radio?
    WeFunkRadio.com | Global Pop Conspiracy | Radio Paradise | SomaFM | The Classic Soul Network | Boot Liquor | WFMU Freeform Radio
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Houston, TX, US
    Posts
    75
    Rep Power
    17
    That worked as promised. I've never popluated an array within a loop before, but I suddenly see the power of that capability. It sounded like such an easy thing to do, and it was.

    Thank you for reading through my post.
  6. #4
  7. This is only a test
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Off the air
    Posts
    2,884
    Rep Power
    156
    No problem. Glad it worked.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Houston, TX, US
    Posts
    75
    Rep Power
    17
    Just FYI...

    I used this to create a bar graph. I didn't write that script, but I was able to hack it to do what I wanted.

    Here is what the result looks like...

    Performance Profile

    Last edited by bamaster; September 30th, 2003 at 07:05 PM.
    Tony Melendez
    tony dizat amerigo dizot com
    http://www.amerigo.com

IMN logo majestic logo threadwatch logo seochat tools logo