Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-04-2007, 08:15 AM   #1 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Do Some Calculations on Query Results

Hello,
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

What I've already done succesfully is to query the database based on 1 record per line. My code is below.

The part that I do not know how to code is as follows:-
1. A join query to query all tables at one go.
2. To calculate the median 4 times(once for each table) & display the results like the sample above.

Please show me how to write the code. Thank you for your help.
PHP Code:
<?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); 

$query1="SELECT price FROM table WHERE ".$email_str $limit
$result1=mysql_query($query1); 


/* Median calculation for the price. */ 
$thearray=array(); 
while ( 
$row=@mysql_fetch_array($result1,MYSQL_NUM) ) { 
$thearray[]=$row[0]; 

$num0=count($thearray); 
if (
$num0 2) { 
$median1 $thearray[floor($num0+1)/1]; 
} else { 
$median1 = ($thearray[$num0/2] + $thearray[$num0/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>
Additional Info:-
My table structure for all tables are as follows:-
Code:
`id` int(6) NOT NULL auto_increment,
`date` year(4) NOT NULL default '0000',
`full_add` varchar(255) NOT NULL default '',
`status` varchar(10) NOT NULL default '',
`size` int(6) default '0',
`price` int(8) default '0',
`price_psf` double NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `full_add` (`full_add`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
transfield is offline   Reply With Quote
Old 03-04-2007, 03:04 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
What's the point of having 4 tables with the exact same structure? If they represent different things, wouldn't it be better to just add 1 additional field that desribed what differentiates each set of rows?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-04-2007, 06:09 PM   #3 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Quote:
What's the point of having 4 tables with the exact same structure?
Good question & thank you for asking. When I initially created everthing, the database sizes where so huge that I broke them down into 4 different tables representing 4 different years.

At this stage, I've got everything working except 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

My code is below & I trust that you can edit it for me. Thanks.
PHP Code:
<?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 here. It's not looping through the 
search results & I don't know how to solve this. Please help me. */
$thearray=array(); 
while ( 
$row=@mysql_fetch_array($result,MYSQL_NUM) ) { 
$thearray[]=$row[0]; 
}
$num0=count($thearray); 
if (
$num0 2) { 
$median $thearray[floor($num0+1)/1]; 
} else { 
$median = ($thearray[$num0/2] + $thearray[$num0/1]) / 2

/* The median calculation ends here. */    
   
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>
transfield is offline   Reply With Quote
Old 03-04-2007, 08:06 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Are you sure that's necessary, I have a 2 million plus row table I work with daily that does fine. It might be easier just to add a year column and construct one big table.

Regardless, It looks like you need an additional for loop to iterate over the keyword types OR make a number of arrays for each type and select the price and type (although from your table and code I can't tell which field is supposed to contain the keyword).
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-04-2007, 08:11 PM   #5 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Quote:
although from your table and code I can't tell which field is supposed to contain the keyword
It's the full_add field.

Quote:
It looks like you need an additional for loop to iterate over the keyword types OR make a number of arrays for each type and select the price and type
Could you show me how to write the code?

Quote:
It might be easier just to add a year column and construct one big table
I still would not know how to write the code anyway so it makes no difference to me.

Thanks for your reply.
transfield is offline   Reply With Quote
Old 03-05-2007, 03:12 AM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
Code:
SELECT SUM(price)/COUNT(*) FROM table1 WHERE full_add LIKE '%%' GROUP BY full_add
UNION SELECT SUM(price)/COUNT(*) FROM table2 WHERE full_add LIKE '%%' GROUP BY full_add
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 03-05-2007, 03:22 AM   #7 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Hi DJMaze,
This is a formula for average & not median. Anyway, I'm not asking for a formula for median. I'm asking why the existing median calculation is not looping through the search results.

Thank you for your initiative.
transfield is offline   Reply With Quote
Old 03-05-2007, 10:25 AM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
instead of that foreach loop where you add everthing in LIKE statements with OR separating them, place your query loop in the body of that foreach.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-06-2007, 02:02 AM   #9 (permalink)
transfield
Code Monkey
 
Join Date: Mar 2006
Posts: 35
transfield is on a distinguished road
Quote:
Originally Posted by teknomage1 View Post
instead of that foreach loop where you add everthing in LIKE statements with OR separating them, place your query loop in the body of that foreach.
Could you show me how to write the code please?
transfield is offline   Reply With Quote
Old 03-06-2007, 12:07 PM   #10 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
You might need to fill in some details, like define the myMedian function but this is the general idea.
PHP Code:
foreach($emails as $e){
     
$email_str="full_add LIKE '%".mysql_escape_string($e)."%'";
     for(
$j 3$j <= 6$j++) 
     { 
      echo 
'<b>Median Price from table0'.$j.'</b><br>'
      
$query "SELECT price FROM table0$j WHERE $email_str ORDER BY price DESC $limit"
           
$result mysql_query($query); 
           
/* The median calculation starts here. */
           
$resultArray=array(); 
           while ( 
$row=@mysql_fetch_array($result,MYSQL_NUM) ) { 
            
$resultArray[]=$row[0]; 
           }
           
$median myMedian($resultArray);
           
/* The median calculation ends here. */    
           
echo $emails[$i].' - $'.$median.'<br>';
     }

Basically, your code was getting every price that matched any of your input words and then it couldn't differentiate between them after you calculated the median. This code fetches the price for each word and calculates their median individually.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-query The Query Results transfield PHP 14 02-19-2007 12:55 PM
socket based CS server query Redline PHP 11 06-18-2006 05:20 PM
Need help optimizing a query Belisarius Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 3 12-23-2005 06:33 AM
Inserting the results of a select query into seperate tables Ste_Boro PHP 1 02-17-2005 05:05 AM
a query type of questiton sde Program Design and Methods 6 08-21-2003 04:55 PM


All times are GMT -8. The time now is 05:20 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting