Jump to content

pulling a set of information from a database


itgranny
Go to solution Solved by mac_gyver,

Recommended Posts

I'm not sure I know how to explain what I want to allow you to understand. I'm working on a ventilation app that has different fan sizes. In the input php file I have a dropdown list that's populated by what's in the database. I'd like a results page that lists the fan size and number of each size and the cfm's each fan kicks out. the problem is that I don't know how to do anything with it other than hard code it.

What I'd like, is for it to also pull all of the fan sizes and list them, that way the database can be fluid.

 

This is the code I'd like to replace with some sort of loop.

 

 //9" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=9", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity9 = $row['capacity'];

              };
              //---------------------------------------------------------
              //10" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=10", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity10 = $row['capacity'];

              };


                        //---------------------------------------------------------
              //14" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=14", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity14 = $row['capacity'];

              };


                        //---------------------------------------------------------
              //16" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=16", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity16 = $row['capacity'];

              };

              //---------------------------------------------------------
              //18" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=18", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity18 = $row['capacity'];

              }
              //---------------------------------------------------------
              //20" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=20", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity20 = $row['capacity'];

              };
              //---------------------------------------------------------
              //24" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=24", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity24 = $row['capacity'];

              };
              //---------------------------------------------------------
              //36" fan
              $results = mysql_query("SELECT * FROM fan WHERE size=36", $link);
              while ($row = mysql_fetch_array($results)) {
                $capacity36 = $row['capacity'];

              };

Link to comment
Share on other sites

the goal of your code should be to run as few queries as possible, in this case one will work, to get the data you want in the order that you want it. then, you would simply loop over the rows the query returned and output the data the way you want.

 

to accomplish the ordering, remove the WHERE size=x clause and add an ORDER BY size clause to the query (assuming your table is designed correctly and is storing the size as an integer data type, if you used a character or text type, changed it to an integer data type.)

Link to comment
Share on other sites

Im not sure I'm understanding this. I'd like to list all of the fan sizes and the cubic feet per minute of each and multiply that by the cfm's (cubic feet per minute of air blown). The end result would be a table with each fan size listed, how many cfm's a single fan of that size would move and finally the number of each size of fan times the single fan's cfm's for a total.

 

so, basically, it would go to the database for 2 things, the fan size and the cfm's. Right now I have information on about 6-8 fan sizes, but I'd like to add to that at some point.

 

  1. this may sound strange but I want to tell the database
  2. open connection
  3. Go to the fan info table
  4. build a table
  • first row: list all the fan sizes
  • second row: match with all the cfm's for each size
  • third row: list the number of each fan that was selected in the previous page (which was a dropdown built with the database fan sizes)
  • fourth row: multiply the second row by the third for the total cfm's of any particular fan.
  • This last line would also be totaled in the end.

So each fan size would have two variables, its name and its output.

 

Clear as mud huh?

Link to comment
Share on other sites

just listing what you want doesn't help us to help you as that doesn't tell us where you got stuck at when you tried to do this.

 

i can only offer two recommendations -

 

1) for each step, define what your input(s) are or what data you need to get/produce, what processing you want to do on those inputs/data, and what result or output that step needs to produce.

 

2) "So each fan size would have two variables, its name and its output." your variables should be general purpose. you should not have variables with names like $capacity9, $capacity14, ... in fact, since you have a SET of data that's all going to be processed the same, just with different values, you should use an array to hold the data. storing the rows you retrieve from the fan/product table into an array would be a good first step, as this will decouple the actual database statements from the code using that data.

Link to comment
Share on other sites

Ok, so I'm back to the fans. thanks everyone for looking at this. I'm thinking my biggest problem is not understanding loops and arrays and how to make them work with mysql.

 

 

So here's what I got.  http://www.swinevetcenter.com/tools/ventinput1.php

 

 

Its not done and I'm seeing a few tweeks I need to make but I actually want the layout of both pages to look similar to this. A table with a series of dropdowns where users choose the number of each particular fan size and it adds things up.

 

 

