SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    iterating through array

    From another thread I recieved this code to select and count the number of rows grouped by dept id.

    This works well but I dont want to echo out the result. I need the variables to perform other calculations. How would I go about giving each one a unique name so I can use them again?

    PHP Code:
    $result mysql_query("SELECT deptid, COUNT(*) AS count FROM unitop GROUP BY deptid" );
    print 
    "<table><tr><td>Dept. ID </td><td> Count </td> </tr>";
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
    print 
    "<tr>";
    print 
    "<td>" $row["deptid"] . " </td> <td> " $row["count"] . "</td>";
    print 
    "</tr>";
    }
    print 
    "</table>"

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $rows = array();
    $result mysql_query("SELECT deptid, COUNT(*) AS count FROM unitop GROUP BY deptid" );
    print 
    "<table><tr><td>Dept. ID </td><td> Count </td> </tr>";
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC))
        
    $rows[] = $row

    that will give you a multi-dimensional array you can use later.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you cannot do the calculation during the while loop, you could do this:
    PHP Code:
    while ($row[] = mysql_fetch_array($resultMYSQL_ASSOC)) 
    You will end up with a multi-dimensional array. E.g.

    $row[0]['deptID'] <-- first row returned
    $row[1]['deptID'] <-- second row returned

    etc.

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see i did it the stupid way... oh well, lol.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, Cyborg. When I started my reply no one else had posted.

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, lol, i was complimenting you on doing it better than me, although it'd be best to do it my way if you want to do something with the data during the loop as well.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, its working now. as for calculations on the fly, this one did my head in, I should probably quit while I still am sane but if its not real difficult, here is what I am doing

    I have 5 departments which contain various numbers of rows. I want to calculate the sum or rows of each and also the sum of dept1 with dept2 and then the sum of dept 1,2 and 3 and then again the sums of dept 1,2,3,4.

    I am using the results to iterate through the rows and insert a dept heading row at the appropriate location. Here is the page if you want to see what it looks like.
    http://www.mantaproductions.com/arro.../testtable.php

    Thanks again, Im actually happy now that the whole thing is down to one query

    Cheers


Bookmarks

Posting Permissions

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