Jump to content

Recommended Posts

Hi all,

 

Very new to all of this so I am looking for some help From mysql database I have used a loop to populate a table that has  7 fields in it,and 20 rows.One of the fields(columns) has numbers in it.I wanted a way to sum those numbers up and echo it out somewhere onto the screen.
I suppose technically I am trying to sum up the contents of an array.

Any help appreciated.

Lindisfarne

 

Link to comment
https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/
Share on other sites

Do you put the rows from the database into an array before outputting to the html table? If so, you could get the total from that array.

 

If not, and you output each table row as you process the query results then you can accumulate the total as output each row.

total=0;
while fetch next row
    total += row value
    output the table row
end while
output total

Not if it is a multidimensional array, and it probably would be.

$rows = [
            ['name'=>'aaa', 'value'=>55.00],
            ['name'=>'bbb', 'value'=>25.00],
            ['name'=>'ccc', 'value'=>100.00],
            ['name'=>'ddd', 'value'=>255.00],
            ['name'=>'eee', 'value'=>65.00]
        ];

echo array_sum(array_column($rows, 'value'));  //--> 500

<html>
    <head>
        <title>My Page</title>
    </head>
    <body>
        <br>
        <form name="myform" action="authors3.php" method="POST">
            <select name="author" size="2">
            <option value="ken davies">ken davies</option>
            <option value= "arthur smith">arthur smith</option>
            <option value="gill rafferty">gill rafferty</option><br />
            <option value="molly brown">molly brown</option><br />
            <option value="gilbert riley">gilbert riley</option><br />
            <input type = "submit" name = "submit" value = "go">

            <select name="genre" size="4">
            <option value="adventure">adventure</option>
            <option value="biography">biography</option>
            <option value="crime">crime</option><br />
            <option value="romance">romance</option>
            <option value="thriller">thriller</option>
            <input type = "submit" name = "submit" value = "go">

            <select name="year" size="4">
            <option value="2002">2002</option>
            <option value="2003">2003</option>
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <input type = "submit" name = "submit" value = "go">

            <select name="publisher" size="4">
            <option value="blue parrot">blue parrot</option>
            <option value="yonkers">yonkers</option>
            <option value="zoot">zoot</option>
            <input type = "submit" name = "submit" value = "go">

                <?php
                    $bird = (!empty($_POST['author'])) ? $_POST['author'] : null;
                    $cat = (!empty($_POST['genre'])) ? $_POST['genre'] : null;
                    $mouse = (!empty($_POST['year'])) ? $_POST['year'] : null;
                    $goat = (!empty($_POST['publisher'])) ? $_POST['publisher'] : null;

                    $con = mysql_connect("localhost","root","");
                    If (!$con) {
                        die("Can not Connect with database" .  mysql_error());
                    }
                    mysql_select_db("authors",$con);


                    if (isset($bird) && isset($cat) && isset($mouse) && isset($goat)){  
                        $sql = "SELECT * FROM books WHERE author = '$bird'
                                AND genre = '$cat' AND year = '$mouse' AND
                                publisher = '$goat' ";
                    }
                    else if (isset($bird)) {
                        $sql = "SELECT * FROM books WHERE author = '$bird' ";
                    }
                    else if (isset($cat)) {
                        $sql = "SELECT * FROM books WHERE genre = '$cat' ";
                    }
                    else if (isset($mouse)) {   
                        $sql = "SELECT * FROM books WHERE year = '$mouse' ";    
                    }       
                    else if (isset($goat)) {
                        $sql = "SELECT * FROM books WHERE publisher = '$goat' ";    
                    }   

                    $myData = mysql_query($sql,$con);

                    echo"<table border=1>

                    <tr>
                        <th>id</th>
                        <th>author</th>
                        <th>title</th>
                        <th>publisher</th>
                        <th>year</th>
                        <th>genre</th>
                        <th>sold</th>
                        </tr>";

                    while($record = mysql_fetch_array($myData)){
                        echo "<tr>";
                        echo "<td>" . $record['id'] . "</td>";
                        echo "<td>" . $record['author'] . "</td>";
                        echo "<td>" . $record['title'] . "</td>";
                        echo "<td>" . $record['publisher'] . "</td>";
                        echo "<td>" . $record['year'] . "</td>";
                        echo "<td>" . $record['genre'] . "</td>";
                        echo "<td>" . $record['sold'] . "</td>";
                        echo "<td>" . $record['sold'] . "</td>";
                        echo "<tr />";
                    }
                    echo "</table>";
                    
                    mysql_close($con);
                ?>
                note: all four are working<br />
                all work individually<br />
                two or three dont work together
        </form>
    </body>
</html>

Hi all,

thanks for the replies,perhaps it would be better if I placed the whole project up here.What I was trying to do was create four drop down boxes,author,genre,year and publisher.I would then combine these  to produce the table.

The table consists of 7 fields which are shown below:The column I wanted to sum was the last column(SOLD)

 

  author title publisher year genre sold 16 arthur smith angel blue parrot 2006 crime 249000 17 arthur smith which one blue parrot 2006 crime 180000 18 arthur smith taxi to nowhere blue parrot 2007 crime 380000 19 arthur smith watershed zoot 2008 adventure 134000 20 arthur smith out of time zoot 2007 adventure 236000 22 arthur smith goldmine blue parrot 2004 crime 22500 23 arthur smith heart of the matter zoot 2008 biography 178000 24 arthur smith the life of eddy bluesocks blue parrot 2005 biography 98000

The project at this stage doesn't work too well.If I click on all 4 boxes,it works,and all the boxes work individually,but if I try say two or three boxes together it doesn't work.So if anybody here wants a go at solving that problem as well as the sum problem you are more than welcome :) :)

 

Many thanks abd again thankyou for the replies.

 

 

Hi Barand

I couldn't get your code to work simply because I wasn't too sure where to place it in my code.That's why I put the whole project up here(with other problems I have) to see if it puts it into a better perspective.

 

Kind Regards
Lindisfarne 

Hi Barand

I couldn't get your code to work simply because I wasn't too sure where to place it in my code.

 

Show us  what you have now. The pseudocode I posted (repeated below) showed you exactly where it goes.

total=0;
while fetch next row
    total += row value
    output the table row
end while
output total

Or did you post the whole code so I could do the amendments for you?

Hi Barand
Thanks for the reply
I posted the whole code because that would give people an insight to what i am trying to do.the whole code has problems also,as i mentioned,and any help with that would also be appreciated.as it stands I cant see precisely where your loop would fit into the code.would it go before or after the existing while loop,where would the output go(the sum).

 

Kind Regards
Lind

I don't see your problem - your code only has one while() loop.

 

As in my pseudocode (which shows how your program should work),

  • initialize the total to 0 before the loop.
  • Inside the loop you accumulate the total.
  • After the loop output a row containing the total

As for your search problem, you only include a condition in the WHERE clause if a value is provided for that condition.

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.