Here's the code I have. As you can see, it works, but it seems pretty long and redundant and I was thinking it could be shortened quite a lot with loops.

 

Input page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html>
<head>
  <title>Ventilation Calculator Input</title>
  <meta id="meta" name="viewport" content="width=device-width; initial-scale=1.0">
  <style type="text/css">
div {
  font-family: "tahoma";
  font-weight: "bold";
  font-size: "24pt"


  }
  table {
    text-align: center;
  }

table {
  border: 1px solid black;
  border-collapse: collapse;
  }
  td {
    border: 1px solid black;
    }
.right {
 text-align: right;
 }
 .left {
   text-align: left;
 }

  </style>


  <script language="JavaScript" type="text/javascript">


  function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
  }

  </script>

</head>

<body>
  <div>
    <form id="form" name="form" action="http://www.swinevetcenter.com/tools/ventresults.php" method="post">
      <b><h1>Ventilation Calculator</h1></b>

      <!--start of number of pigs form..........................................................-->
       <b><span >Pig Info:</span></b><br />

      Number of pigs in the barn:<br>
      <input id="pigNumber" name="pigNumber" type="text" size="10" onkeypress='validate(event)'>
      <!--end of number of pigs form..........................................................-->
       <!--start of pig weight form..........................................................-->
      <br>
      <br>

<?php


mysql_connect('db', 'un', 'pw');
mysql_select_db('un');

$sql = "SELECT weight FROM cfm ORDER BY weight";
$result = mysql_query($sql);


echo "<p><b>Pig Weight</b><br>";
echo "<select weight='weight' name='weight' id='weight'>";
while ($row = mysql_fetch_array($result)) {

echo "<option value='" . $row['weight'] . "'>" . $row['weight'] . "</option>";
}
echo "</select>";

?>


 <br>
<br>
      <!--end of pig weight form..........................................................-->


      <!--start of minimun ventilation form..........................................................-->
<b>Fan Info</b>
      <table>

        <tr>
          <td colspan="11" class="left"><b>Select Size and Number of Stage 1 Fans</b></td>
        </tr>
         <tr>
         <td class="right">Fan Size</td>

          <td >9"</td>

          <td >10"</td>

          <td >14"</td>

          <td >16"</td>

          <td >18"</td>

          <td >20"</td>

          <td >24"</td>

          <td >36"</td>

          <td >48"</td>

          <td >50"</td>

          <td >52"</td>

          <td >54"</td>


        </tr>

        <tr>
        <td class="right">Number of Stage 1 Fans</td>
          <td ><select name="9inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="10inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

                    <td ><select name="14inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

                    <td ><select name="16inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="18inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="20inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="24inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="36inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="48inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="50inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="52inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="54inmin" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>
        </tr>
      </table>
      <!--end of minimun ventilation form..........................................................-->

 <br /><br />
      <!--start of minimun ventilation form..........................................................-->

      <table>

        <tr>
          <td  colspan="11" class="left"><b>Select Size and Number of Barn Fans</b></td>
        </tr>
         <tr></tr>
           <td  class="right">Fan Size</td>
          <td >9"</td>

          <td >10"</td>

          <td >18"</td>

          <td >20"</td>

          <td >24"</td>

          <td >36"</td>

          <td >48"</td>

          <td >50"</td>

          <td >52"</td>

          <td >54"</td>


        </tr>

        <tr>
        <td class="right">Number of Barn Fans</td>
          <td ><select name="9in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="10in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="18in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="20in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="24in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="36in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="48in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="50in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="52in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>

          <td ><select name="54in" size="0">
            <option value="0" selected>
              0
            </option>

            <option value="1">
              1
            </option>

            <option value="2">
              2
            </option>

            <option value="3">
              3
            </option>

            <option value="4">
              4
            </option>

            <option value="5">
              5
            </option>

            <option value="6">
              6
            </option>

            <option value="7">
              7
            </option>

            <option value="8">
              8
            </option>

            <option value="9">
              9
            </option>

            <option value="10">
              10
            </option>
          </select></td>
        </tr>
      </table>

 <br />
 <br />
 <br />




