I have got 4 tables in the same database. The name of the tables are table03, table04, table05 & table06. The structure of the tables are exactly the same. I want to calculate the median price of the price column in each table individually based on the keywords I keyed in. The keywords will query the full_add column.
So assuming that my keywords were Toyota, Honda, Mercedes and BMW(keyed in as 1 keyword per line in the form), the end result should display something like this:-
Median Price from table03
Toyota - $3883
Honda - $356
Mercedes - $7865
BMW - $1347
Median Price from table04
Toyota - $7688
Honda - $4312
Mercedes - $8709
BMW - $1254
Median Price from table05
Toyota - $8776
Honda - $3445
Mercedes - $8778
BMW - $1223
Median Price from table06
Toyota - $6578
Honda - $5445
Mercedes - $545
BMW - $12548
The part that I do not know how to code & require your help is the median calculation is not looping through the search results. Therefore, instead of getting a result like
Median Price from table03
Toyota - $3883
Honda - $356
Mercedes - $7865
BMW - $1347
I am getting a result like
Median Price from table03
Toyota - $3883
Honda - $3883
Mercedes - $3883
BMW - $3883
Please show me how to write the code. Thank you for your help. My code is below.
Code: Select all
<?php
$limit = "LIMIT 0,100000";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
if(isset($_POST['Submit'])){
$emails=explode("\n", str_replace("\r", "", $_POST['femail']));
$email_r=array();
foreach($emails as $e){
$email_r[]="full_add LIKE '%".mysql_escape_string($e)."%'";
}
$email_str=implode(' OR ',$email_r);
for($j = 3; $j <= 6; $j++)
{
echo '<b>Median Price from table0'.$j.'</b><br>';
for($i = 0; $i < sizeof($emails); $i++)
{
$query = "SELECT price FROM table0$j WHERE $email_str ORDER BY price DESC $limit";
$result = mysql_query($query);
/* The median calculation starts below. While it is working fine,
it is not looping through the search results. This is where i need help */
$thearray=array();
while ( $row=@mysql_fetch_array($result,MYSQL_NUM) ) {
$thearray[]=$row[0];
}
$num0=count($thearray);
if ($num0 % 2) {
$median = $thearray[floor($num0+1)/2 - 1];
} else {
$median = ($thearray[$num0/2] + $thearray[$num0/2 - 1]) / 2;
}
/* The median calculation ended above */
echo $emails[$i].' - $'.$median.'<br>';
}
}
}
?>
<form name="ftest" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="femail"></textarea><br />
<input type="submit" name="Submit" value="Send" />
</form>