lindisfarne Posted September 15, 2016 Share Posted September 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/ Share on other sites More sharing options...
Barand Posted September 15, 2016 Share Posted September 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537598 Share on other sites More sharing options...
cyberRobot Posted September 15, 2016 Share Posted September 15, 2016 I suppose technically I am trying to sum up the contents of an array. Alternate solution: array_sum() - http://php.net/manual/en/function.array-sum.php Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537602 Share on other sites More sharing options...
Barand Posted September 15, 2016 Share Posted September 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537603 Share on other sites More sharing options...
lindisfarne Posted September 15, 2016 Author Share Posted September 15, 2016 <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. Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537615 Share on other sites More sharing options...
Barand Posted September 15, 2016 Share Posted September 15, 2016 I've given you two methods of getting the total. How many more do you want? Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537616 Share on other sites More sharing options...
lindisfarne Posted September 15, 2016 Author Share Posted September 15, 2016 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 RegardsLindisfarne Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537621 Share on other sites More sharing options...
Barand Posted September 15, 2016 Share Posted September 15, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537629 Share on other sites More sharing options...
lindisfarne Posted September 16, 2016 Author Share Posted September 16, 2016 Hi BarandThanks for the replyI 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 RegardsLind Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537638 Share on other sites More sharing options...
Barand Posted September 16, 2016 Share Posted September 16, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537640 Share on other sites More sharing options...
Barand Posted September 29, 2016 Share Posted September 29, 2016 Solution posted in your other topic with this code https://forums.phpfreaks.com/topic/302251-mysql-query-not-working-with-drop-down-boxes/?do=findComment&comment=1537892 Quote Link to comment https://forums.phpfreaks.com/topic/302198-summing-up-the-values-in-an-array/#findComment-1537893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.