<input id="submit" name="submit" type="submit" value="Submit" method="post">
    </form>



  </div>
</body>
</html>

-------------------------------------------------------------------------------------------------------------------------------

 

the results page:

<!DOCTYPE html>

<html>
<head>
  <title>Ventilation Calculator Results</title>
  <style type="text/css">
<!--
  body {
  font-family: Tahoma, Arial, sans-serif;
  font-size: 18px;
  }
  .headline {
  font-weight: bold;
  font-size: 22pt;
  }
  .alignR {
  text-align: right;
  }
  .alignL {
  text-align:left;
  }
  .alignM {
  text-align: center;
  }

  </style>
  <style type="text/css">
table.c2 {border-collapse:collapse;}
  tr.c1 {text-align: center;}
  </style>
</head>

<body>
  <?php

      $pigNumber = $_POST['pigNumber'];
      $weight = $_POST['weight'];


                //open database connection
                $link = mysql_connect("db", "un", "pw");
                mysql_select_db("un", $link);
                //---------------------------------------------------
                //cfm's per pig for pig size
                //data pulled from the cfm table;
                $results = mysql_query("SELECT * FROM cfm WHERE weight=$weight", $link);
                while ($row = mysql_fetch_array($results)) {
                  $cfmperpig = $row['cfm'];
                };

                //data pulled from the fan table.
                //currently I have it going in over and over to pull relevant information
                //what I need is a way for it to go in, get the full table's worth of information
                //variables needed:
                  //$fansize (for the inches)
                  //$fanspeed (for cfm)
                //---------------------------------------------------------
                //9" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=9", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity9 = $row['capacity'];

                };
                //---------------------------------------------------------
                //10" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=10", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity10 = $row['capacity'];

                };


                          //---------------------------------------------------------
                //14" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=14", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity14 = $row['capacity'];

                };


                          //---------------------------------------------------------
                //16" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=16", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity16 = $row['capacity'];

                };

                //---------------------------------------------------------
                //18" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=18", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity18 = $row['capacity'];

                }
                //---------------------------------------------------------
                //20" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=20", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity20 = $row['capacity'];

                };
                //---------------------------------------------------------
                //24" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=24", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity24 = $row['capacity'];

                };
                //---------------------------------------------------------
                //36" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=36", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity36 = $row['capacity'];

                };


                //---------------------------------------------------------
                //48" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=48", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity48 = $row['capacity'];

                };
                //---------------------------------------------------------
                //50" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=50", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity50 = $row['capacity'];
                };

                          //---------------------------------------------------------
                //52" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=52", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity52 = $row['capacity'];

                };

                          //---------------------------------------------------------
                //54" fan
                $results = mysql_query("SELECT * FROM fan WHERE size=54", $link);
                while ($row = mysql_fetch_array($results)) {
                  $capacity54 = $row['capacity'];

                };

        $output9 = $capacity9*$fan9in;
        $output10 = $capacity10*$fan10in;
        $output14 = $capacity14*$fan14in;
        $output16 = $capacity16*$fan16in;
        $output18 = $capacity18*$fan18in;
        $output20 = $capacity20*$fan20in;
        $output24 = $capacity24*$fan24in;
        $output36 = $capacity36*$fan36in;
        $output48 = $capacity48*$fan48in;
        $output50 = $capacity50*$fan50in;
        $output52 = $capacity52*$fan52in;
        $output54 = $capacity54*$fan54in;
        $minout9 = $capacity9*$minvent9;
        $minout10 = $capacity10*$minvent10;
        $minout14 = $capacity14*$minvent14;
        $minout16 = $capacity16*$minvent16;
        $minout18 = $capacity18*$minvent18;
        $minout20 = $capacity20*$minvent20;
        $minout24 = $capacity24*$minvent24;
        $neededcfms = $cfmperpig*$pigNumber;


     ?><b>Ventilation Worksheet Results</b><br>
  <br>
  <br>
  <b class="c1">Pig Info</b><br>
  Number of pigs: <?php echo $pigNumber ?><br>
  Weight of pigs: <?php echo $weight ?><br>
  cfm's needed per pig: <?php echo $cfmperpig ?><br>
  cfm's needed for barn: <?php echo $neededcfms;?><br>
  <br>
  <br>
  <br>
  <b>Fan Information</b><br>
  <!--would like to get the fan list straight from the database and have them as individual variables;
