Connecting Tech Pros Worldwide Forums | Help | Site Map

Having trouble with arrays

Member
 
Join Date: Nov 2006
Posts: 38
#1: Dec 28 '08
Hi guys

I am trying to compare every row of mysql database against an array. My aim is to compute the euclidean distance. This is my reference array:

Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 )

And i Have written the following to compute first the difference betwenn the element.However my reference array can only be compare from the element of table i.e array[3].

Expand|Select|Wrap|Line Numbers
  1. foreach($array as $i=>$value){
  2.  
  3.             $sum[][$i]=($array[$i+3]- $input_arrays[$i]);
  4.             }
  5.  
Any time i run it it is given wrong results.
The following is my entire code
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5.  
  6. <head>
  7.   <title>Hello!</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12.  
  13.  
  14. <?php
  15.  
  16.  
  17. ////////////////////////////////////////////////////////
  18. include("mysql_class_2.php");
  19. include("input_A.php");
  20.  
  21. $home=$_REQUEST['usage'];
  22. $outside=$_REQUEST['outside'];
  23. $games=$_REQUEST['games'];
  24. $budget_range=$_REQUEST['Budget_Range'];
  25. $storage=$_REQUEST['storage'];
  26.  
  27.  
  28.   $DB = new mysql();
  29.  
  30.   $host = "localhost";
  31.   $name = "root";
  32.   $pass = "";
  33.   $db = "mytable";
  34.  
  35.   $connection = $DB->Connect($host, $name, $pass, $db);
  36.  
  37. $input_array= new input_A();
  38. $input_arrays=$input_array->Handle_Input($home,$outside,$games,$budget_range,$storage);
  39. print_r($input_arrays);
  40.  
  41.   //define an SQL statement and execute it
  42.   $sql = "SELECT * FROM laptop2";
  43.   $query = $DB->Query($sql);
  44.  
  45.  
  46.  
  47.   //output all rows from the statement
  48.   if($array = $DB->FetchArray($query)){
  49.   //extract($array);
  50.   //echo "<b>All rows</b><br /><br />Title: $title<b>
  51.  // Author: $author<br />";
  52.      $key=0;
  53.     echo "<table border=1>\n";
  54.     echo"<tr><td>ID</td><td>Brand</td><td>Model</td><td>Processor</td>
  55.     <td>Hard Drive</td><td> Graphic card</td>
  56.     <td>Battery life</td><td>price</td></tr>\n";
  57.  
  58.                 echo " <pre>";
  59.                 print_r($array);
  60.                 echo"</pre>";
  61.  
  62.  
  63.             foreach($array as $i=>$value){
  64.  
  65.             $sum[][$i]=($array[$i+3]- $input_arrays[$i]);
  66.             }
  67.  
  68.  
  69.  
  70.    do {
  71.  
  72.  
  73.  
  74.  printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</d><td>%s</d><td>%s</d> </tr>",$array['CODE'],$array['BRAND'],$array['MODEL'],$array['PROCESSOR (Ghz)'],
  75.             $array["STORAGE (GB)"],$array["GRAPHIC CARD (MB)"],$array["BATTERY"],$array["PRICE"]);
  76.  
  77.  
  78.  
  79.     }while($array=$DB->FetchArray($query));
  80.  
  81.  
  82.  
  83.     echo"</table>";
  84.   }
  85.  
  86.  
  87. //////////////////////////////////////////////////////////////////////////////////////////
  88.  
  89.         echo " <pre>";
  90.  
  91.                 print_r($sum);
  92.  
  93.                 echo"</pre>";
  94.  
  95.  // close the connection
  96.   $DB->Close();
  97.  
  98.  
  99.  
  100.  
  101.  
  102. ?>
  103.  
  104. </body>
  105.  
  106. </html>
  107.  
Can anyone spot a mistake?
Thanks

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Dec 28 '08

re: Having trouble with arrays


Heya, poreko.

Given the example array you provided above:
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 )
  2.  
What should $sum look like when your script is finished operating on it?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#3: Dec 28 '08

re: Having trouble with arrays


the reference array looks quite strange... (in terms of PHP syntax—at least to me)

according to php.net array keys may be only integers or strings. is "[0]" really one of these? (commas are missing too)

regards
Member
 
Join Date: Nov 2006
Posts: 38
#4: Dec 28 '08

re: Having trouble with arrays


the array :Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 ) must be substracted from the 3rd element of this array:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => 1
  4.     [CODE] => 1
  5.     [1] => Sony
  6.     [BRAND] => Sony
  7.     [2] => BZ11MN
  8.     [MODEL] => BZ11MN
  9.     [3] => 2.5
  10.     [PROCESSOR (Ghz)] => 2.5
  11.     [4] => 200
  12.     [STORAGE (GB)] => 200
  13.     [5] => 126
  14.     [GRAPHIC CARD (MB)] => 126
  15.     [6] => 2.5
  16.     [BATTERY] => 2.5
  17.     [7] => 500
  18.     [PRICE] => 500
  19. )
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Dec 29 '08

re: Having trouble with arrays


Try using mysql_fetch_assoc() instead of mysql_fetch_array(). It will make your life a little easier (PHP: mysql_fetch_assoc - Manual).

I'd probably approach it along these lines:
Expand|Select|Wrap|Line Numbers
  1. $sum = array();
  2. $keys = array('PROCESSOR (Ghz)', 'STORAGE (GB)', 'etc.');
  3.  
  4. while( $row = $DB->FetchArray($query) )
  5. {
  6.   foreach( $keys as $key )
  7.   {
  8.     $sum[$key][] = (int) $row[$key] - $input_arrays[$key];
  9.   }
  10. }
  11.  
Member
 
Join Date: Nov 2006
Posts: 38
#6: Dec 29 '08

re: Having trouble with arrays


hi PBMODS thank you for ur suggestion i have tried it but i am having lot of errors
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5.  
  6. <head>
  7.   <title>Hello!</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <?php
  13. include("mysql_class_2.php");
  14. include("input_A.php");
  15.  
  16. $home=$_REQUEST['usage'];
  17. $outside=$_REQUEST['outside'];
  18. $games=$_REQUEST['games'];
  19. $budget_range=$_REQUEST['Budget_Range'];
  20. $storage=$_REQUEST['storage'];
  21.  
  22.  
  23.   $DB = new mysql();
  24.  
  25.   $host = "localhost";
  26.   $name = "root";
  27.   $pass = "";
  28.   $db = "mytable";
  29.  
  30.   $connection = $DB->Connect($host, $name, $pass, $db);
  31.  
  32. $input_array= new input_A();
  33. $input_arrays=$input_array->Handle_Input($home,$outside,$games,$budget_range,$storage);
  34. print_r($input_arrays);
  35.  
  36.   //define an SQL statement and execute it
  37.   $sql = "SELECT * FROM laptop2";
  38.   $query = $DB->Query($sql);
  39.  
  40. $sum = array();
  41. $keys = array('PROCESSOR (Ghz)', 'STORAGE (GB)', 'GRAPHIC CARD (MB)','BATTERY','PRICE');
  42.  
  43. while( $row = $DB->FetchArray($query) )
  44. {
  45.   foreach( $keys as $key )
  46.   {
  47.     $sum[$key][] =  $row[$key] - $input_arrays[$key];
  48.   }
  49. }
  50.  
  51.  
  52.  
  53. ?>
  54.  
  55. </body>
  56.  
  57. </html>
  58.  
  59.  
Reply