Hi all,

I have extracted this table from mysql database.
I have 7 fields all populated.The last field gives the number of copies sold by the author in that year.How can I sum this column up and place the sum beneath the column?

id author title publisher year genre sold
1 ken davies xpatrol zoot 2007 adventure 175000
2 ken davies dawn zoot 2007 adventure 260000
3 ken davies zero hour zoot 2008 adventure 279000
9 ken davies mr evans zoot 2007 adventure 155000
10 ken davies revenge zoot 2005 crime 225000
11 ken davies backdrop blue parrot 2003 crime 175000
12 ken davies firefly zoot 2004 crime 64000
13 ken davies newstime zoot 2004 adventure 450000
14 ken davies the lamp zoot 2004 crime 275000
15 ken davies switch zoot 2005 adventure 180000

Many Thanks
Saragosa

    Since you've already extracted the individual amounts all in their own array and use the [man]array_sum[/man].

      Weedpacket;11057407 wrote:

      Since you've already extracted the individual amounts all in their own array and use the [man]array_sum[/man].

      Hi Weedpacket,

      Thanks for the reply.I assume that the column to sum up would be Column 7,the sold column so I assumed that this column would be the 7th element in the array

      I tried this

       $a = array(7);
      	echo "sum(a)".array_sum($a)."\n";

      I then get the message
      Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\authors3.php on line 117

      Also a bit confused as to where the sum value would actually be placed,as the table is of a particular size and hasn't anymore cells to place the value in.
      Kind Regards
      s

        saragosa wrote:
          $a = array(7); 

        This doesn't do what you maybe think it does. This creates an array with one element at position 0 with the value 7.
        The seventh element in a numerically-indexed array would be $a[6] (see [man]arrays[/man] for details).

        I assume that the column to sum up would be Column 7,the sold column so I assumed that this column would be the 7th element in the array

        I can't tell if your assumptions are correct or not as I'm not psychic and don't know what you've written.

        Also a bit confused as to where the sum value would actually be placed,as the table is of a particular size and hasn't anymore cells to place the value in.

        Well, if you have nowhere to put the result then I can't magic one up for you from here. I expect you'd need to make one, but if you haven't got any room...

          So it back to my original question,or lets make is purely it non specific.

          How do you add a series of values up in a table.If a table is an array,then how do you add a series of numerical elements up in an array
          and how do you create a vessel on the page for storing that sum.

            So it back to my original question,or lets make is it purely it non specific.

            How do you add a series of values up in a table.If a table is an array,then how do you add a series of numerical elements up in an array
            and how do you create a vessel on the page for storing that sum.

              I could be done easily via SQL

              select author, sum(sold) as total_sold
              from your_table_name
              group by author
              

              PS: You could add a WHERE clause if you only want the total for a specific author.

                Write a Reply...