That way fan sizes could be added to the database and it would just update easily by itself.-->
  <br>

  <table border="1" class="c2">
    <tr class="c1">
      <td colspan="14">Fan Information by Size</td>
    </tr>

    <tr>
      <td>Fan Size</td>

      <td>9"</td>

      <td>10"</td>

      <td>14"</td>

      <td>16"</td>

      <td>18"</td>

      <td>20"</td>

      <td>24"</td>

      <td>36"</td>

      <td>48"</td>

      <td>50"</td>

      <td>52"</td>

      <td>54"</td>

      <td colspan="2"></td>
    </tr>

    <tr>
      <td>cfm's</td>

      <td><?php echo $capacity9 ?></td>

      <td><?php echo $capacity10 ?></td>

      <td><?php echo $capacity14 ?></td>

      <td><?php echo $capacity16 ?></td>

      <td><?php echo $capacity18 ?></td>

      <td><?php echo $capacity20 ?></td>

      <td><?php echo $capacity24 ?></td>

      <td><?php echo $capacity36 ?></td>

      <td><?php echo $capacity48 ?></td>

      <td><?php echo $capacity50 ?></td>

      <td><?php echo $capacity52 ?></td>

      <td><?php echo $capacity54 ?></td>



    </tr>

    <tr>
      <td colspan="14">Stage 1 Fans</td>
    </tr>

    <tr>
      <td>Number of Stage 1 Fans</td>

      <td><?php echo $_POST['9inmin'] ?></td>

      <td><?php echo $_POST['10inmin'] ?></td>

      <td><?php echo $_POST['14inmin'] ?></td>

      <td><?php echo $_POST['16inmin'] ?></td>

      <td><?php echo $_POST['18inmin'] ?></td>

      <td><?php echo $_POST['20inmin'] ?></td>

      <td><?php echo $_POST['24inmin'] ?></td>

      <td><?php echo $_POST['36inmin'] ?></td>

      <td><?php echo $_POST['48inmin'] ?></td>

      <td><?php echo $_POST['50inmin'] ?></td>

      <td><?php echo $_POST['52inmin'] ?></td>

      <td><?php echo $_POST['54inmin'] ?></td>

      <td>Total Stage 1 cfm's</td>
    </tr>

    <tr>
      <td>cfm's</td>

      <td><?php echo $capacity9*$_POST['9inmin'] ?></td>

      <td><?php echo $capacity10*$_POST['10inmin'] ?></td>

      <td><?php echo $capacity14*$_POST['14inmin'] ?></td>

      <td><?php echo $capacity16*$_POST['16inmin'] ?></td>

      <td><?php echo $capacity18*$_POST['18inmin'] ?></td>

      <td><?php echo $capacity20*$_POST['20inmin'] ?></td>

      <td><?php echo $capacity24*$_POST['24inmin'] ?></td>

      <td><?php echo $capacity36*$_POST['36inmin'] ?></td>

      <td><?php echo $capacity48*$_POST['48inmin'] ?></td>

      <td><?php echo $capacity50*$_POST['50inmin'] ?></td>

      <td><?php echo $capacity52*$_POST['52inmin'] ?></td>

      <td><?php echo $capacity54*$_POST['54inmin'] ?></td>

      <td>
      <?php
      echo ($capacity9*$_POST['9inmin'])+($capacity10*$_POST['10inmin'])+($capacity14*$_POST['14inmin'])+($capacity16*$_POST['16inmin'])+($capacity18*$_POST['18inmin'])+($capacity20*$_POST['20inmin'])+($capacity24*$_POST['24inmin'])+($capacity36*$_POST['36inmin'])+($capacity48*$_POST['48inmin'])+($capacity50*$_POST['50inmin'])+($capacity52*$_POST['52inmin'])+($capacity54*$_POST['54inmin']);
      ?>
      </td>
    </tr>
  </table>
