Integrated Query Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cover

    Integrated Query Question

    I have a table with 50 fields that receive input depending on whether
    that input came in from a 'shaker' form or a 'conveyor' form. Input
    from the 'conveyor' form might populate 25 fields while input from the
    'shaker' form will populate another 20-25 fields but not the same
    fields (however there are about 10 common fields to both). I'd
    thought about using two tables (one for 'conveyor' and the other for
    'shaker') but thought I'd try just the single table for now.

    My query question: My pull down allows for selection of various areas
    and also what type of equipment; i.e. 'shaker' or 'conveyor'. I have
    the code started within a single query to cover the 'conveyor' 25
    specific fields of data if the user selected 'conveyor' on their query
    but what about if they'd selected 'shaker'? How do I write that
    particular portion of code to return those different 20-25 fields?
    Almost one for 'else if' but I don't know that this would work either.
    Seems as though I'm looking for a conditional or compound query.

    thanks,
    Chris

    The following code passes variable data from two pull downs on another
    form (area and equiptype) to what I hope will be a single integrated
    form below. Again, conveyor code example follows with needed shaker
    code I'd like to integrate into a single query below that if that's
    possible: Would something like what follows work?
    ---------------------------------------------------------------------

    $DBhost = "localhost" ;
    $DBuser = "dobey";
    $DBpass = "#321@#2123 4";
    $DBname = "equipment" ;
    $table = "equipment_tbl" ;

    $area = $_POST['area'];
    $equiptype = $_POST['equiptype'];

    mysql_connect($ DBhost, $DBuser, $DBpass) or die("Unable to connect to
    host $DBhost");

    mysql_select_db ($DBname) or die("Unable to select database $DBname");

    $query =
    "SELECT area, equiptype, equipname, motorsize, motorframe,
    beltspec, beltspeed
    FROM $table
    WHERE 1 = 1 ";
    if($area != "All") $query .= "and area = '".$area."'" ;
    if($equiptype != "CONVEYOR") $query .= "and equiptype =
    "'.$equiptype." '";
    $result = mysql_query($qu ery);

    $number = mysql_numrows($ result);

    print "<h2>There are $number CONVEYOR records in the Equipment
    Database:</h2>
    <table cellpadding=5>
    <tr bgcolor=black>
    <td><font color=white><b> area</b></td></font></td>
    <td><font color=white><b> equip type</b></td></font></td>
    <td><font color=white><b> equip name</b></td></font></td>
    <td><font color=white><b> motor size</b></td></font></td>
    <td><font color=white><b> motor frame</b></td></font></td>
    <td><font color=white><b> belt spec</b></font></td>
    <td><font color=white><b> belt speed</b></td></font></td>";
    for($i=0; $i<$number; $i++) {
    $area = mysql_result($r esult,$i,"area" );
    $equiptype = mysql_result($r esult,$i,"equip type");
    $equipname = mysql_result($r esult,$i,"equip name");
    $motorsize = mysql_result($r esult,$i,"motor size");
    $motorframe = mysql_result($r esult,$i,"motor frame");
    $beltspec = mysql_result($r esult,$i,"belts pec");
    $beltspeed = mysql_result($r esult,$i,"belts peed");
    /* print even-numbered rows with a grey background,
    odd-numbered with a white background */
    if ($i%2 == 0) {
    print "<tr bgcolor=lightgr ey>";
    } else {
    print "<tr>";
    }
    print "<td>$area</td>
    <td>$equiptyp e</td>
    <td>$equipnam e</td>
    <td>$motorsiz e</td>
    <td>$motorframe </td>
    <td>$beltspec </td>
    <td>$beltspee d</td>";
    }
    print "</table>";

    // OTHERWISE IF SHAKER HAS BEEN SELECTED:

    $query =
    "SELECT area, equiptype, equipname, motorsize, motorframe,
    shakerstroke, shaker_rpm
    FROM $table
    WHERE 1 = 1 ";
    if($area != "All") $query .= "and area = '".$area."'" ;
    if($equiptype != "SHAKER") $query .= "and equiptype =
    "'.$equiptype." '";
    $result = mysql_query($qu ery);

    $number = mysql_numrows($ result);

    print "<h2>There are $number SHAKER records in the Equipment
    Database:</h2>
    <table cellpadding=5>
    <tr bgcolor=black>
    <td><font color=white><b> area</b></td></font></td>
    <td><font color=white><b> equip type</b></td></font></td>
    <td><font color=white><b> equip name</b></td></font></td>
    <td><font color=white><b> motor size</b></td></font></td>
    <td><font color=white><b> motor frame</b></td></font></td>
    <td><font color=white><b> shaker stroke</b></font></td>
    <td><font color=white><b> shaker RPM</b></td></font></td>";
    for($i=0; $i<$number; $i++) {
    $area = mysql_result($r esult,$i,"area" );
    $equiptype = mysql_result($r esult,$i,"equip type");
    $equipname = mysql_result($r esult,$i,"equip name");
    $motorsize = mysql_result($r esult,$i,"motor size");
    $motorframe = mysql_result($r esult,$i,"motor frame");
    $shakerstroke = mysql_result($r esult,$i,"shake rstroke");
    $shaker_rpm = mysql_result($r esult,$i,"shake r_rpm");
    /* print even-numbered rows with a grey background,
    odd-numbered with a white background */
    if ($i%2 == 0) {
    print "<tr bgcolor=lightgr ey>";
    } else {
    print "<tr>";
    }
    print "<td>$area</td>
    <td>$equiptyp e</td>
    <td>$equipnam e</td>
    <td>$motorsiz e</td>
    <td>$motorframe </td>
    <td>$shakerstro ke</td>
    <td>$shaker_rpm </td>";
    }
    print "</table>";

    // Close the database connection
    mysql_close();
    ?>
  • Lüpher Cypher

    #2
    Re: Integrated Query Question

    If I understand correctly, you may have either 'conveyor' or 'shaker'
    passed to the script, and depending on that value different columns
    should be shown. If that's the case, I'd rather do it like this:

    # Common to all queries fields

    $commonFields = array("area" => "Area",
    "equiptype" => "Equip Type",
    "equipname" => "Equip Name",
    "motorsize" => "Motor Size",
    "motorframe " => "Motor Frame");

    # Fields depending on a variable

    $varFields = array("CONVEYOR " => array("beltspec " => "Belt Spec",
    "beltspeed" => "Belt Speed"),
    "SHAKER" => array("shakerst roke" => "Shaker Stroke",
    "shaker_rpm " => "Shaker RPM"));

    # Get vars

    $area = $_POST["area"];
    $equiptype = $_POST["equiptype"];

    # Build query fields
    # This will return an associative array of all fields needed

    $queryFields = array_merge($co mmonFields,$var Fields[$equiptype]);

    # This will set $fields to "field,field,fi eld,..."

    $fields = implode(",",arr ay_keys($queryF ields));

    # Build full query

    $query = "SELECT $fields FROM $table ".
    "WHERE 1=1 AND equiptype='$equ iptype'".
    ($area != "All" ? " AND area='$area'" : "");
    $result = mysql_query($qu ery);

    # Output table
    echo "<table ...>";
    # Header
    echo "<tr>";
    foreach ($queryFields as $field=>$title) {
    echo "<th>$title </th>";
    }
    echo "</tr>";
    # Rows
    while ($row = mysql_fetch_ass oc($result)) {
    foreach ($queryFields as $field=>$title) {
    echo "<td>{$row[$field]}</td>";
    }
    }
    echo "</table ...>";


    In case you get more equipment types, you can simply update $varFields
    array and leave everything else unchanged - it'll still work :)
    The idea is that since you have common and uncommon fields, it's easier
    to describe them separately. You can then "choose" which uncommon fields
    you want retrieved and merge them with common fields. Having that, you
    can use loops to output a table. :)



    cover wrote:[color=blue]
    > I have a table with 50 fields that receive input depending on whether
    > that input came in from a 'shaker' form or a 'conveyor' form. Input
    > from the 'conveyor' form might populate 25 fields while input from the
    > 'shaker' form will populate another 20-25 fields but not the same
    > fields (however there are about 10 common fields to both). I'd
    > thought about using two tables (one for 'conveyor' and the other for
    > 'shaker') but thought I'd try just the single table for now.
    >
    > My query question: My pull down allows for selection of various areas
    > and also what type of equipment; i.e. 'shaker' or 'conveyor'. I have
    > the code started within a single query to cover the 'conveyor' 25
    > specific fields of data if the user selected 'conveyor' on their query
    > but what about if they'd selected 'shaker'? How do I write that
    > particular portion of code to return those different 20-25 fields?
    > Almost one for 'else if' but I don't know that this would work either.
    > Seems as though I'm looking for a conditional or compound query.
    >
    > thanks,
    > Chris
    >
    > The following code passes variable data from two pull downs on another
    > form (area and equiptype) to what I hope will be a single integrated
    > form below. Again, conveyor code example follows with needed shaker
    > code I'd like to integrate into a single query below that if that's
    > possible: Would something like what follows work?
    > ---------------------------------------------------------------------
    >
    > $DBhost = "localhost" ;
    > $DBuser = "dobey";
    > $DBpass = "#321@#2123 4";
    > $DBname = "equipment" ;
    > $table = "equipment_tbl" ;
    >
    > $area = $_POST['area'];
    > $equiptype = $_POST['equiptype'];
    >
    > mysql_connect($ DBhost, $DBuser, $DBpass) or die("Unable to connect to
    > host $DBhost");
    >
    > mysql_select_db ($DBname) or die("Unable to select database $DBname");
    >
    > $query =
    > "SELECT area, equiptype, equipname, motorsize, motorframe,
    > beltspec, beltspeed
    > FROM $table
    > WHERE 1 = 1 ";
    > if($area != "All") $query .= "and area = '".$area."'" ;
    > if($equiptype != "CONVEYOR") $query .= "and equiptype =
    > "'.$equiptype." '";
    > $result = mysql_query($qu ery);
    >
    > $number = mysql_numrows($ result);
    >
    > print "<h2>There are $number CONVEYOR records in the Equipment
    > Database:</h2>
    > <table cellpadding=5>
    > <tr bgcolor=black>
    > <td><font color=white><b> area</b></td></font></td>
    > <td><font color=white><b> equip type</b></td></font></td>
    > <td><font color=white><b> equip name</b></td></font></td>
    > <td><font color=white><b> motor size</b></td></font></td>
    > <td><font color=white><b> motor frame</b></td></font></td>
    > <td><font color=white><b> belt spec</b></font></td>
    > <td><font color=white><b> belt speed</b></td></font></td>";
    > for($i=0; $i<$number; $i++) {
    > $area = mysql_result($r esult,$i,"area" );
    > $equiptype = mysql_result($r esult,$i,"equip type");
    > $equipname = mysql_result($r esult,$i,"equip name");
    > $motorsize = mysql_result($r esult,$i,"motor size");
    > $motorframe = mysql_result($r esult,$i,"motor frame");
    > $beltspec = mysql_result($r esult,$i,"belts pec");
    > $beltspeed = mysql_result($r esult,$i,"belts peed");
    > /* print even-numbered rows with a grey background,
    > odd-numbered with a white background */
    > if ($i%2 == 0) {
    > print "<tr bgcolor=lightgr ey>";
    > } else {
    > print "<tr>";
    > }
    > print "<td>$area</td>
    > <td>$equiptyp e</td>
    > <td>$equipnam e</td>
    > <td>$motorsiz e</td>
    > <td>$motorframe </td>
    > <td>$beltspec </td>
    > <td>$beltspee d</td>";
    > }
    > print "</table>";
    >
    > // OTHERWISE IF SHAKER HAS BEEN SELECTED:
    >
    > $query =
    > "SELECT area, equiptype, equipname, motorsize, motorframe,
    > shakerstroke, shaker_rpm
    > FROM $table
    > WHERE 1 = 1 ";
    > if($area != "All") $query .= "and area = '".$area."'" ;
    > if($equiptype != "SHAKER") $query .= "and equiptype =
    > "'.$equiptype." '";
    > $result = mysql_query($qu ery);
    >
    > $number = mysql_numrows($ result);
    >
    > print "<h2>There are $number SHAKER records in the Equipment
    > Database:</h2>
    > <table cellpadding=5>
    > <tr bgcolor=black>
    > <td><font color=white><b> area</b></td></font></td>
    > <td><font color=white><b> equip type</b></td></font></td>
    > <td><font color=white><b> equip name</b></td></font></td>
    > <td><font color=white><b> motor size</b></td></font></td>
    > <td><font color=white><b> motor frame</b></td></font></td>
    > <td><font color=white><b> shaker stroke</b></font></td>
    > <td><font color=white><b> shaker RPM</b></td></font></td>";
    > for($i=0; $i<$number; $i++) {
    > $area = mysql_result($r esult,$i,"area" );
    > $equiptype = mysql_result($r esult,$i,"equip type");
    > $equipname = mysql_result($r esult,$i,"equip name");
    > $motorsize = mysql_result($r esult,$i,"motor size");
    > $motorframe = mysql_result($r esult,$i,"motor frame");
    > $shakerstroke = mysql_result($r esult,$i,"shake rstroke");
    > $shaker_rpm = mysql_result($r esult,$i,"shake r_rpm");
    > /* print even-numbered rows with a grey background,
    > odd-numbered with a white background */
    > if ($i%2 == 0) {
    > print "<tr bgcolor=lightgr ey>";
    > } else {
    > print "<tr>";
    > }
    > print "<td>$area</td>
    > <td>$equiptyp e</td>
    > <td>$equipnam e</td>
    > <td>$motorsiz e</td>
    > <td>$motorframe </td>
    > <td>$shakerstro ke</td>
    > <td>$shaker_rpm </td>";
    > }
    > print "</table>";
    >
    > // Close the database connection
    > mysql_close();
    > ?>[/color]

    Comment

    • cover

      #3
      Re: Integrated Query Question

      On Sat, 31 Dec 2005 04:07:54 GMT, L?pher Cypher
      <lupher.cypher@ verizon.net> wrote:
      [color=blue]
      >If I understand correctly, you may have either 'conveyor' or 'shaker'
      >passed to the script, and depending on that value different columns
      >should be shown. If that's the case, I'd rather do it like this:
      >[/color]

      thanks very much - works great. :-)

      Comment

      • Jim Michaels

        #4
        Re: Integrated Query Question

        if it's of any interest, you can get rid of the WHERE 1=1
        it does nothing and is not needed.

        "cover" <coverlandNOSPA M914@yahoo.com> wrote in message
        news:0fubr1t6bk i6tsmfr3gfrg2cc 0kn42a9kg@4ax.c om...[color=blue]
        > $query =
        > "SELECT area, equiptype, equipname, motorsize, motorframe,
        > beltspec, beltspeed
        > FROM $table
        > WHERE 1 = 1 ";
        > $query =
        > "SELECT area, equiptype, equipname, motorsize, motorframe,
        > shakerstroke, shaker_rpm
        > FROM $table
        > WHERE 1 = 1 ";[/color]


        Comment

        Working...