Forum Moderators: coopster

Message Too Old, No Replies

Improving a PHP compare page

Need help formatting the output.

         

dpinion

3:54 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



Ok, first, I have the following code that takes checkboxes from a form and based on items checked selects the item details from a database. I first create a header row and column, and then iterate through each part number that was passed from the form (checked) I then throw away the "Compare" value from the submit button as I don't need it:

 <?php
echo "<table class=\"comptbl\"><tr>";
echo "<td><table class=\"comptbl\">";
echo "<tr><td class=\"label\">Model Number</td></tr>";
echo "<tr><td class=\"label\">Model Name</td></tr>";
echo "<tr><td class=\"label\">Description</td></tr>";
echo "<tr><td class=\"label\"># of Memory Slots</td></tr>";
echo "<tr><td class=\"label\">Maximum Memory</td></tr>";
echo "<tr><td class=\"label\">PCIe Support</td></tr>";
echo "<tr><td class=\"label\">PCI Support</td></tr>";
echo "<tr><td class=\"label\">ISA Support</td></tr>";
echo "<tr><td class=\"label\">SATA Support</td></tr>";
echo "<tr><td class=\"label\">USB Support</td></tr>";
echo "<tr><td class=\"label\">OnBoard Video</td></tr>";
echo "<tr><td class=\"label\">Onboard Ethernet</td></tr>";
echo "<tr><td class=\"label\">Front Side Bus Speed</td></tr>";
echo "</table></td>";
foreach($_POST as $name => $value) {
if ($value!="Compare")
{
mysql_select_db("tti_site", $ttiConn) or die('Sorry, could not connect to database');
$query = "SELECT * from tblsbccompare where modelNum = $value";
$result = mysql_query($query) or die('Could not get values');
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
$mNum = $row['modelNum'];
$mName = $row['modelName'];
$mDesc = $row['modelDesc'];
$mQty = $row['memQty'];
$mMax = $row['memMax'];
$mPCIe = $row['PCIe'];
$mPCI = $row['PCI'];
$mISA = $row['ISA'];
$mSATA = $row['SATA'];
$mUSB = $row['USB'];
$mVideo = $row['Video'];
$mEthernet = $row['Ethernet'];
$mFSB = $row['FSB'];
}
echo "<td><table class=\"comptbl\">";
echo "<tr><td class=\"comphdr\">$mNum</td></tr>";
echo "<tr><td>$mName</td></tr>";
echo "<tr><td>$mDesc</td></tr>";
echo "<tr><td>$mQty</td></tr>";
echo "<tr><td>$mMax</td></tr>";
echo "<tr><td>$mPCIe</td></tr>";
echo "<tr><td>$mPCI</td></tr>";
echo "<tr><td>$mISA</td></tr>";
echo "<tr><td>$mSATA</td></tr>";
echo "<tr><td>$mUSB</td></tr>";
echo "<tr><td>$mVideo</td></tr>";
echo "<tr><td>$mEthernet</td></tr>";
echo "<tr><td>$mFSB</td></tr>";
echo "</table></td>";
}
}
echo "</tr>";
echo "</table>";
?>

The above code gives me the output I want as far as the data itself is concerned. However, as you can see, there are a couple of issues.

1) I have to create tables for each column of data (I am trying to make the item's data columnar). This means that if any row wraps, then suddenly my rows are uneven and data can't be analyzed by row. This would not be an issue if I could simply have one table and place each item and its data in its own column

2) Probably the bigger issue. I am running a SELECT statement for EACH product that was selected on my original form. I am thinking there should be some way to do ONE query (using the IN keyword, I imagine), and feed all that to an array or similar, then add it to the table. Unfortunately I am still a bit green with PHP, combining it with MySQL especially.

Any tips/advice on this would be appreciated, as I want to learn the right way to do it. :0)

npwsol

5:48 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



1) That nested table haunts me. In my dreams. Use a div with BRs or even a list with style-type: none. That's not what you were asking, though. white-space: nowrap might help you out with that concern; instead of flowing onto the next line, the data will run out of the cell, though you hit another problem with tables first, I think, in that they expand to fit the content... if it were a div or a ul, though, the data would overlap the next column instead of wrapping. Still not ideal.

2) The only method I could conceive of you doing this is to expand your OR on the select statement with a loop. Create the first part of the statement, then use your foreach loop.


$szQuery = "SELECT * FROM tblsbccompare WHERE ";
$szOrs = "";
foreach($_POST as $name => $value)
{
if($value != "Compare")
{
if($szOrs != "")
{
$szOrs = $szOrs . " OR ";
}
$szOrs = $szOrs . "modelNum = $value";
}
}
if($szOrs != "")
{
$szQuery = $szQuery . $szOrs;
}

Using that method you could get all of your rows simultaneously and throw them into an array together, then output your data in actual columnar fashion, but I suspect it's highly inefficient.

Another possible method would be to take the high and low values from the $value parameter and get every product in between those values, then compare them to $value before inserting them into your array.

highest_value and lowest_value do not, to my knowledge, exist, but I am lazy:


$big = highest_value($_POST);
$small = lowest_value($_POST);
$nX = 0;
$szQuery = "SELECT * FROM tblsbccompare WHERE modelNum <= $big AND modelNum >= $small";
$oResults = $oSQL->query($szQuery);
while($oRow = $oResults->fetch_array())
{
if(in_array($oRow["modelNum"], $_POST))
{
$aProdList[$nX] = $oRow;
$nX++;
}
}

That would create an array of all the matching rows for you. Potential issues: Another value in $_POST is equal to $oRow["modelNum"]; it's probably not efficient (a loop inside a loop is never a good sign; I believe in_array loops). It would, however, get you the data in a fashion you could use to do absolute columnar data.