0

I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.

$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
	echo "$firstOne";
	echo "<br/>";
	echo "$secondOne";

	$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
	$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
	$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());

	if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
	
	//if (mysql_num_rows($result) > 0) {
		
		while($row = mysql_fetch_row($result)) {			
			if ($firstOne == $row[1]) {
			{
				echo "<table border=1>";			
				echo "<tr>";
				echo "<td>" . $row[0] . "</td>";
				echo "<tr>";
				echo "<td>" . $row[1] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row[2] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row[3] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row[4] . "</td>";
				echo "</tr>";
				}
			}
			}echo "</table>";

		while($row2 = mysql_fetch_row($result2)) {			
			if ($secondOne == $row2[1]) {
			{	echo "<table border=1>";		
				echo "<tr>";
				echo "<td>" . $row2[0] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row2[1] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row2[2] . "</td>";
				echo "</tr>";
				echo "<tr>";
				echo "<td>" . $row2[3] . "</td>";
				echo "</tr>";
				echo "<td>" . $row2[4] . "</td>";
				echo "</tr>";
				}
			}
			}
			echo "</table>";
		}
	 else {
			echo "No Results";
		}

Thanks

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by diafol
0

I assume that your table has the following structure:

test_id | test_name | brand | cost | etc...

Can't you just run ONE query?

THe following is for horizontal items:

$listitems = array($firstOne,$secondOne); //get this from your form etc by combining $_POST or $_GET vars. they also need to be escaped with mysql_real_escape_string()

$list = implode("','",$listitems);
$r = mysql_query("SELECT * FROM test_table WHERE test_name IN('$list')");
if(mysql_num_rows($query) == count($listitems)){
  $out = "";
  while($d = mysql_fetch_array($r)){
     $out .= <tr><th>{$d['test_name']}</th><td>{$d['brand']}</td><td>{$d['cost']}</td></tr>;
  }
}else{
  echo "There was a problem retrieving some data";
}

<table>
  <thead>
    <tr>
      <th>TName</th><th>Brand</th><th>Cost(£)</th>
    </tr>
  <thead>
  <tbody>
    <?php echo $out;?>
  </tbody>
</table>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.