How to count the amount of numbers in row column MySQL & PHP - php

I've been searching for a way to count the amount of numbers in one row column spaced by ",". (But nothing found, I use MySQL and what to count with the use of PHP) This is going to be done for whole the database to find the total amount of numbers (by 100'reds). What I do is storing multiple numbers like the following.
100, 200, 300, 400
What I want is to get the amount of numbers in this column to be 4 and add this to the next row column number. Which could be
200
and therefore equal to 1 + 4 = 5.
To show it the database way see here
See database sample if it helps
The total number should then endup beeing 6.
I want to hear if it is possible in someway to do this?
Thanks in advance :)

You can write code as:-
<?php
$rows = array( '303, 117, 210, 211', '117', '444');
$count = 0;
foreach( $rows as $row ) {
$count += count(explode(',', $row));
}
echo "total : " . $count;
?>

To count the amount of numbers in this column you could use following:
$count_of_numbers = sizeof(explode(",",$row['yourColumn']));
Now add em up and you are done ;-)

Related

PHP likely chance

Okay, so i don't really know how I go about this.
I'm currently working on a lottery system for a game.
I have a table with virtual items which I want to randomly select by a likely chance.
Table examples:
ID = 1, item_name = Sword, likely_chance = 75
ID = 2, Item_name = 10,000, likely_chance = 20
For id 2, 10,000 represents 10,000 coins.
I want to come up with an algorithm which will select a item with a higher chance of selecting a higher likely chance but also still be able to win a item with a lower likely chance rarely.
If you have items with "likely chances" of C1, C2, C3...Cn, then you can calculate the sum Ctotal.
Then, you can get a random value between 0 and Ctotal, and walk through your array (order is irrelevant) until the sum of "skipped" items exceeds this random value.
For example, in your case, Ctotal = 75 + 20 = 95. Get a random number between 0 and 95, and if it is less than 75 - give a sword; else - give 10000 coins. It will provide a fair winnings distribution according to your likely chances - 78.95% and 21.05%, respectively.
$items = ['Sword', '10000 coins'];
$chances = [70, 25];
$ctotal = array_sum($chances); echo "Total is $ctotal\n";
$rand = rand(0, $ctotal); echo "Rand is $rand\n";
$i = 0;
$currentSum = 0;
while (true)
{
$currentSum += $chances[$i];
if ($currentSum >= $rand)
{
echo "You win: ".$items[$i];
break;
}
$i++;
}
Here is the working Demo. Note that IDEOne remembers the last output and doesn't run this program again every time. The output will appear to be the same, but it is not.

Concat unique or delete duplicate string php/mysql

I need to remove duplicates from a table row.
to Combine the 3 numbers and generate all possible combinations, posted by .html form
(will be 6 if all numbers are different) Numbers must have 6 numbers each, like:
123, 234,etc.... Reduces the number of combinations if one number is the same of another,
like 112.
What i did, till now...
to isolate the numbers and store it in a row:
$cc=GetRow("SELECT numbers FROM table");
$n1=substr($cc, 0, 1);
$n2=substr($cc, 1, 1);
$n3=substr($cc, 2, 1);
//scrambling the numbers
$n1n2n3=$n1.$n2.$n3; //123 number stored
$n1n3n2=$n1.$n3.$n2; //132 number stored
$n2n1n3=$n2.$n1.$n3; //213 number stored
$n2n3n1=$n2.$n3.$n1; //231 number stored
$n3n1n2=$n3.$n1.$n2; //312 number stored
$n3n2n1=$n3.$n2.$n1; //321 number stored
$sql = sqlQuery("UPDATE table SET cc_concat = CONCAT_WS(',', '$n1n2n3', '$n1n3n2','$n2n1n3','$n2n3n1','$n3n1n2','$n3n2n1')");
But here´s the problem:
if the number is 112 will generate duplicates, only 3 are uniques:
$n1n2n3=$n1.$n2.$n3; //112 number stored
$n1n3n2=$n1.$n3.$n2; //121 number stored
$n2n1n3=$n2.$n1.$n3; //112 number stored
$n2n3n1=$n2.$n3.$n1; //121 number stored
$n3n1n2=$n3.$n1.$n2; //211 number stored
$n3n2n1=$n3.$n2.$n1; //211 number stored
Is there a way to update the table without the duplicates? or remove the duplicates
after update?
Thanks!
You could use an array to store the combinations and take advantage of the in_array() function to make sure you won't have duplicates:
$combinations = array();
if ( !in_array( $n1n2n3, $combinations ) ) $combinations[] = $n1n2n3;
if ( !in_array( $n1n3n2, $combinations ) ) $combinations[] = $n1n3n2;
// Do the same for the rest
// Then you could easily concatenate the result set from PHP.
$cc_concat = implode( ',', $combinations );
// Finally update the database.
$sql = sqlQuery("UPDATE table SET cc_concat = '{$cc_concat}' ");
Something like that should solve the problem.
Additionally you might prefer to use an algorithm to generate the combinations for you from an array of the three digits. For such algorithm you can check this thread out:
Get all permutations of a PHP array?

PHP not dividing correctly

