PHP - Moved: String Correction
This topic has been moved to PHP Regex.
http://www.phpfreaks.com/forums/index.php?topic=351099.0 Similar TutorialsThis topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=326004.0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="description" content="Nearest Neighbour Algorithm - Data Mining" /> <meta name="keywords" content="nearest neighbour, data mining, php, computer science, james hamilton" /> <meta http-equiv="Content-Language" content="en-gb" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta name="robots" content="all" /> <meta name="revisit-after" content="1 days" /> <title>Nearest Neighbour Algorithm - Data Mining11</title> <?PHP echo "<br><left><small>CONNECTION MESSAGES <br>"; echo "<ul>"; echo "<li>establishing connection with database</li>"; $conn = pg_Connect ("port=5432 dbname=my-database user=nobody"); if (!$conn) { echo "<li>connection to database failed</li>" ; exit; } echo "<li>connection to database ok</li>" ; ?> <link rel="stylesheet" media="all" href="style.css" type="text/css" /> <link rel="stylesheet" media="all" href="nearestneighbour.css" type="text/css" /> </head> <body> <?php if(isset($_GET[source])) { highlight_file(__FILE__); }else{ ?> <p>The nearest neighbour algorithm classifies a given instance based on a set of already classified instances (the training set), by calculating the distance to the nearest training case. The new instance is classified in the same class as the closest training case (i.e. the one that has the least differences/the one that is most the same). </p> <p> Try this out below. There are 10 instances in the training set. Use the select boxes to diagnose a new patient using the nearest neighbour algorithm. The diagnosis is made by selecting the diagnosis (class) of the instance in the training set with the least differences (i.e. closest distance). </p> <table id="patients" cellpadding="3" cellspacing="0"> <tr> <th>Patient ID</th> <th>age</th> <th>sex</th> <th>chestpaintype</th> <th>bloodpressure</th> <th>cholesterol</th> <th>fbs120</th> <th>restingegh</th> <th>maxheartrate</th> <th>angina</th> <th>slop</th> <th>thal</th> <th>diagnosis</th> </tr> <?php /* function getRows takes an SQL query string (and a optional array key type - NUM or ASSOC) and returns and array containing all the rows of the query. define(ASSOC, 1); define(NUM, 2); function getRows($sql, $type=ASSOC) { $result = mysql_query($sql); if($type == ASSOC) { while($row = mysql_fetch_array($result)) $ret[] = $row; }else if($type == NUM) { while($row = mysql_fetch_row($result)) $ret[] = $row; }else{ die("type should be NUM or ASSOC"); } return $ret; } */ $rows = getRows("SELECT * FROM heartcondition;", NUM); $closest = -1; if(isset($_POST[diagnose])) { $distance = array_fill(0, count($rows), 0); $instance = $_POST[instance]; for($i = 0; $i < count($rows); $i++) { for($j = 1; $j < 11; $j++) { if($rows[$i][$j] != $instance[$j]) $distance[$i]++; } } $closest = array_search(min($distance), $distance); $diagnosis = $rows[$closest][12]; } $a = 0; foreach($rows as $row) { ?> <tr<?php if($closest == $a) { ?> class="selected"<?php } ?>> <td><?=$row[0]?></td> <td><?=$row[1] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[2] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[3] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[4] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[5] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[6] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[7] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[8] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[9] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[10] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[11] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[12]?></td> <td><?=$distance[$a++]?></td> </tr> <?php } ?> <tr> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> <td><input type="hidden" name="instance[]" value="11" />11</td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][1] == "0") echo "selected=\"selected\"";?>>No</option> <option value="1"<?php if($_POST[instance][1] == "1") echo "selected=\"selected\"";?>>Yes</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][2] == "0") echo "selected=\"selected\"";?>>Sixties</option> <option value="1"<?php if($_POST[instance][2] == "1") echo "selected=\"selected\"";?>>forties</option> <option value="1"<?php if($_POST[instance][2] == "2") echo "selected=\"selected\"";?>>fifties</option> <option value="1"<?php if($_POST[instance][2] == "2") echo "selected=\"selected\"";?>>thirties</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][3] == "0") echo "selected=\"selected\"";?>>Male</option> <option value="1"<?php if($_POST[instance][3] == "1") echo "selected=\"selected\"";?>>Female</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][4] == "0") echo "selected=\"selected\"";?>>Angina</option> <option value="1"<?php if($_POST[instance][4] == "1") echo "selected=\"selected\"";?>>AbnomalAngina</option> <option value="1"<?php if($_POST[instance][4] == "2") echo "selected=\"selected\"";?>>No tang</option> <option value="1"<?php if($_POST[instance][4] == "3") echo "selected=\"selected\"";?>>Asymptomatic</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][5] == "0") echo "selected=\"selected\"";?>>onethirty+</option> <option value="1"<?php if($_POST[instance][5] == "1") echo "selected=\"selected\"";?>>oneten+</option> <option value="1"<?php if($_POST[instance][5] == "2") echo "selected=\"selected\"";?>>onefourty+</option> <option value="1"<?php if($_POST[instance][5] == "3") echo "selected=\"selected\"";?>>onefifty+</option> <option value="1"<?php if($_POST[instance][5] == "4") echo "selected=\"selected\"";?>>onetwenty+</option> <option value="1"<?php if($_POST[instance][5] == "5") echo "selected=\"selected\"";?>>onesisty+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][6] == "0") echo "selected=\"selected\"";?>>twoten+</option> <option value="1"<?php if($_POST[instance][6] == "1") echo "selected=\"selected\"";?>>twosixty+</option> <option value="1"<?php if($_POST[instance][6] == "2") echo "selected=\"selected\"";?>>twofifty+</option> <option value="1"<?php if($_POST[instance][6] == "3") echo "selected=\"selected\"";?>>twohundred+</option> <option value="1"<?php if($_POST[instance][6] == "4") echo "selected=\"selected\"";?>>twotwenty+</option> <option value="1"<?php if($_POST[instance][6] == "5") echo "selected=\"selected\"";?>>twoeight+</option> <option value="1"<?php if($_POST[instance][6] == "6") echo "selected=\"selected\"";?>>twothirty+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][7] == "0") echo "selected=\"selected\"";?>>f</option> <option value="1"<?php if($_POST[instance][7] == "1") echo "selected=\"selected\"";?>>t</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][8] == "0") echo "selected=\"selected\"";?>>Hyp</option> <option value="1"<?php if($_POST[instance][8] == "1") echo "selected=\"selected\"";?>>Normal</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][9] == "0") echo "selected=\"selected\"";?>>onethirty+</option> <option value="1"<?php if($_POST[instance][9] == "1") echo "selected=\"selected\"";?>>oneforty+</option> <option value="0"<?php if($_POST[instance][9] == "2") echo "selected=\"selected\"";?>>onefifty+</option> <option value="0"<?php if($_POST[instance][9] == "3") echo "selected=\"selected\"";?>>onesixty+</option> <option value="0"<?php if($_POST[instance][9] == "4") echo "selected=\"selected\"";?>>oneseventy+</option> <option value="0"<?php if($_POST[instance][9] == "5") echo "selected=\"selected\"";?>>oneeighty+</option> <option value="0"<?php if($_POST[instance][9] == "6") echo "selected=\"selected\"";?>>onetwenty+</option> <option value="0"<?php if($_POST[instance][9] == "7") echo "selected=\"selected\"";?>>onehundred+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][10] == "0") echo "selected=\"selected\"";?>>f</option> <option value="1"<?php if($_POST[instance][10] == "1") echo "selected=\"selected\"";?>>t</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][11] == "0") echo "selected=\"selected\"";?>>Falt</option> <option value="1"<?php if($_POST[instance][11] == "1") echo "selected=\"selected\"";?>>Up</option> <option value="1"<?php if($_POST[instance][11] == "2") echo "selected=\"selected\"";?>>Down</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][12] == "0") echo "selected=\"selected\"";?>>Rev</option> <option value="1"<?php if($_POST[instance][12] == "1") echo "selected=\"selected\"";?>>Normal</option> <option value="1"<?php if($_POST[instance][12] == "2") echo "selected=\"selected\"";?>>Fix</option> </select> </td> <td<?php if($diagnosis) { ?> class="selected"<?php } ?>><?=$diagnosis?></td> <td><input type="submit" name="diagnose" value="Diagnose" /></td> </form> </tr> </table> <p> <a href="<?=$_SERVER[PHP_SELF]?>?source">View Page Source</a> </p> <?php } ?> <p> Back to <a href="index.php">Data Mining</a> </p> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-1032885-1"; urchinTracker(); </script> </body> </html> is this code correct, i really need help with connecting the database This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=321050.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=330820.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=354362.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=319445.0 This topic has been moved quickly to PHP Regex because I need to go to work instead of lurking phpfreaks. http://www.phpfreaks.com/forums/index.php?topic=359460.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=358198.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=320682.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=322578.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=345658.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=345200.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=321479.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=342245.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=353613.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=353390.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=327134.0 Hi, I am trying to make some adjustments to uploadify.php which comes with the latest version of uploadify (3.0 beta), so that it works with a session variable that stores the login username and adds it to the path for uploads. Here is uploadify.php as it currently looks: Code: [Select] <?php session_name("MyLogin"); session_start(); $targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('m4a','mp3','flac','ogg'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } } echo $targetFolder; ?> I added Code: [Select] echo $targetFolder; at the bottom so that I could make sure that the string returned was correct, and it is, i.e. '/songs/nick'. For some reason though, uploads are not going to the correct folder, i.e. the username folder, but instead are going to the parent folder 'songs'. The folder for username exists, with correct permissions, and when I manually enter Code: [Select] $targetFolder = '/songs/nick';all works fine. Which strikes me as rather strange. I have limited experience of using php, but wonder how if the correct string is returned by the session variable, the upload works differently than with the manually entered string. Any help would be much appreciated. It's the last issue with a website that was due to go live 2 days ago! Thanks, Nick Hello all, I'm trying to change the end of a javascript call based on the end of the url string. The common part of all the url strings is sobi2Id=, I'm trying to do this with strstr but am having no luck. I'm new to php so my syntax knowledge is terrible! at the moment i've got Code: [Select] <?php $url = $_SERVER['REQUEST_URI']; $tag = strstr ($url, 'sobi2Id='); echo $tag; ?> but this returns an unexpected T_STRING, expecting ',' or ';' Can anyone debug this? I may well be being really silly! |