Jump to content

lindisfarne

Members
  • Posts

    11
  • Joined

  • Last visited

lindisfarne's Achievements

Member

Member (2/5)

0

Reputation

  1. In the code below I can get the total of the column into the field in a table,but how would I go about this if I wanted to get the total into a text box(html). I tried placing the $total variable into the value attribute of the text box but this didn't work.Any suggestions much appreciated:) $total = 0; // initialise total 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 "<tr />"; $total += $record['sold']; // accumulate total } echo "<tr><td colspan='6'>Total:</td><td>$total</td></tr>"; // output total echo "</table>"; Kind regards Lind
  2. Hi Barand COBOL Yes! that big bad beautiful language
  3. Aha!! got it,thanks Barand many thanks for that.Being an ancient cobolist all these curly brackets are quite confusing
  4. Hell there Barand 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>"; $total = 0; 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 "<tr />"; $total += $record['sold']; echo "<tr><td colspan='6'>Total:</td><td>$total</td></tr>"; } echo "</table>"; mysql_close($con); ?> </form> </body> </html> I have reproduced duced the code here,it is the same as my main code above but with your code added.
  5. Hi Barand, Many thanks for your reply,the problems is was that I wanted the total in just one box at the base of the "sold" array.The code you suggested results in a total after each record in the table is displayed.Is there a way I could just get a final total figure at the bottom of the sold column.Please see the result below as it stands. Any suggestions appreciated and thanks for your help. Kind Regards Lindisfarne id author title publisher year genre sold 1 ken davies xpatrol zoot 2007 adventure 175000 Total: 175000 2 ken davies dawn watch zoot 2007 adventure 260000 Total: 435000 3 ken davies zero hour zoot 2008 adventure 279000 Total: 714000 9 ken davies mr evans zoot 2007 adventure 155000 Total: 869000 10 ken davies revenge zoot 2005 crime 225000 Total: 1094000 11 ken davies backdrop blue parrot 2003 crime 175000 Total: 1269000 12 ken davies firefly zoot 2004 crime 64000 Total: 1333000 13 ken davies newstime zoot 2004 adventure 450000 Total: 1783000 14 ken davies the department zoot 2004 crime 275000 Total: 2058000 15 ken davies switch zoot 2005 adventure 180000 Total: 2238000
  6. Hi mac_gyver and cyber_robot, I cannot thank you both enough for not only brilliant advice on mysql and PDO but some great code that has has solved the current prtoblem.Many many thanks and very much appreciated Barand ,thanks for the reply,I haven't included the summing up because I still haven't figured out how to do it. Many thanks to you all for your help
  7. Hi all, The idea of this project was to create a sql query that would be created in relation to which item was selected in the drop down boxes.So for example If I clicked on an authors name,I would get up all the authors records,then if I wanted the authors+genre.I would select the author from the author drop down and the genre from the genre dropdown and so on. The idea I had was to replace the author name with a variable,so insteda of the query reading: $sql = "SELECT * FROM books WHERE author = 'Ken Davies' I replaced the name with the variable $bird which in turn was already declared as author here $bird = ( ! empty($_POST['author'])) ? $_POST['author'] : null; But the code is only partly working.Each box works individually,but they wont combine. So if I select author Ken Davies then I select genre,I get the genre up and all of the author not just ken davies and his genre. Can anyone see what I am doing wrong,any help much appreciated,I have placed all the code on here to look at. <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 #variables created and tested $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' "; } if(!is_null($author)){ $sql.="AND author = $author"; } else if (isset($cat)) { $sql = "SELECT * FROM books WHERE genre = '$cat' "; } if(!is_null($genre)){ $sql.="AND genre = $genre"; } else if (isset($mouse)) { $sql = "SELECT * FROM books WHERE year = '$mouse' "; } if(!is_null($year)){ $sql.="AND year = $year"; } else if (isset($goat)) { $sql = "SELECT * FROM books WHERE publisher = '$goat' "; } if(!is_null($publisher)){ $sql.="AND publisher = $publisher"; } $myData = mysql_query($sql,$con); echo"<table border=3> <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 "<tr />"; } echo "</table>"; mysql_close($con); ?> note: all four are working individually<br /> not working when combined<br /> </form> </body> </html>
  8. 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
  9. 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
  10. <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.
  11. 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
×
×
  • 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.