I have some PHP code that is dividing two numbers that are pulled from a mySQL database however it is not computing correctly. When I echo $comm and $total_fix individually, the numbers are correct. However, when I echo the division of the two it is not the correct answer. Both numbers are DECIMAL(10,0) data type in the database. Below is the PHP code
$percent_comm = $comm / $total_fix;
$percent_comm = number_format($percent_comm, 2, '.', ',');
echo "<td align=\"center\">".$percent_comm."</td>";
here $comm = 2700, $total_fix = 75 but $percent_comm is computing to be 0.03 when it should be 36
From what I see on your comments, you are getting the $comm variable as a string with a comma, because of the format. I suggest to convert the formatted string into a valid number.
Mean while I'll recomend this:
$comm = '2,700';
$comm = str_replace(',','',$comm);
That remove the comma from your number.
From the variable names, you want to know $comm as a percentage of $total_fix. Your code almost does this: You correctly divide $comm/$total_fix, and it correctly gives you 0.027. But you got it backwards when you checked by hand: 36 is the result of dividing 2700/75 (i.e., $total_fix/$comm)
But to get a percentage, multiply by 100 the result of the division:
(75.0 / 2700) * 100 = 2.7 percent.
That's what your code should be getting.

Slicing / Limiting an Array by Value

Background;
to create a dropdown menu for a fun gambling game (Students can 'bet' how much that they are right) within a form.
Variables;
$balance
Students begin with £3 and play on the £10 table
$table(there is a;
£10 table, with a range of 1,2,3 etc to 10.
£100 table with a range of 10,20,30 etc to 100.
£1,000 table with a range of 100, 200, 300, 400 etc to 1000.)
I have assigned $table to equal number of zeros on max value,
eg $table = 2; for the £100 table
Limitations;
I only want the drop down menu to offer the highest 12 possible values (this could include the table below -IMP!).
Students are NOT automatically allowed to play on the 'next' table.
resources;
an array of possible values;
$a = array(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000);
I can write a way to restrict the array by table;
(the maximum key for any table is (9*$table) )//hence why i use the zeroes above (the real game goes to $1 billion!)
$arrayMaxPos = (9*$table);
$maxbyTable = array_slice($a, 0, $arrayMaxPos);
Now I need a way to make sure no VALUE in the $maxbyTable is greater than $balance.
to create a $maxBet array of all allowed bets.
THIS IS WHERE I'M STUCK!
(I would then perform "array_slice($maxBet, -12);" to present only the highest 12 in the dropdown)
EDIT - I'd prefer to NOT have to use array walk because it seems unnecessary when I know where i want the array to end.
SECOND EDIT Apologies I realised that there is a way to mathematically ascertain which KEY maps to the highest possible bid.
It would be as follows
$integerLength = strlen($balance);//number of digits in $balance
$firstDigit = substr($balance, 0, 1);
then with some trickery because of this particular pattern
$maxKeyValue = (($integerlength*9) - 10 + $firstDigit);
So for example;
$balance = 792;
$maxKeyValue = ((3*9) - 10 + 7);// (key[24] = 700)
This though works on this problem and does not solve my programming problem.
Optional!
First of all, assuming the same rule applies, you don't need the $a array to know what prices are allowed on table $n
$table = $n; //$n being an integer
for ($i = 1; $i <= 10; $i++) {
$a[] = $i * pow(10, $n);
}
Will generate a perfectly valid array (where table #1 is 1-10, table #2 is 10-100 etc).
As for slicing it according to value, use a foreach loop and generate a new array, then stop when you hit the limit.
foreach ($a as $value) {
if ($value > $balance) { break; }
$allowedByTable[] = $value;
}
This will leave you with an array $allowedByTable that only has the possible bets which are lower then the user's current balance.
Important note
Even though you set what you think is right as options, never trust the user input and always validate the input on the server side. It's fairly trivial for someone to change the value in the combobox using DOM manipulation and bet on sums he's not supposed to have. Always check that the input you're getting is what you expect it to be!

PHP Unique Random Numbers

What would be a good way to generate 7 unique random numbers between 1 and 10.
I can't have any duplicates.
I could write a chunk of PHP to do this (using rand() and pushing used numbers onto an array) but there must be a quick way to do it.
any advice would be great.
Create an array from 1 to 10 (range).
Put it in random order
(shuffle).
Select 7 items from the array (array_slice)
Populate an array with ten elements (the numbers one through ten), shuffle the array, and remove the first (or last) three elements.
Simple one-liner:
print_r(array_rand(array_fill(1, 10, true), 7));
Check out the comments in the php manual, there are several solutions for this.
An easy one is this one:
$min = 1;
$max = 10;
$total = 7;
$rand = array();
while (count($rand) < $total ) {
$r = mt_rand($min,$max);
if (!in_array($r,$rand)) $rand[] = $r;
}
Whole numbers? Well, if you want 7 out of 10 then you more efficiently DON'T want 3 out of 10.
Feel free to use any of the other responses but instead of creating 7 numbers start with 10 and eliminate 3. That will tend to speed things up by more than double.
The "shuffle" method has a MAJOR FALW. When the numbers are big, shuffle 3 billion indexs will instantly CAUSE 500 error. Here comes a best solution for really big numbers.
function getRandomNumbers($min, $max, $total) {
$temp_arr = array();
while(sizeof($temp_arr) < $total) $temp_arr[rand($min, $max)] = true;
return $temp_arr;
}
Say I want to get 10 unique random numbers from 1 billion to 4 billion.
$random_numbers = getRandomNumbers(1000000000,4000000000,10);
PS: Execution time: 0.027 microseconds

Categories