Reply to Re: Integrated Query Question

Your name:

Reply:


Posted by Lόpher Cypher on 12/31/05 06:07

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("shakerstroke" => "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($commonFields,$varFields[$equiptype]);

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

$fields = implode(",",array_keys($queryFields));

# Build full query

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

# Output table
echo "<table ...>";
# Header
echo "<tr>";
foreach ($queryFields as $field=>$title) {
echo "<th>$title</th>";
}
echo "</tr>";
# Rows
while ($row = mysql_fetch_assoc($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:
> 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@#21234";
> $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($query);
>
> $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($result,$i,"area");
> $equiptype = mysql_result($result,$i,"equiptype");
> $equipname = mysql_result($result,$i,"equipname");
> $motorsize = mysql_result($result,$i,"motorsize");
> $motorframe = mysql_result($result,$i,"motorframe");
> $beltspec = mysql_result($result,$i,"beltspec");
> $beltspeed = mysql_result($result,$i,"beltspeed");
> /* print even-numbered rows with a grey background,
> odd-numbered with a white background */
> if ($i%2 == 0) {
> print "<tr bgcolor=lightgrey>";
> } else {
> print "<tr>";
> }
> print "<td>$area</td>
> <td>$equiptype</td>
> <td>$equipname</td>
> <td>$motorsize</td>
> <td>$motorframe</td>
> <td>$beltspec</td>
> <td>$beltspeed</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($query);
>
> $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($result,$i,"area");
> $equiptype = mysql_result($result,$i,"equiptype");
> $equipname = mysql_result($result,$i,"equipname");
> $motorsize = mysql_result($result,$i,"motorsize");
> $motorframe = mysql_result($result,$i,"motorframe");
> $shakerstroke = mysql_result($result,$i,"shakerstroke");
> $shaker_rpm = mysql_result($result,$i,"shaker_rpm");
> /* print even-numbered rows with a grey background,
> odd-numbered with a white background */
> if ($i%2 == 0) {
> print "<tr bgcolor=lightgrey>";
> } else {
> print "<tr>";
> }
> print "<td>$area</td>
> <td>$equiptype</td>
> <td>$equipname</td>
> <td>$motorsize</td>
> <td>$motorframe</td>
> <td>$shakerstroke</td>
> <td>$shaker_rpm</td>";
> }
> print "</table>";
>
> // Close the database connection
> mysql_close();
> ?>

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация