Having trouble with arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poreko
    New Member
    • Nov 2006
    • 38

    Having trouble with arrays

    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].

    Code:
    foreach($array as $i=>$value){
    
                $sum[][$i]=($array[$i+3]- $input_arrays[$i]);
                }
    Any time i run it it is given wrong results.
    The following is my entire code
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
      <title>Hello!</title>
    </head>
    
    <body>
    
    
    
    <?php
    
    
    ////////////////////////////////////////////////////////
    include("mysql_class_2.php");
    include("input_A.php");
    
    $home=$_REQUEST['usage'];
    $outside=$_REQUEST['outside'];
    $games=$_REQUEST['games'];
    $budget_range=$_REQUEST['Budget_Range'];
    $storage=$_REQUEST['storage'];
    
    
      $DB = new mysql();
    
      $host = "localhost";
      $name = "root";
      $pass = "";
      $db = "mytable";
    
      $connection = $DB->Connect($host, $name, $pass, $db);
    
    $input_array= new input_A();
    $input_arrays=$input_array->Handle_Input($home,$outside,$games,$budget_range,$storage);
    print_r($input_arrays);
    
      //define an SQL statement and execute it
      $sql = "SELECT * FROM laptop2";
      $query = $DB->Query($sql);
    
    
    
      //output all rows from the statement
      if($array = $DB->FetchArray($query)){
      //extract($array);
      //echo "<b>All rows</b><br /><br />Title: $title<b>
     // Author: $author<br />";
         $key=0;
        echo "<table border=1>\n";
        echo"<tr><td>ID</td><td>Brand</td><td>Model</td><td>Processor</td>
        <td>Hard Drive</td><td> Graphic card</td>
        <td>Battery life</td><td>price</td></tr>\n";
    
                    echo " <pre>";
                    print_r($array);
                    echo"</pre>";
    
    
                foreach($array as $i=>$value){
    
                $sum[][$i]=($array[$i+3]- $input_arrays[$i]);
                }
    
    
    
       do {
    
    
    
     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)'],
                $array["STORAGE (GB)"],$array["GRAPHIC CARD (MB)"],$array["BATTERY"],$array["PRICE"]);
    
    
    
        }while($array=$DB->FetchArray($query));
    
    
    
        echo"</table>";
      }
    
          
    //////////////////////////////////////////////////////////////////////////////////////////
    
            echo " <pre>";
    
                    print_r($sum);
    
                    echo"</pre>";
    
     // close the connection
      $DB->Close();
    
    
    
    
    
    ?>
    
    </body>
    
    </html>
    Can anyone spot a mistake?
    Thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, poreko.

    Given the example array you provided above:
    Code:
    Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 )
    What should $sum look like when your script is finished operating on it?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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

      Comment

      • poreko
        New Member
        • Nov 2006
        • 38

        #4
        hi

        the array :Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 ) must be substracted from the 3rd element of this array:
        Code:
        Array
        (
            [0] => 1
            &#91;CODE] => 1
            [1] => Sony
            [BRAND] => Sony
            [2] => BZ11MN
            [MODEL] => BZ11MN
            [3] => 2.5
            [PROCESSOR (Ghz)] => 2.5
            [4] => 200
            [STORAGE (GB)] => 200
            [5] => 126
            [GRAPHIC CARD (MB)] => 126
            [6] => 2.5
            [BATTERY] => 2.5
            [7] => 500
            [PRICE] => 500
        )
        Last edited by Atli; Dec 29 '08, 10:53 AM. Reason: Replaced the [b] around the formatted text with [code].

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Try using mysql_fetch_ass oc() instead of mysql_fetch_arr ay(). It will make your life a little easier (PHP: mysql_fetch_ass oc - Manual).

          I'd probably approach it along these lines:
          [code=php]
          $sum = array();
          $keys = array('PROCESSO R (Ghz)', 'STORAGE (GB)', 'etc.');

          while( $row = $DB->FetchArray($qu ery) )
          {
          foreach( $keys as $key )
          {
          $sum[$key][] = (int) $row[$key] - $input_arrays[$key];
          }
          }
          [/code]

          Comment

          • poreko
            New Member
            • Nov 2006
            • 38

            #6
            hi

            hi PBMODS thank you for ur suggestion i have tried it but i am having lot of errors
            Code:
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            
            <html xmlns="http://www.w3.org/1999/xhtml">
            
            <head>
              <title>Hello!</title>
            </head>
            
            <body>
            
            <?php
            include("mysql_class_2.php");
            include("input_A.php");
            
            $home=$_REQUEST['usage'];
            $outside=$_REQUEST['outside'];
            $games=$_REQUEST['games'];
            $budget_range=$_REQUEST['Budget_Range'];
            $storage=$_REQUEST['storage'];
            
            
              $DB = new mysql();
            
              $host = "localhost";
              $name = "root";
              $pass = "";
              $db = "mytable";
            
              $connection = $DB->Connect($host, $name, $pass, $db);
            
            $input_array= new input_A();
            $input_arrays=$input_array->Handle_Input($home,$outside,$games,$budget_range,$storage);
            print_r($input_arrays);
            
              //define an SQL statement and execute it
              $sql = "SELECT * FROM laptop2";
              $query = $DB->Query($sql);
            
            $sum = array();
            $keys = array('PROCESSOR (Ghz)', 'STORAGE (GB)', 'GRAPHIC CARD (MB)','BATTERY','PRICE');
            
            while( $row = $DB->FetchArray($query) )
            {
              foreach( $keys as $key )
              {
                $sum[$key][] =  $row[$key] - $input_arrays[$key];
              }
            }
            
            
            
            ?>
            
            </body>
            
            </html>

            Comment

            Working...