This is a very rough outline of achieving desired result.
When I wrote this I made a lot of assumptions, which might prove wrong. code might require tweaking.
For example you have to use actual real data for connection.
SortedOrder
, currentTokenArray
, partner_id
and $sequenceArray
variables
have to come from somewhere.
Also the biggest aasumption currentTokenArray
is an array of fixed length, globally.
The code has to be tweaked for dynamic currentTokenArray
.
I tried to put as much comments as I could.
<?php
// Update. A better error reporting setup
// Turn on displaying errors
ini_set('display_errors', 1);
// Set reporting to everything.
ini_set('error_reporting', E_ALL);
// Make Mysqli trow exceptions instead of warnings.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
/** I assume SortedOrder has to come from somewhere also, for testing purposes it's set to 0.
However this is not really correct, SortedOrder can be defined anywhere,
probably somewhere at global scope, but that is an assumption.
The functions are not really needed, but I will leave them because it might be easier to understand
where arrays come from and what sizes they are.
Also you can always replace the redundant generation with some actual code for getting values
*/
function getToken() {
$token = array(); // Creating example array for holding the data
// Filling the array with some data
for($i=0; $i<10; ++$i) {
array_push($token, $i); //Here we add an element to the end of the array.
}
return $token;
}
function getSequenceArr() {
$sequenceArray = array(); // Creating example array for holding the data
// Filling the array with some data
for($i = 30; $i < 40; ++$i) {
array_push($sequenceArray, $i); //Here we add an element to the end of the array
}
return $sequenceArray;
}
$SortedOrder = 0;
/** In reality this should also be somewhere else. At the include file, or even the config file.
For testing purposes.
*/
// Connect to database, use actual data for host, username, password and database name
$conn = mysqli_connect("127.0.0.1", "username", "pass", "database");
// I assume token has to come from somewhere
$currentTokenArray = getToken();
// sequenceArray has also to come from somewhere.
$sequenceArray = getSequenceArr();
// We only have to implode array once. Result is a string.
$imploded = implode(", ", $currentTokenArray);
echo "$imploded</br>";
// partner_id comes from somewhere, for example purposes it is set to one. I am assuming partner_id is number
$partner_id = 1;
// Make placeholders for numbers
$in = str_repeat('?,', count($currentTokenArray) - 1) . '?';
$types = str_repeat('d', count($currentTokenArray));
// This is where param binding comes in. We put ? to places where binding will occur. Explanation sucks, but I suck at good explaining.
// Docs shed more light on it. As it was pointed out we only need to do it once
$query = "UPDATE Users SET SortedOrder = ? where WaitLessNumber = ? AND TokenNumber IN ($in)";
$stmt = $conn->prepare($query); // Preparing the query
// We can't iterate using foreach as we have to stop before the last element in the array.
for($i = 0; $i < count($currentTokenArray)-2; $i++) {
$token = $currentTokenArray[$i]; // Get current token. I am assuming here token is a number as it is used in calculations.
$SortedOrder = ((($SortedOrder+$currentTokenArray[$i])+($SortedOrder+$currentTokenArray[$i+1]))/2);
echo "$SortedOrder</br>";
// Binding values. Operator ... is arcument unpacking
$stmt->bind_param('dd'.$types, $SortedOrder, $partner_id, ...$currentTokenArray);
$stmt->execute();
printf("%d Rows updated.</br>", $stmt->affected_rows); // Check how much rows were updated;
}
$stmt->close(); // Close statement
$conn->close(); // Close connection
The implementation of storing SortedOrder
sucks a bit also. The value exists only when the script is running. To keep it when the script is not running
But that's the easiest way it can be done.
I you want to keep the value of SortedOrder
between two scripts running either save it to database or even plain text file