Decimals For negative PHP values - php

I am writing a bit of php code to output a random value
$max_mal = (3 - $oray);
$oray = 1;
$max = 100;
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
and for each loop goes here
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
I am looking to get decimal values upto 5 numbers, but once a negative value comes it results out as 0.000-1 instead of -0.00001
$myresultb = str_pad($mario, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
$total_score = 300;
echo $myresultf;
Secondly I am new to php learning so am I doing this PHP correct or it needs improvement
I have a div to show total score like this
<div id="total_score"></div>
and another div to show current score which value comes as echo $myresultf;
<div id="current_score"></div>
I want to update total score in real time with jquery wheneven button is clicked and <?php echo $myresultf ?> is refreshed in real time also
$("#play").click(function() {
var currentscore = $("#current_score").val();
var totalscore = $("#total_score").val();
how to do this.....
});

Try this:
$max = 100;
$oray = 1;
$max_mal = (3 - $oray);
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
$negative_var=false;
if($myresult < 0)
{
$negative_var=true;
$myresult = 0-$myresult;
}
$myresultb = str_pad($myresult, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
if($negative_var)
$myresultf="-".$myresultf;
$total_score = 300;
echo $myresultf;

simple use as follow:
$myresultb =str_replace('-','',$myresultb);
if($myresult == -1) {
$myresultf = '-0.'.$myresultb.'<br/>';
}
else {
$myresultf = '0.' . $myresultb . '<br/>';
}

Related

PHP generate football schedule

I have 38 days and 20 clubs (EPL).
How can I generate not repeated matches for this clubs in this days (schedule)?
For example:
Day 1:
club1 - club2
club3 - club4
...
club19 - club 20
Day 2:
club1 - club3
club2 - club4
...
club20 - club18
Each club plays with other two games (home and away). Respectively do not play with himself.
My thinks:
$clubs1 = array();
$clubs2 = array();
$days = range(1, 38);
$calendar = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $item) {
$clubs1[] = $item['id'];
$clubs2[] = $item['id'];
}
shuffle($clubs1);
shuffle($clubs2);
$total = (count($clubs1) * 2) - 2;
for ($j = 1; $j <= $total; $j ++) {
$day = $days[$j];
for ($i = 0; $i < count($clubs1); $i++) {
WHAT I SHOULD DO IN THIS BODY?
}
}
You need only one clubs array
1) remove $clubs2
2) rename $clubs1 to $clubs
3) remove whole for structure
//for testing: $clubs=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$countofteams=count($clubs);
$c=1;
for($j=0;$j<2;$j++) //home/away
for($i=1;$i<$countofteams;$i++){ //move teams
echo '----DAY '.$c++.'----<br>';
for($a=0;$a<$countofteams;$a++) //all teams are playing
echo 'Match '.$clubs[$a].' vs '.$clubs[($a+$i)%$countofteams].'<br>';
}
$team = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $k => $item) {
$team[$k+1] = $item['id'];
}
$all_team = count($team);
$k = $all_team/2;
$days = range(7, 100, 2); // first halh of season
$days2 = range(55, 100, 2); // second half
// 1 tour
for ($i=1; $i <= $k; $i++) {
$pars[] = $days[0].'|'.$team[$i].'|'.$team[($all_team-$i+1)];
$pars[] = $days2[0].'|'.$team[($all_team-$i+1)].'|'.$team[$i];
}
// Next tours
for($i=2; $i<$all_team; $i++)
{
$team2 = $team[2];
for($y=2;$y<$all_team;$y++)
{
$team[$y] = $team[$y+1];
}
$team[$all_team] = $team2;
for($j=1;$j<=$k;$j++)
{
$pars[] = $days[$i - 1].'|'.$team[$j].'|'.$team[($all_team-$j+1)];
$pars[] = $days2[$i - 1].'|'.$team[($all_team-$j+1)].'|'.$team[$j];
}
}
Here's my solution, replace the $clubs array with your result set of club IDs from database. The only slight inaccuracy with real-world EPL is that the second half of the season will mirror the first half exactly.
See if this adaption of http://board.phpbuilder.com/showthread.php?10300945-Round-Robin-Generator is any better :) - just for first half of the season.
$clubs = array(
'che', 'swa', 'ast', 'manc', 'liv', 'tot', 'ars', 'sot', 'hul', 'stok', 'wham', 'qpr', 'sun', 'mutd', 'lei', 'new', 'eve', 'wba', 'cry', 'bur',
);
shuffle($clubs);
$num_players = count($clubs) - 1;
// Set the return value
$ret = '';
// Generate the pairings for each round.
for ($round = 0; $round < $num_players; $round++) {
$ret .= '<h3>' . ($round + 1) . '</h3>';
$players_done = array();
// Pair each player except the last.
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
// Select opponent.
$opponent = $round - $player;
$opponent += ($opponent < 0) ? $num_players : 1;
$playerName = $clubs[$player];
$opponentName = $clubs[$opponent];
// Ensure opponent is not the current player.
if ($opponent != $player) {
// Choose colours.
if (($player + $opponent) % 2 == 0 xor $player < $opponent) {
// Player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
// Player plays black.
$ret .= "$opponentName - $playerName $br";
}
// This pair of players are done for this round.
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
// Pair the last player.
if ($round % 2 == 0) {
$playerName = $clubs[$num_players];
$opponent = ($round + $num_players) / 2;
$opponentName = $clubs[$opponent];
// Last player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
$opponent = ($round + 1) / 2;
// Last player plays black.
$ret .= "$opponentName - $playerName $br";
}
}
echo $ret;

How do I use Holt-Winters Seasonal Dampened Method to compute a two-month sales projection in PHP?

Holt-Winters is introduced here:
http://en.wikipedia.org/wiki/Holt-Winters
The Seasonal Dampened version of it is discussed here (scroll down the page):
http://otexts.com/fpp/7/5/
In a nutshell, it basically looks at 3 things:
long-term trend
short-term trend
seasonal trend
It also doesn't average those together, because really what you need is weighted averaging, where seasonal and short-term are more significant than long-term trend, naturally, with financial data trends.
Given $anYear1 and $anYear2, how do I apply the Holt-Winters Seasonal Dampened Method to forecast 2 more months past the end of $anYear2? Assume $anYear1 is an array of 12 numbers. Assume $anYear2 is an array of a range of 0 to 12 numbers.
So, I can fill it with random data like so:
<?php
$anYear1 = array();
$anYear2 = array();
$nStop = 10; // so we need 11 and 12 of the year
for ($i = 1; $i <= 12; $i++) {
$anYear1[$i] = rand(200,500);
if ($i <= $nStop) {
// give it a natural lift like real financial data
$anYear2[$i] = rand(400,700);
}
}
$nSeasonRange = 4; // 4 months in a business quarter
Therefore, I want to create a function like so:
function forecastHoltWinters($anYear1, $anYear2, $nSeasonRange = 4) {
///////////////////
// DO MAGIC HERE //
///////////////////
// an array with 2 numbers, indicating 2 months forward from end of $anYear2
return $anForecast;
}
$anForecast = forecastHoltWinters($anYear1, $anYear2, $nSeasonRange);
echo "YEAR 1\n";
print_r($anYear1);
echo "\n\nYEAR 2\n"
print_r($anYear2);
echo "\n\nTWO MONTHS FORECAST\n";
print_r($anForecast);
Note: I have found a Github example here, but it doesn't show how to do a projection. It is also discussed here.
I found a way to adapt Ian Barber's function to do what I needed.
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
$anYear1 = array();
$anYear2 = array();
$nStop = 10;
for($i = 1; $i <= 12; $i++) {
$anYear1[$i] = rand(100,400);
if ($i <= $nStop) {
$anYear2[$i+12] = rand(200,600);
}
}
print_r($anYear1);
print_r($anYear2);
$anData = array_merge($anYear1,$anYear2);
print_r(forecastHoltWinters($anData));
function forecastHoltWinters($anData, $nForecast = 2, $nSeasonLength = 4, $nAlpha = 0.2, $nBeta = 0.01, $nGamma = 0.01, $nDevGamma = 0.1) {
// Calculate an initial trend level
$nTrend1 = 0;
for($i = 0; $i < $nSeasonLength; $i++) {
$nTrend1 += $anData[$i];
}
$nTrend1 /= $nSeasonLength;
$nTrend2 = 0;
for($i = $nSeasonLength; $i < 2*$nSeasonLength; $i++) {
$nTrend2 += $anData[$i];
}
$nTrend2 /= $nSeasonLength;
$nInitialTrend = ($nTrend2 - $nTrend1) / $nSeasonLength;
// Take the first value as the initial level
$nInitialLevel = $anData[0];
// Build index
$anIndex = array();
foreach($anData as $nKey => $nVal) {
$anIndex[$nKey] = $nVal / ($nInitialLevel + ($nKey + 1) * $nInitialTrend);
}
// Build season buffer
$anSeason = array_fill(0, count($anData), 0);
for($i = 0; $i < $nSeasonLength; $i++) {
$anSeason[$i] = ($anIndex[$i] + $anIndex[$i+$nSeasonLength]) / 2;
}
// Normalise season
$nSeasonFactor = $nSeasonLength / array_sum($anSeason);
foreach($anSeason as $nKey => $nVal) {
$anSeason[$nKey] *= $nSeasonFactor;
}
$anHoltWinters = array();
$anDeviations = array();
$nAlphaLevel = $nInitialLevel;
$nBetaTrend = $nInitialTrend;
foreach($anData as $nKey => $nVal) {
$nTempLevel = $nAlphaLevel;
$nTempTrend = $nBetaTrend;
$nAlphaLevel = $nAlpha * $nVal / $anSeason[$nKey] + (1.0 - $nAlpha) * ($nTempLevel + $nTempTrend);
$nBetaTrend = $nBeta * ($nAlphaLevel - $nTempLevel) + ( 1.0 - $nBeta ) * $nTempTrend;
$anSeason[$nKey + $nSeasonLength] = $nGamma * $nVal / $nAlphaLevel + (1.0 - $nGamma) * $anSeason[$nKey];
$anHoltWinters[$nKey] = ($nAlphaLevel + $nBetaTrend * ($nKey + 1)) * $anSeason[$nKey];
$anDeviations[$nKey] = $nDevGamma * abs($nVal - $anHoltWinters[$nKey]) + (1-$nDevGamma)
* (isset($anDeviations[$nKey - $nSeasonLength]) ? $anDeviations[$nKey - $nSeasonLength] : 0);
}
$anForecast = array();
$nLast = end($anData);
for($i = 1; $i <= $nForecast; $i++) {
$nComputed = round($nAlphaLevel + $nBetaTrend * $anSeason[$nKey + $i]);
if ($nComputed < 0) { // wildly off due to outliers
$nComputed = $nLast;
}
$anForecast[] = $nComputed;
}
return $anForecast;
}

Optimized Logic to find high score

Basically this is related to a squash application where we have 2 scores. One is from winner point of view and another from loser point of view.
eg.
Score1: 11-5,11-5,11-5 (Winner point of view)
Score2: 5-11, 5-11,5-11 (Loser point of view)
Now in my logic i want to find which is the winner score and which is the loser score.
I have written my logic in the below way and it does work. But i want to know if their is any other better/optimized way of writing this.
$high1 = 0;
$high2 = 0;
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$score1Array = explode(",",$score1);
$size = sizeof($score1Array);
for($i = 0; $i < $size; $i++) {
$checkscore1 = explode("-",$score1Array[$i]);
if($checkscore1[0] < $checkscore1[1]) {
$high1++;
}else if($checkscore1[0] > $checkscore1[1]) {
$high2++;
}
}
if($high1 > $high2) {
$winningScore = $score2;
$losingScore = $score1;
}else{
$winningScore = $score1;
$losingScore = $score2;
}
echo $winningscore;
echo $losingscore;
What about something like this:
function is_winning($score) {
$split_scores = preg_split('/(-|,)/', $score);
$wins = $losses = 0;
for($i = 0; $i < count($split_scores) / 2; $i += 2) {
if($split_scores[$i] > $split_scores[$i + 1])
$wins++;
if($split_scores[$i] < $split_scores[$i + 1])
$losses++;
}
return $wins > $losses;
}
Assuming $score is formatted as in your question. You can then use it like this:
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
if(is_winning($score1)) {
$winning_score = $score1;
$losing_score = $score2;
} else {
$winning_score = $score2;
$losing_score = $score1;
}
echo $winning_score;
echo $losing_score;
The idea is to split the score into an array where the even numbered indexes have the left score and the odd numbered indexes the right score. We then count the number of wins and the number of losses. If there's more wins then losses then we return true since the score was a winning score. If there's not more wins then losses we simply return false.
This should work
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$l = $r = 0;
$score1_sets_arr = explode(',', $score1);
foreach ($score1_sets_arr as $set_score) {
$set_score_arr = explode('-', $set_score);
if ($set_score_arr[0] > $set_score_arr[1]) {
$l++;
} else {
$r++;
}
}
if ($l > $r) {
$winning_score = $score1;
$losing_score = $score2;
} else {
$winning_score = $score2;
$losing_score = $score1;
}
you can use this :
<?php
$high1 = 0;
$high2 = 0;
$score1 = "2-11,5-11,4-11,4-4";
$score2 = "11-2,11-5,11-4,4-4";
$explode = explode(",",$score1);
for($i=0;$i< sizeof($explode);$i++){
$explode2= explode("-", $explode[$i]);
if($explode2[0] <= $explode2[1]){
echo $explode2[0]."-";
echo $explode2[1]." ";
}
}
echo "<br />";
for($i=0;$i< sizeof($explode);$i++){
$explode2= explode("-", $explode[$i]);
if($explode2[1] >= $explode2[0]){
echo $explode2[1]."-";
echo $explode2[0]." ";
}
}
?>
for Winner point of view, all big score in left,otherwise in right. so u can just detect the first score.
$score1Array = explode(",",$score1);
$checkscore1 = explode("-",$score1Array[$i]);
if($checkscore1[0] < $checkscore1[1]) {
echo $score2;
echo $score1;
}else{
echo $score1;
echo $score2;
}
Fix: above code is wrong,try this:
$score1value = eval(str_replace(",","+",$score1));
$score2value = eval(str_replace(",","+",$score2));
if($score1value < $score2value) {
echo $score2;
echo $score1;
}else{
echo $score1;
echo $score2;
}

Split total into 5 different numbers

Ok what i'm wanting to do is split a number ($row['count']) into 5, this is easy enough if you want equal numbers:
$sum = ($row['count'] / 5);
$fsum = floor($sum);
but I want each number to be different and still add up to total ie $row['count'] how can this be achieved?
Update:
If this helps its to be used to update 5 rows in a database:
$query = "SELECT * FROM foo";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$sum = ($row['count'] / 5);
$fsum = floor($sum);
$id = $row['id'];
$update = "UPDATE foo SET foo1='$fsum', foo2='$fsum', foo3='$fsum', foo4='$fsum', foo5='$fsum' WHERE id='$id'";
mysql_query($update);
}// while
so ideally the $update query would be something like:
$update = "UPDATE foo SET foo1='$fsum1', foo2='$fsum2', foo3='$fsum3', foo4='$fsum4', foo5='$fsum5' WHERE id='$id'";
This is my take:
function randomize($sum, $parts) {
$part_no = count($parts);
$continnue_counter = 0;
while (count(array_unique($parts)) != $part_no) {
$changing = array_rand($parts, 2);
if (($parts[$changing[0]] - 1) == 0 || ($parts[$changing[1]] - 1) == 0) { // don't let them go under 1
++$continnue_counter;
// sometime one element get everything and others even out on 1
// just throw away everything you got so far and start over
if ($continnue_counter > 10) {
$parts = setup($sum, $part_no);
$continnue_counter = 0;
}
continue;
}
$continnue_counter = 0;
$signum = mt_rand(0, 100) % 2 ? 1 : -1;
$delta = $signum * mt_rand(1, min($parts[$changing[0]] - 1, $parts[$changing[1]] - 1)); // -1 to make sure they don't go under 0
$parts[$changing[0]] += $delta;
$parts[$changing[1]] -= $delta;
}
return $parts;
}
function setup($sum, $part_no) {
$parts = array_fill(0, $part_no, (int)($sum / $part_no));
// acount for the reminder of (int) cast
$reminder = $sum - array_sum($parts);
while ($reminder) {
$parts[array_rand($parts)] += 1;
--$reminder;
}
return $parts;
}
$part_no = 5;
$sum = 42;
$parts = randomize($sum, setup($sum, $part_no));
var_export($parts);
print array_sum($parts)
Update:
I've added a version that introduces a little more entropy.
Update2:
The more random one had a tendency to decrement everything to 1 except one part, added an explicit detection to deal with this. Still the algorithm behind it has unknown termination time.

For loop problem takes the third data as undefined

Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.

Categories