Hello,
I want to query a MySql database & run some statistical calculations on each query result individually & then display the results. Explained slightly differently, if the query result consists of 20 records, then I want to run the statistical calculation 20 times(once on each record).
A good working example of what I'm trying to create can be found here:- http://www.checker.freeproxy.ru/checker/ In the case of this website, the user enters a few ip addresses(1 ip address per line) & hits the Check Proxy! button. The code then processes each ip address individually & display the individual results. I want to do the same thing.
What I've already done succesfully(thanks to the help of many helpful forummers here) is to query the database based on 1 record per line. My code is below.
What I do not know is how to run the statistical calculation(median calculation in this case) on each record individually. If you observe my code, I am actually grouping all the records(the search results) together & running the median calculation once on the whole group. This is not what I want to achieve. I want to run the median calculation on each record individually.
Please help me to edit my code. Thank you for your help.
<?php
/* The next 15 lines are working fine */
$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);
$query1="SELECT price FROM table WHERE ".$email_str . $limit;
$result1=mysql_query($query1);
}
/* Median calculation for the price. This is where I'm doing things
wrongly because I'm using $result1 to do the calculation. */
$thearray=array();
while ( $row=@mysql_fetch_array($result1,MYSQL_NUM) ) {
$thearray[]=$row[0];
}
$num0=count($thearray);
if ($num0 % 2) {
$median1 = $thearray[floor($num0+1)/2 - 1];
} else {
$median1 = ($thearray[$num0/2] + $thearray[$num0/2 - 1]) / 2;
}
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form name="ftest" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="femail"></textarea><br />
<input type="submit" name="Submit" value="Send" />
</form>
<table width=700 border=1 height="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td style="border-style: solid; border-width: 1" bgcolor="#99CCCC" align="left" width="350"><b>
<div align="center"><font size="3" face="Arial, Helvetica, sans-serif"><? echo "Median Price - RM<font color='#FF33cc'> $median1 </font>"; ?></font></div></td>
</tr>
</table>
</body>
</html>