Add one to INT in table if not exists issue - php

(this is my first question so sorry if I do something wrong)
So I am trying to write a tournament standings program. The idea is submit 12 players through a form with their placement and then if that person is not already in the table, create a row in a DB and if it is in the table, add one to the appropriate column. Here is the applicable code :
for($i = 1; $i < 14; $i++) {
$g = $i - 1;
$fields = array('first', 'second', 'third', 'fourth', 'lose', 'tie');
$array[$g] = array($_GET['nameplayer' . $i], $_GET['placementplayer' . $i]); //Because there are 12 people, I used a for-loop for selecting player # hence the $array[$g]. It is being filled with the name and placement
//Insert Player Name
if($array[$g][0] != "") {
$k = $array[$g][0]; //Name of the person
$z = $_GET['tournyname'];
$t = $array[$g][1]; //Placement of the person (can be 1, 2, 3, 4, tie or lose)
$checkme = mysql_query("SELECT * FROM tournamentstandings WHERE name = '$k' AND tournyname = '$_GET[tournyname]'")or die(mysql_error()); //Getting the specific row for the guy I wanna add +1 to
if($t != "Tie" && $t != "Loss") { //Defining $y which is the field name for where the +1 should go
$y = $fields[$t - 1];
} else if ($t == "Tie") {
$y = $fields[5];
} else if ($t == "Loss") {
$y = $fields[4];
}
if (mysql_num_rows($checkme)){
$qone = mysql_query("SELECT $y FROM tournamentstandings WHERE name = '$k' AND tournyname = '$_GET[tournyname]'");
while($query = mysql_fetch_array($qone)) {
echo "updated";
mysql_query("UPDATE tournamentstandings SET $y = '$qone[$y] + 1' WHERE name = '$k' AND tournyname = '$z'") or die(mysql_error()); //THIS IS THE TROUBLE LINE
}
}
else if($y == "first" && !mysql_num_rows($checkme)) {
mysql_query("REPLACE INTO tournamentstandings(name, first, tie, second, third, fourth, lose, tournyname, totalplayed, totalpoints, pointspergame, winpercentage) VALUES('$k', '1', '0', '0', '0', '0', '0', '$z', '0', '0', '0', '0')")or die(mysql_error()); //Does the exact same thing but for $y == "second" and so on inserting 1 if the row does not already exist
}
So the issue is that sometimes when I add a new player, it goes to like 3 instead of 1 meaning that the 'trouble line' is called but no "update" (echoed) is displayed which is bizarre. If I comment out that line, it works fine but will not add +1 if the row already exists
So table wise lets say someone submits 12 names; name1 (first), name2 (second), name3 (third), name4 (fourth), name5 (lose) ... name 12 (lose) and none of those exist all under tournyname FFA (can be FFA, 1 Ally, 2 Ally, 6v6 which you can specify in the form)
it should be :
name | first | tie | second | third | fourth | lose | tournyname
name1| 1 | 0 | 0 | 0 | 0 | 0 | FFA
name2| 0 | 0 | 1 | 0 | 0 | 0 | FFA
name3| 0 | 0 | 0 | 1 | 0 | 0 | FFA
name4| 0 | 0 | 0 | 0 | 1 | 0 | FFA
name5| 0 | 0 | 0 | 0 | 0 | 1 | FFA
and so on
but what is happening is:
name | first | tie | second | third | fourth | lose | tournyname
name1| 3 | 0 | 0 | 0 | 0 | 0 | FFA
name2| 0 | 0 | 3 | 0 | 0 | 0 | FFA
name3| 0 | 0 | 0 | 2 | 0 | 0 | FFA
name4| 0 | 0 | 0 | 0 | 1 | 0 | FFA
name5| 0 | 0 | 0 | 0 | 0 | 1 | FFA
Or something random like that where the first 3 are broken and the last few are okay. When I try to update it later (just add one to an existing row, it works fine. It is just that initial submission. I have tried a while to figure out what it is and I dont know. I have tried :
Changing queries using REPLACE
Limiting the number of times the for loop runs to the number of submitted entries (does not have to be 12, can be 1-12
Adding AUTO_INCREMENT id
Primary Key changes
and a whole bunch of playing around with the select queries and such. Is there something small I am missing? :) If I am missing something that yall need just let me know!
Thanks very much!
INSERTED IMAGE OF ISSUE ::
All of the red boxes indicate the THIRD BATCH OF 12 all of the black boxes indicated the errors. Those numbers should be one because before I submitted the form, those rows did not exist. All of the names are unique so nothing in that pic should be above a one.

You can use insert...on duplicate key update in place of first searching for value and then updating the value. It will reduce MySQL operation to single query and must solve your problem as well.

It might simplify your life if you pushed the SQL calls to stored procs in the database, then you can test the SQL separately from the application code.

while($query = mysql_fetch_array($qone)) {
echo "updated";
$val=$query["$y"]+1;
mysql_query("UPDATE tournamentstandings SET $y = '$val' WHERE name = '$k' AND tournyname = '$z'") or die(mysql_error()); //THIS IS THE TROUBLE LINE
}

Related

Group Array items in PHP

I have the above array:
| Student First Name |Student Last Name | Age |Disability|
| Student_First_Name_1 |Student_Last_Name_1 | 30 | 1 |
| Student_First_Name_2 |Student_Last_Name_2 | 28 | 0 |
| Student_First_Name_3 |Student_Last_Name_3 | 21 | 0 |
| Student_First_Name_4 |Student_Last_Name_4 | 20 | 1 |
| Student_First_Name_5 |Student_Last_Name_5 | 22 | 0 |
and I want to grouped the students by age and Disability.
So if my code runs correctly I'll have the above results:
Student_First_Name_1 : Student_First_Name_4
Student_First_Name_3 : Student_First_Name_5
Student_First_Name_2
But instead I have the above:
Student_First_Name_1 : Student_First_Name_4
Student_First_Name_3 : Student_First_Name_5
Student_First_Name_2 : Student_Last_Name_3
Student_First_Name_2 : Student_Last_Name_5
My code is:
$StudentsForSID = $conn->prepare("SELECT * FROM members WHERE sid = :sid AND level = :level");
$StudentsForSID->execute([ 'sid' => $SelectedSID, 'level' => 'LRN_B1' ]);
while($row = $StudentsForSID->fetch(PDO::FETCH_ASSOC)){
$TempSelected[] = $row;
}
$count=count($TempSelected);
for($i=0; $i<$count-1; $i++){
for ($j = $i+1; $j < $count; $j++) {
if($TempSelected[$i]['disability']==$TempSelected[$j]['disability']){
if( abs($TempSelected[$i]['age']-$TempSelected[$j]['age']) <= 23 ){
$Student1 = $TempSelected[$j]['first_name'];
$Student2 = $TempSelected[$i]['first_name'];
print_r($Student1.'-'.$Student2.'<br/>');
}
}
}
}
I don't think I explained very well. So i edit the question.
What I want:
I want to make groups of 2 students with the same value in disability and the age difference between the 2 students to be equal or under 23.
So I have the above array with 5 students. From this array I'll make 3 groups and the groups will be the above (2 groups with 2 students with fulfilled the criteria, and 1 group with one student).
Can you help me?
Thank you
Why don't you use a double group by in your query?
Group by Age , Disability
This will actually group your results into two groups like you wanted so you will save the php sorting and those multiple if and for.

Keep subtracting value in loop getting mysql result php

I have an mysql table named example.
Id | Amount | Left | Filled
1 | 1 | 1 | 0
2 | 4 | 4 | 0
5 | 7 | 7 | 0
I have an variable named $var = 9
Now I have an array named $array with those ids as array([0] => 1, [1] => 2, [2] => 5) Which itself is a mysql result.
How do I make a loop so that ids in array keep subtracting the left and keep filling as per the amount but within the total value of $var so that my end result in table is
Id | Amount | Left | Filled
1 | 1 | 0 | 1
2 | 4 | 0 | 4
5 | 7 | 3 | 4
You can use while loop in order to loop on the ids and reduce the amount in each iteration.
I am not sure how you access your DB so I leave it pseudo.
Consider the following code:
$ids = array(1,2,5);
$value = 9;
function reduceAmount($id, $value) {
$query = mysqli_query($conn, "SELECT * FROM example WHERE Id='$id'");
$row = mysqli_fetch_array($query);
$take = min($row['Left'], $value); // the amount you can take (not more then what left)
$left = $row['Left'] - $take;
$filled = $row['Filled'] + $take;
$conn->query("UPDATE example SET Left='$left', Filled='$filled' WHERE Id='$id'")
return max(0, $value - $take);
}
while ($value > 0 && !empty($ids)) { // check if value still high and the options ids not finish
$id = array_shift($ids); //get first ID
$value = reduceAmount($id, $value);
}
You can check at the end of the loop if value still bigger then 0 - this can happen when no enough "Amount" in ids

Compare all columns between 2 rows in same table

I'm looking to compare all columns between two rows in a table using php/mysqli but just can't figure out how to do it. My table looks something like:
+----------+----------+----------+----------+----------+--------------+
| username | compare1 | compare2 | compare3 | compare4 | compare5 etc |
+----------+----------+----------+----------+----------+--------------+
| Adam | 1 | 0 | 1 | 1 | 0 |
| Billy | 1 | 1 | 1 | 1 | 0 |
| Charlie | 1 | 0 | 0 | 1 | 1 |
+----------+----------+----------+----------+----------+--------------+
I want to select say username Charlie as the child and username Adam as the parent then compare their values for all other columns (there's quite a few) in the table. If any of the child values is 0 where the parent value is 1 then the query returns false, otherwise, it returns true.
Thanks in advance!
You can do:
select (count(*) = 0) as flag
from t tp join
t tc
on tp.username = 'Adam' and tc.username = 'Charlie'
where (0, 1) in ( (tp.compare1, tc.compare1), (tp.compare2, tc.compare2),
. . .
);
Note: This assumes that each username has only one row.
An alternative query could be:
select c.compare1 >= p.compare1
and c.compare2 >= p.compare2
and c.compare3 >= p.compare3
and c.compare4 >= p.compare4
and c.compare5 >= p.compare5
as res
from mytable p
, mytable c
where p.username = 'Adam'
and c.username = 'Charlie';
Demo: http://sqlfiddle.com/#!9/b51672/2

How different two users with the same IP and user agent? [duplicate]

I'm building an analytic tool and I can currently get the user's IP address, browser and operating system from their user agent.
I'm wondering if there is a possibility to detect the same user without using cookies or local storage? I'm not expecting code examples here; just a simple hint of where to look further.
Forgot to mention that it would need to be cross-browser compatible if it's the same computer/device. Basically I'm after device recognition not really the user.
Introduction
If I understand you correctly, you need to identify a user for whom you don't have a Unique Identifier, so you want to figure out who they are by matching Random Data. You can't store the user's identity reliably because:
Cookies Can be deleted
IP address Can change
Browser Can Change
Browser Cache may be deleted
A Java Applet or Com Object would have been an easy solution using a hash of hardware information, but these days people are so security-aware that it would be difficult to get people to install these kinds of programs on their system. This leaves you stuck with using Cookies and other, similar tools.
Cookies and other, similar tools
You might consider building a Data Profile, then using Probability tests to identify a Probable User. A profile useful for this can be generated by some combination of the following:
IP Address
Real IP Address
Proxy IP Address (users often use the same proxy repeatedly)
Cookies
HTTP Cookies
Session Cookies
3rd Party Cookies
Flash Cookies (most people don't know how to delete these)
Web Bugs (less reliable because bugs get fixed, but still useful)
PDF Bug
Flash Bug
Java Bug
Browsers
Click Tracking (many users visit the same series of pages on each visit)
Browsers Finger Print
  - Installed Plugins (people often have varied, somewhat unique sets of plugins)
Cached Images (people sometimes delete their cookies but leave cached images)
Using Blobs
URL(s) (browser history or cookies may contain unique user id's in URLs, such as https://stackoverflow.com/users/1226894 or http://www.facebook.com/barackobama?fref=ts)
System Fonts Detection (this is a little-known but often unique key signature)
HTML5 & Javascript
HTML5 LocalStorage
HTML5 Geolocation API and Reverse Geocoding
Architecture, OS Language, System Time, Screen Resolution, etc.
Network Information API
Battery Status API
The items I listed are, of course, just a few possible ways a user can be identified uniquely. There are many more.
With this set of Random Data elements to build a Data Profile from, what's next?
The next step is to develop some Fuzzy Logic, or, better yet, an Artificial Neural Network (which uses fuzzy logic). In either case, the idea is to train your system, and then combine its training with Bayesian Inference to increase the accuracy of your results.
The NeuralMesh library for PHP allows you to generate Artificial Neural Networks. To implement Bayesian Inference, check out the following links:
Implement Bayesian inference using PHP, Part 1
Implement Bayesian inference using PHP, Part 2
Implement Bayesian inference using PHP, Part 3
At this point, you may be thinking:
Why so much Math and Logic for a seemingly simple task?
Basically, because it is not a simple task. What you are trying to achieve is, in fact, Pure Probability. For example, given the following known users:
User1 = A + B + C + D + G + K
User2 = C + D + I + J + K + F
When you receive the following data:
B + C + E + G + F + K
The question which you are essentially asking is:
What is the probability that the received data (B + C + E + G + F + K) is actually User1 or User2? And which of those two matches is most probable?
In order to effectively answer this question, you need to understand Frequency vs Probability Format and why Joint Probability might be a better approach. The details are too much to get into here (which is why I'm giving you links), but a good example would be a Medical Diagnosis Wizard Application, which uses a combination of symptoms to identify possible diseases.
Think for a moment of the series of data points which comprise your Data Profile (B + C + E + G + F + K in the example above) as Symptoms, and Unknown Users as Diseases. By identifying the disease, you can further identify an appropriate treatment (treat this user as User1).
Obviously, a Disease for which we have identified more than 1 Symptom is easier to identify. In fact, the more Symptoms we can identify, the easier and more accurate our diagnosis is almost certain to be.
Are there any other alternatives?
Of course. As an alternative measure, you might create your own simple scoring algorithm, and base it on exact matches. This is not as efficient as probability, but may be simpler for you to implement.
As an example, consider this simple score chart:
+-------------------------+--------+------------+
| Property | Weight | Importance |
+-------------------------+--------+------------+
| Real IP address | 60 | 5 |
| Used proxy IP address | 40 | 4 |
| HTTP Cookies | 80 | 8 |
| Session Cookies | 80 | 6 |
| 3rd Party Cookies | 60 | 4 |
| Flash Cookies | 90 | 7 |
| PDF Bug | 20 | 1 |
| Flash Bug | 20 | 1 |
| Java Bug | 20 | 1 |
| Frequent Pages | 40 | 1 |
| Browsers Finger Print | 35 | 2 |
| Installed Plugins | 25 | 1 |
| Cached Images | 40 | 3 |
| URL | 60 | 4 |
| System Fonts Detection | 70 | 4 |
| Localstorage | 90 | 8 |
| Geolocation | 70 | 6 |
| AOLTR | 70 | 4 |
| Network Information API | 40 | 3 |
| Battery Status API | 20 | 1 |
+-------------------------+--------+------------+
For each piece of information which you can gather on a given request, award the associated score, then use Importance to resolve conflicts when scores are the same.
Proof of Concept
For a simple proof of concept, please take a look at Perceptron. Perceptron is a RNA Model that is generally used in pattern recognition applications. There is even an old PHP Class which implements it perfectly, but you would likely need to modify it for your purposes.
Despite being a great tool, Perceptron can still return multiple results (possible matches), so using a Score and Difference comparison is still useful to identify the best of those matches.
Assumptions
Store all possible information about each user (IP, cookies, etc.)
Where result is an exact match, increase score by 1
Where result is not an exact match, decrease score by 1
Expectation
Generate RNA labels
Generate random users emulating a database
Generate a single Unknown user
Generate Unknown user RNA and Values
The system will merge RNA information and teach the Perceptron
After training the Perceptron, the system will have a set of weightings
You can now test the Unknown user's pattern and the Perceptron will produce a result set.
Store all Positive matches
Sort the matches first by Score, then by Difference (as described above)
Output the two closest matches, or, if no matches are found, output empty results
Code for Proof of Concept
$features = array(
'Real IP address' => .5,
'Used proxy IP address' => .4,
'HTTP Cookies' => .9,
'Session Cookies' => .6,
'3rd Party Cookies' => .6,
'Flash Cookies' => .7,
'PDF Bug' => .2,
'Flash Bug' => .2,
'Java Bug' => .2,
'Frequent Pages' => .3,
'Browsers Finger Print' => .3,
'Installed Plugins' => .2,
'URL' => .5,
'Cached PNG' => .4,
'System Fonts Detection' => .6,
'Localstorage' => .8,
'Geolocation' => .6,
'AOLTR' => .4,
'Network Information API' => .3,
'Battery Status API' => .2
);
// Get RNA Lables
$labels = array();
$n = 1;
foreach ($features as $k => $v) {
$labels[$k] = "x" . $n;
$n ++;
}
// Create Users
$users = array();
for($i = 0, $name = "A"; $i < 5; $i ++, $name ++) {
$users[] = new Profile($name, $features);
}
// Generate Unknown User
$unknown = new Profile("Unknown", $features);
// Generate Unknown RNA
$unknownRNA = array(
0 => array("o" => 1),
1 => array("o" => - 1)
);
// Create RNA Values
foreach ($unknown->data as $item => $point) {
$unknownRNA[0][$labels[$item]] = $point;
$unknownRNA[1][$labels[$item]] = (- 1 * $point);
}
// Start Perception Class
$perceptron = new Perceptron();
// Train Results
$trainResult = $perceptron->train($unknownRNA, 1, 1);
// Find matches
foreach ($users as $name => &$profile) {
// Use shorter labels
$data = array_combine($labels, $profile->data);
if ($perceptron->testCase($data, $trainResult) == true) {
$score = $diff = 0;
// Determing the score and diffrennce
foreach ($unknown->data as $item => $found) {
if ($unknown->data[$item] === $profile->data[$item]) {
if ($profile->data[$item] > 0) {
$score += $features[$item];
} else {
$diff += $features[$item];
}
}
}
// Ser score and diff
$profile->setScore($score, $diff);
$matchs[] = $profile;
}
}
// Sort bases on score and Output
if (count($matchs) > 1) {
usort($matchs, function ($a, $b) {
// If score is the same use diffrence
if ($a->score == $b->score) {
// Lower the diffrence the better
return $a->diff == $b->diff ? 0 : ($a->diff > $b->diff ? 1 : - 1);
}
// The higher the score the better
return $a->score > $b->score ? - 1 : 1;
});
echo "<br />Possible Match ", implode(",", array_slice(array_map(function ($v) {
return sprintf(" %s (%0.4f|%0.4f) ", $v->name, $v->score,$v->diff);
}, $matchs), 0, 2));
} else {
echo "<br />No match Found ";
}
Output:
Possible Match D (0.7416|0.16853),C (0.5393|0.2809)
Print_r of "D":
echo "<pre>";
print_r($matchs[0]);
Profile Object(
[name] => D
[data] => Array (
[Real IP address] => -1
[Used proxy IP address] => -1
[HTTP Cookies] => 1
[Session Cookies] => 1
[3rd Party Cookies] => 1
[Flash Cookies] => 1
[PDF Bug] => 1
[Flash Bug] => 1
[Java Bug] => -1
[Frequent Pages] => 1
[Browsers Finger Print] => -1
[Installed Plugins] => 1
[URL] => -1
[Cached PNG] => 1
[System Fonts Detection] => 1
[Localstorage] => -1
[Geolocation] => -1
[AOLTR] => 1
[Network Information API] => -1
[Battery Status API] => -1
)
[score] => 0.74157303370787
[diff] => 0.1685393258427
[base] => 8.9
)
If Debug = true you would be able to see Input (Sensor & Desired), Initial Weights, Output (Sensor, Sum, Network), Error, Correction and Final Weights.
+----+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+----+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----------+
| o | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | x11 | x12 | x13 | x14 | x15 | x16 | x17 | x18 | x19 | x20 | Bias | Yin | Y | deltaW1 | deltaW2 | deltaW3 | deltaW4 | deltaW5 | deltaW6 | deltaW7 | deltaW8 | deltaW9 | deltaW10 | deltaW11 | deltaW12 | deltaW13 | deltaW14 | deltaW15 | deltaW16 | deltaW17 | deltaW18 | deltaW19 | deltaW20 | W1 | W2 | W3 | W4 | W5 | W6 | W7 | W8 | W9 | W10 | W11 | W12 | W13 | W14 | W15 | W16 | W17 | W18 | W19 | W20 | deltaBias |
+----+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+----+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----------+
| 1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 0 | -1 | 0 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 0 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 |
| -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | -1 | -1 | 1 | -19 | -1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 |
| -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
| 1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 19 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 |
| -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | -1 | -1 | 1 | -19 | -1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | -1 | -1 | -1 | 1 | 1 | 1 |
| -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
+----+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+----+---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----+----+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----------+
x1 to x20 represent the features converted by the code.
// Get RNA Labels
$labels = array();
$n = 1;
foreach ( $features as $k => $v ) {
$labels[$k] = "x" . $n;
$n ++;
}
Here is an online demo
Class Used:
class Profile {
public $name, $data = array(), $score, $diff, $base;
function __construct($name, array $importance) {
$values = array(-1, 1); // Perception values
$this->name = $name;
foreach ($importance as $item => $point) {
// Generate Random true/false for real Items
$this->data[$item] = $values[mt_rand(0, 1)];
}
$this->base = array_sum($importance);
}
public function setScore($score, $diff) {
$this->score = $score / $this->base;
$this->diff = $diff / $this->base;
}
}
Modified Perceptron Class
class Perceptron {
private $w = array();
private $dw = array();
public $debug = false;
private function initialize($colums) {
// Initialize perceptron vars
for($i = 1; $i <= $colums; $i ++) {
// weighting vars
$this->w[$i] = 0;
$this->dw[$i] = 0;
}
}
function train($input, $alpha, $teta) {
$colums = count($input[0]) - 1;
$weightCache = array_fill(1, $colums, 0);
$checkpoints = array();
$keepTrainning = true;
// Initialize RNA vars
$this->initialize(count($input[0]) - 1);
$just_started = true;
$totalRun = 0;
$yin = 0;
// Trains RNA until it gets stable
while ($keepTrainning == true) {
// Sweeps each row of the input subject
foreach ($input as $row_counter => $row_data) {
// Finds out the number of columns the input has
$n_columns = count($row_data) - 1;
// Calculates Yin
$yin = 0;
for($i = 1; $i <= $n_columns; $i ++) {
$yin += $row_data["x" . $i] * $weightCache[$i];
}
// Calculates Real Output
$Y = ($yin <= 1) ? - 1 : 1;
// Sweeps columns ...
$checkpoints[$row_counter] = 0;
for($i = 1; $i <= $n_columns; $i ++) {
/** DELTAS **/
// Is it the first row?
if ($just_started == true) {
$this->dw[$i] = $weightCache[$i];
$just_started = false;
// Found desired output?
} elseif ($Y == $row_data["o"]) {
$this->dw[$i] = 0;
// Calculates Delta Ws
} else {
$this->dw[$i] = $row_data["x" . $i] * $row_data["o"];
}
/** WEIGHTS **/
// Calculate Weights
$this->w[$i] = $this->dw[$i] + $weightCache[$i];
$weightCache[$i] = $this->w[$i];
/** CHECK-POINT **/
$checkpoints[$row_counter] += $this->w[$i];
} // END - for
foreach ($this->w as $index => $w_item) {
$debug_w["W" . $index] = $w_item;
$debug_dw["deltaW" . $index] = $this->dw[$index];
}
// Special for script debugging
$debug_vars[] = array_merge($row_data, array(
"Bias" => 1,
"Yin" => $yin,
"Y" => $Y
), $debug_dw, $debug_w, array(
"deltaBias" => 1
));
} // END - foreach
// Special for script debugging
$empty_data_row = array();
for($i = 1; $i <= $n_columns; $i ++) {
$empty_data_row["x" . $i] = "--";
$empty_data_row["W" . $i] = "--";
$empty_data_row["deltaW" . $i] = "--";
}
$debug_vars[] = array_merge($empty_data_row, array(
"o" => "--",
"Bias" => "--",
"Yin" => "--",
"Y" => "--",
"deltaBias" => "--"
));
// Counts training times
$totalRun ++;
// Now checks if the RNA is stable already
$referer_value = end($checkpoints);
// if all rows match the desired output ...
$sum = array_sum($checkpoints);
$n_rows = count($checkpoints);
if ($totalRun > 1 && ($sum / $n_rows) == $referer_value) {
$keepTrainning = false;
}
} // END - while
// Prepares the final result
$result = array();
for($i = 1; $i <= $n_columns; $i ++) {
$result["w" . $i] = $this->w[$i];
}
$this->debug($this->print_html_table($debug_vars));
return $result;
} // END - train
function testCase($input, $results) {
// Sweeps input columns
$result = 0;
$i = 1;
foreach ($input as $column_value) {
// Calculates teste Y
$result += $results["w" . $i] * $column_value;
$i ++;
}
// Checks in each class the test fits
return ($result > 0) ? true : false;
} // END - test_class
// Returns the html code of a html table base on a hash array
function print_html_table($array) {
$html = "";
$inner_html = "";
$table_header_composed = false;
$table_header = array();
// Builds table contents
foreach ($array as $array_item) {
$inner_html .= "<tr>\n";
foreach ( $array_item as $array_col_label => $array_col ) {
$inner_html .= "<td>\n";
$inner_html .= $array_col;
$inner_html .= "</td>\n";
if ($table_header_composed == false) {
$table_header[] = $array_col_label;
}
}
$table_header_composed = true;
$inner_html .= "</tr>\n";
}
// Builds full table
$html = "<table border=1>\n";
$html .= "<tr>\n";
foreach ($table_header as $table_header_item) {
$html .= "<td>\n";
$html .= "<b>" . $table_header_item . "</b>";
$html .= "</td>\n";
}
$html .= "</tr>\n";
$html .= $inner_html . "</table>";
return $html;
} // END - print_html_table
// Debug function
function debug($message) {
if ($this->debug == true) {
echo "<b>DEBUG:</b> $message";
}
} // END - debug
} // END - class
Conclusion
Identifying a user without a Unique Identifier is not a straight-forward or simple task. it is dependent upon gathering a sufficient amount of Random Data which you are able to gather from the user by a variety of methods.
Even if you choose not to use an Artificial Neural Network, I suggest at least using a Simple Probability Matrix with priorities and likelihoods - and I hope the code and examples provided above give you enough to go on.
This technique (to detect same users without cookies - or even without ip address) is called browser fingerprinting. Basically you crawl as information about the browser as you can - better results can be achieved with javascript, flash or java (f.ex. installed extensions, fonts, etc.). After that, you can store the results hashed, if you want.
It's not infallible, but:
83.6% of the browsers seen had a unique fingerprint; among those with Flash or Java enabled, 94.2%. This does not include cookies!
More info:
https://panopticlick.eff.org/
https://wiki.mozilla.org/Fingerprinting
https://www.browserleaks.com/
The above mentioned thumbprinting works, but can still suffer colisions.
One way is to add UID to the url of each interaction with the user.
http://someplace.com/12899823/user/profile
Where every link in the site is adapted with this modifier. It is similar to the way ASP.Net used to work using FORM data between pages.
Have you looked into Evercookie?
It may or may not work across browsers. An extract from their site.
"If a user gets cookied on one browser and switches to another browser,
as long as they still have the Local Shared Object cookie, the cookie
will reproduce in both browsers."
You could do this with a cached png, it would be somewhat unreliable (different browsers behave differently, and it'll fail if the user clears their cache), but it's an option.
1: set up a Database that stores a unique user id as a hex string
2: create a genUser.php (or whatever language) file that generates a user id, stores it in the DB and then creates a true color .png out of the values of that hex string (each pixel will be 4 bytes) and return that to the browser. Be sure to set the content-type and cache headers.
3: in the HTML or JS create an image like <img id='user_id' src='genUser.php' />
4: draw that image to a canvas ctx.drawImage(document.getElementById('user_id'), 0, 0);
5: read the bytes of that image out using ctx.getImageData, and convert the integers to a hex string.
6: That is your unique user id that's now cached on the your users computer.
You can do it with etags. Although I am not sure if this legal as a bunch of lawsuits were filed.
If you properly warn your users or if you have something like an intranet website it might be ok.
You could potentially create a blob to store a device identifier ...
the downside is that the user needs to download the blob ( you can force the download ),
as the browser can't access the File System to directly save the file.
reference:
https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-22/blobs
Based on what you have said :
Basically I'm after device recognition not really the user
Best way to do it is to send the mac address which is the NIC ID.
You can take a look at this post :
How can I get the MAC and the IP address of a connected client in PHP?
Inefficient, but may give you the desired results, would be to poll an API on your side. Have a background process on the client side which sends user data at an interval. You will need a user identifier to send to your API. Once you have that you can send along any information associated to that unique identifier.
This removes the need for cookies and localstorage.
I can't believe, http://browserspy.dk still has not been mentioned here!
The site describes many features (in terms of pattern recognition), which could be used to build a classifier.
And of cause, for evaluating the features I'd suggest Support Vector Machines and libsvm in particular.
Track them during a session or across sessions?
If your site is HTTPS Everywhere you could use the TLS Session ID to track the user's session
create a cross-platform dummy (nsapi)plugin and generate a unique name for the plugin name or version when the user downloads it (eg after login).
provide a installer for the plugin / install it per policy
this will require the user to willingly install the identifier.
once the plugin is installed, the fingerprint of any (plugin enabled) browser will contain this specific plugin. To return the info to a server, a algorithm to effectively detect the plugin on client-side is needed, otherwise IE and Firefox >= 28 users will need a table of possible valid identifies.
This requires a relatively high investment into a technology that will likely be shut down by the browser-vendors. When you are able to convince your users to install a plugin, there may also be options like install a local proxy, use vpn or patch the network drivers.
Users that do not want to be identified (or their machines) will always find a way to prevent it.

Find lowest value from multiple array php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm developing a number based game to calculate the combined lowest number from a multiple array.
Example:
$arr1 = array('score_1'=>0,'score_2'=>5,'score_3'=>0,'score_4'=>2,'score_5'=>1);
$arr2 = array('score_1'=>3,'score_2'=>0,'score_3'=>2,'score_4'=>0,'score_5'=>0);
$arr3 = array('score_1'=>0,'score_2'=>0,'score_3'=>0,'score_4'=>4,'score_5'=>0);
The above example result will be:
score_1 = 0+3+0 = 3
score_2 = 5+0+0 = 5
score_3 = 0+2+0 = 2
score_4 = 2+0+4 = 6
score_5 = 1+0+0 = 1 /*This is the winning number*/
Each array is a submission from each user, stored in a single db field (instead of in separate fields eg.. score_1,score_e etc...)
I'm not using separate table field because the game requires 180 score fields later. To optimize the database I'm using an array instead of table field for each score.
I'm storing each row like this:
score_1:0,score_2:0 etc...
Later I'm looping through each row like:
$main_score[main_score] = $array_score[score_$key]=$value;
And the end result for each row:
$arr1 = array('score_1'=>0,'score_2'=>5,'score_3'=>0,'score_4'=>2,'score_5'=>1);
How do I make the calculation?
Explain me how am i gonna deal with this situation: Ref: below image.
This isn't it, there are more 210 individual inputs more to go.
Similar application
You really should read about database normalization, then if you normalize your database, it isn't important if you store 5 or 100 scores per user. Take a look at the following table schemas:
users
id
name
scores
id
user_id
score_nr
value
To fetch your example result from these tables, you have to use the following query:
SELECT `score_nr`, SUM(`value`) AS `value_sum`
FROM `scores` GROUP BY `score_nr`
To fetch the score_nr with the smallest sum:
SELECT `score_nr`, SUM(`value`) AS `value_sum`
FROM `scores` GROUP BY `score_nr` ORDER BY `value_sum` ASC LIMIT 1
Loop trough the arrays simultaneously and calculate the sum of them. Keep track of the smallest key and value:
$smallest_val = PHP_INT_MAX;
$smallest_key = '';
foreach($arr1 as $key => $val) {
echo $key ." = ". $val ."+". $arr2[$key] ."+". $arr3[$key] ."<br />";
$sum = $val + $arr2[$key] + $arr3[$key];
if($sum < $smallest_val) {
$smallest_val = $sum;
$smallest_key = $key;
}
}
echo "Winrar: ". $smallest_key . " with value " . $smallest_val;
Working example
i think you can make one new table on your database for scoring.
for example you have table game which primary key by game_id
table : scoring
------------------------------------
|id|game_id | score_no| arr | score |
------------------------------------
|1 | 0 | score_1 | 1 | 0 |
|2 | 0 | score_2 | 1 | 5 |
|3 | 0 | score_3 | 1 | 0 |
|4 | 0 | score_4 | 1 | 2 |
|5 | 0 | score_5 | 1 | 1 |
|6 | 0 | score_1 | 2 | 0 |
|7 | 0 | score_2 | 2 | 5 |
|8 | 0 | score_3 | 2 | 0 |
|9 | 0 | score_4 | 2 | 2 |
|10| 0 | score_5 | 2 | 1 |
|11| 0 | score_1 | 3 | 0 |
|12| 0 | score_2 | 3 | 5 |
|13| 0 | score_3 | 3 | 0 |
|14| 0 | score_4 | 3 | 2 |
|15| 0 | score_5 | 3 | 1 |
------------------------------------
and you can select it with query
SELECT score_no, SUM(`score`) AS `sum_score` FROM `scores` GROUP BY game_id, score_no
If you want to stick with your current schema, the solution with the least amount of code should be the following:
$summedScores = array_map('array_sum', array_merge_recursive($arr1, $arr2, $arr3));
asort($summedScores, SORT_NUMERIC);
reset($summedScores);
$winnerScoreNr = key($summedScores);

Categories