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]. - 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 -
<!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
5 1380 pbmods 5,821
Recognized Expert Expert
Heya, poreko.
Given the example array you provided above: -
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?
Dormilich 8,658
Recognized Expert Moderator Expert
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
the array : Array ( [0] => 2.5 [1] => 120 [2] => 128 [3] => 2 [4] => 1000 ) must be substracted from the 3rd element of this array: - Array
-
(
-
[0] => 1
-
[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
-
)
pbmods 5,821
Recognized Expert Expert
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: -
$sum = array();
-
$keys = array('PROCESSOR (Ghz)', 'STORAGE (GB)', 'etc.');
-
-
while( $row = $DB->FetchArray($query) )
-
{
-
foreach( $keys as $key )
-
{
-
$sum[$key][] = (int) $row[$key] - $input_arrays[$key];
-
}
-
}
-
hi PBMODS thank you for ur suggestion i have tried it but i am having lot of errors -
<!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>
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: suzee_q00 |
last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it. Here's the code:
while ( ( fgets ( buf, BUFLEN, fin ) ) != NULL ) /* pulls a line from "fin" up to the length of BUFLEN and stores it in "buf" */
{...
|
by: Doug_J_W |
last post by:
I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are of
different lengths (e.g. usually between 25 and 45 rows. The columns in
each file have the same length). The text files have been numbered...
|
by: ericstein81 |
last post by:
I have tried to develop a code using arrays and void functions to take the current earnings of employees and using it to calculate their future income in ten years. This was performed by a 8% percent increase every year. below i've posted my attempt and for some reason the code will compile successfully however, when trying to run the program...
|
by: polas |
last post by:
Afternoon everyone. I am having some trouble and thought a few of you
might be able to help me... I have a simple function to copy the
contents of one array into another - the arguments to the function are
the first array (to copy into), the second array (copy from) and the
number of items to copy, the prototype is void cpyarray (float * a,...
|
by: GesterX |
last post by:
First of all I'm new to this site but it certainly looks like a place that i will be visiting more often!
Onto my problem.
I am creating a Hotel Bussiness project in java using BlueJ
The classes are as follows:
Hotel - Deals with the major functions of the hotel (i.e guests booking in, finding rooms etc.)
Room - Used to decide whether a...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |