Foreach() returns single row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flydev
    New Member
    • Feb 2008
    • 33

    Foreach() returns single row

    Hey guys, I hardly ever work with Foreach(), but I've read over the procedures for it and I cant seem to figure out why this is only returning a single record. I could be way off base on the proper use here. Note this is test code for trying to figure out whats going on, so none of the function elements are being used, but it illustrates my problem.

    The array is storing the entire table with 18 columns and 3 rows.

    Here is the function being called:
    [CODE=PHP]function topPilots($star tTime, $endTime, $orderBy) // Retrieves list of pilots order
    {
    $sql = "SELECT * FROM completedFlight s";
    $result = mysql_query($sq l);

    $array = mysql_fetch_arr ay($result, MYSQL_ASSOC);

    return $array;
    }[/CODE]

    Here is the function in use:
    [CODE=PHP]$array = topPilots($this MonthStart, time(), "SUM(cargo) ");

    foreach($array as $key => $value)
    {
    echo $key.": ".$value." <br />";
    }[/CODE]
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    since this is a test case it's not easy to tell.
    first make sure you actually have more than one entry in $array (use var_dump($array )).
    second, is "SUM(cargo) " supposed to be a string?

    regards

    Comment

    • flydev
      New Member
      • Feb 2008
      • 33

      #3
      SUM(cargo) is a string that is inputted into the function topPilots, but it is not being used in this case as the function does not use any of the elements. I did an var_dump($array ) and it only brought up the single row. I suppose its a problem with the function...

      Will returning an array in a function do this?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        mysql_fetch_arr ay will only retrieve a single row from your result set, storing the fields in an array.

        To get the entire table into a array, you would have to do something like:
        [code=php]
        $table = array();
        while($row = mysql_fetch_arr ay($result)) {
        $table[] = $row;
        }
        [/code]
        Also, rather than using mysql_fetch_arr ay and pass MYSQL_ASSOC, you could simply use the mysql_fetch_ass oc function.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by flydev
          I did an var_dump($array ) and it only brought up the single row. I suppose its a problem with the function...
          could be as well a problem with the SQL or the database.

          Comment

          • flydev
            New Member
            • Feb 2008
            • 33

            #6
            Originally posted by Atli
            Hi.

            mysql_fetch_arr ay will only retrieve a single row from your result set, storing the fields in an array.

            To get the entire table into a array, you would have to do something like:
            [code=php]
            $table = array();
            while($row = mysql_fetch_arr ay($result)) {
            $table[] = $row;
            }
            [/code]

            Ahhh, thats the key...so I will have to build a new array using a WHILE() loop inside the function, and then return that new array?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by flydev
              Ahhh, thats the key...so I will have to build a new array using a WHILE() loop inside the function, and then return that new array?
              yep.&#160;&#160 ;&#160;&#160;&# 160;&#160;&#160 ;&#160;&#160;&# 160;&#160;&#160 ;&#160;&#160;&# 160;&#160;&#160 ;&#160;&#160;&# 160;

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Yes. You will have to build a two-dimensional array to represent the table, like my example shows.

                Comment

                • flydev
                  New Member
                  • Feb 2008
                  • 33

                  #9
                  Thanks guys! I dont know why I had it stuck in my head that WHILE() is used to loop through an existing array...i forget you pass a query when using it. Thanks again!

                  Comment

                  Working...