kiran patel kiran patel - 1 month ago 5
MySQL Question

How should I compare two different product price in php script?

$comapre
is an array with unique product ids passed on from previous page.so in the following page I want to look in the database using that
productid
, get information and compare the prices of the product corresponding to each
productid
from an
array($compare)
.

So far I can display the relevant product information using
productid
from
$compare(array
) in the following format

productid | product name| price |

20______|__iphone5__|_529 |

i am using the code :

foreach($compare as $value) {

$query = mysql_query("SELECT * FROM `product_info` WHERE `productid`=".$value."");
echo "<table border='1' bordercolor='#000066'>
<tr>
<th>productid</th>
<th>product name</th><br />
<th>price</th>
</tr>";

while ($query_row=mysql_fetch_assoc($query)) {
echo "<tr bgcolor='#F5FFFF'>";
echo "<td>". $query_row['productid']. "</td>";
echo "<td>". $query_row['title']. "</td>";
echo "<td>". $query_row['price']."</td>";
echo "</tr>";
}
echo "</table>";
}


Please give me any suggestion how to compare the price of each product and say like "productA is £_ cheaper than productB" something similar to that would also work.please help.

Answer

Move the prices to a new array say product_prices[].

$product_prices['$productID'] = $query_row['price'];

You can then sort this array by increasing/decreasing order of prices and then subtract prices between individual keys.

How you work with the product_prices array is totally left to you and the use case that you have.

Comments