</body>
</html>

it just seems like it could be made so much simpler with loops.

Edited by mac_gyver
code tags please
Link to comment
Share on other sites

  • Solution

the goal for your code would be to use a data driven design, where you have data defined somewhere (a database table, array) that tells one small set of code what to do and how many times to do it.
 
here's an example of your form code that takes only about 1/10 the number of lines of code (this doesn't retrieve the fan data from the table as that's a trivial task) - 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>Ventilation Calculator Input</title>
<meta id="meta" name="viewport" content="width=device-width; initial-scale=1.0">
<style type="text/css">
div {
font-family: "tahoma";
font-weight: "bold";
font-size: "24pt"
}
table {
text-align: center;
}
table {
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
}
.right {
text-align: right;
}
.left {
text-align: left;
}
</style>
<script language="JavaScript" type="text/javascript">
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
</head>
<body>
<div>
<form id="form" name="form" action="http://www.swinevetcenter.com/tools/ventresults.php" method="post">
<b><h1>Ventilation Calculator</h1></b>
<b><span >Pig Info:</span></b><br />
Number of pigs in the barn:<br>
<input id="pigNumber" name="pigNumber" type="text" size="10" onkeypress='validate(event)'>
<br>
<br>
<?php
mysql_connect('db', 'un', 'pw');
mysql_select_db('un');
 
// stage 1 fans 9" 10" 14" 16" 18" 20" 24" 36" 48" 50" 52" 54"
$stage1_fans = array(9,10,14,16,18,20,24,36,48,50,52,54); // this apparently your fan.csv data
 
// barn fans 9" 10" 18" 20" 24" 36" 48" 50" 52" 54"
$barn_fans = array(9,10,18,20,24,36,48,50,52,54); 
 
$category['stage1'] = array('legend'=>'Stage 1','sizes'=>$stage1_fans);
$category['barn'] = array('legend'=>'Barn','sizes'=>$barn_fans);
 
// produce option list 0-10
$num = range(0,10);
$options = '';
foreach($num as $val){
$options .= "<option value='$val'>$val</option>\n";
}
 
// pig weight
$sql = "SELECT weight FROM cfm ORDER BY weight";
$result = mysql_query($sql);
echo "<p><b>Pig Weight</b><br>";
echo "<select weight='weight' name='weight' id='weight'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['weight'] . "'>" . $row['weight'] . "</option>";
}
echo "</select>";
?>
<br>
<br>
<b>Fan Info</b>
<?php
foreach($category as $key=>$arr){
?>
<table>
<tr>
<td colspan="<?php echo count($arr['sizes'])+1;?>" class="left"><b>Select Size and Number of <?php echo $arr['legend']; ?> Fans</b></td>
</tr>
<tr>
<td class="right">Fan Size</td>
<?php
foreach($arr['sizes'] as $val){
echo "<td >$val\"</td>";
}
?>
</tr>
<tr>
<td class="right">Number of <?php echo $arr['legend']; ?> Fans</td>
<?php
foreach($arr['sizes'] as $val){
echo "<td ><select name='{$key}[$val]'>";
echo $options;
echo "</select></td>";
}
?>
</tr>
</table>
<br /><br />
<?php } ?>
<br />
<input id="submit" name="submit" type="submit" value="Submit" method="post">
</form>
</div>
</body>
</html>

other than reducing the amount of code, the only significant change in the above is to name the form fields as an array that you can loop over to process the submitted data.
 
the code you posted has two different sets of fan sizes. is this intentional or should the list of 'Barn' fan sizes be the same as the 'Stage 1' fan sizes?

Link to comment
Share on other sites

OK, thanks, I absent mindedly deleted the original file and this was put together quickly.

Can that array be put together on the fly so that it gets constructed by whatever's in the database? That way, when more fan information comes in, (example a 12 inch fan or a 60 inch fan) They can be plugged into the db and  the calculator will update automatically.

 

Thanks for your help!

Link to comment
Share on other sites

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.