Array Calculation PHP - php

Again I am stuck with PHP array calculation, please if anyone understands my question try to help me out, let's explain...
[data.txt content]
Peer Call ID Duration Recv: Pack Lost ( %) Jitter Send: Pack Lost ( %) Jitter
139.59.232.196 0bb9262d6a1 00:01:12 0000003558 0000000000 ( 0.00%) 0.0000 0000001177 0000000000 ( 0.00%) 0.0200
139.59.232.196 41283499492 00:00:00 0000000000 0000000000 ( 0.00%) 0.0000 0000000000 0000000000 ( 0.00%) 0.0000
139.59.232.196 7033a541240 00:00:08 0000000000 0000000000 ( 0.00%) 0.0000 0000000019 0000000000 ( 0.00%) 0.0000
3 active SIP channels
PHP Code Starting from here.
$data = file_get_contents('./data.txt', TRUE);
$lines = explode("\n", $data);
foreach ($lines as $line) {
if (!preg_match('/( 0.00%)/', $line)) {
continue;
}
$data = explode(' ', $line);
$list[] = $data;
}
foreach ($list as $qoscalc) {
$average[] = ($qoscalc[17] * 1000 / 2);
$jitter[] = (int)$qoscalc[14];
$packet_loss[] = (int)$qoscalc[13];
}
print_r($average);
Till here the code is working fine it's giving me the output of $average array this >>
Array ( [0] => 10 [1] => 0 [2] => 0 )
After that, I couldn't do this math with the array, if I convert them in variable & I go with 1 data only then code is working fine, but when I try to get the result of all I couldn't make it, please help me if anyone understands my question.
$effective_latency = ($average + $jitter * 2 + 10 );
if ($effective_latency < 160) {
$r_value = 93.2 - ($effective_latency / 40);
} else {
$r_value = 93.2 - ($effective_latency - 120) / 10;
}
$r_value = $r_value - ($packet_loss * 2.5);
$mosresult = 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
$moslist[] = $mosresult;
I want to get all 3 array result, its suppose to be like this example: Array ( [0] => 4.40372901469 [1] => 3.40372901469 [2] => 4.90372901469 )
$i = 0; $t = 0; $e = 0; $g = 0; $f = 0; $p = 0; $b = 0;
foreach ($moslist as $mos) {
$i++;
if ($mos <= "5") {
$qosq = 'Excellent';
$e++;
} else if ($mos <= "4") {
$qosq = 'Good';
$g++;
} else if ($mos < "3") {
$qosq = 'Fair';
$f++;
} else if ($mos <= "2") {
$qosq = 'Poor';
$p++;
} else if ($mos <= "1") {
$qosq = 'Bad';
$b++;
} else {
continue;
}
$t++;
}
echo $qosq, "<br><br>\n";

If I understood the question correctly, the simplest solution would be to create a function for calculating the mosResult
function getMosResult($average,$jitter,$packet_loss)
{
$effective_latency = ($average + $jitter * 2 + 10 );
if ($effective_latency < 160) {
$r_value = 93.2 - ($effective_latency / 40);
} else {
$r_value = 93.2 - ($effective_latency - 120) / 10;
}
$r_value = $r_value - ($packet_loss * 2.5);
return 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
}
and then applying the function in a for loop on all the results like so
$length = count($list);
for($i = 0;$i < $length;$i++){
$mosList[]=getMosResult($average[$i],$jitter[$i],$packet_loss[$i]);
}
This solution would not be good for a larger project as you would quickly lose consistency between the three source arrays. For a more solid solution, look into objects or t least associative arrays.

Related

HTML PHP Math : Fill a progress bar using rank values

I got a progress bar to be filled showing remaining rank points to next level.
Ex: got 25 points and next rank is at 50 points so the bar must to be filled at 50%, if i got 40 point must to be 75% filled etc.
<div class="progress-bar" role="progressbar" aria-valuenow="<?php print $rank; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php print get_rank($rank) * next_rank($level) / 2; ?>%;">
The get_rank() function take Rank points and convert to remain points minus $rank value, then I'd pass the $rank value to level() function in order to retrieve the level and finally next_rank() function get the level and return the remaining points.
So the used math is (get_rank($rank) * next_rank($level) / 2) / 10.
Using this return the remaining value ( eg. 19 * 20 / 2 = 500 / 10 = 19% ) print in the width 0.25%, how i can reverse the result to get 99.75% instead of 0.25?
For completeness will paste the complete functions:
<?php
function get_rank($rank) {
if ( $rank >= 5 ) { $rem_point = 20 - $rank; }
if ( $rank >= 20 ) { $rem_point = 50 - $rank; }
if ( $rank >= 50 ) { $rem_point = 100 - $rank; }
if ( $rank >= 100 ) { $rem_point = 500 - $rank; }
if ( $rank >= 500 ) { $rem_point = 1000 - $rank; }
if ( $rank >= 1000 ) { $rem_point = 2500 - $rank; }
if ( $rank >= 2500 ) { $rem_point = 5000 - $rank; }
return $rem_point;
}
function level($rank) {
if ( $rank <= 5 ) { $level = 1; }
elseif ( $rank <= 20 ) { $level = 2; }
elseif ( $rank <= 50 ) { $level = 3; }
elseif ( $rank <= 100 ) { $level = 4; }
elseif ( $rank <= 500 ) { $level = 5; }
elseif ( $rank <= 1000 ) { $level = 6; }
elseif ( $rank <= 2500 ) { $level = 7; }
elseif ( $rank <= 5000 ) { $level = 8; }
return $level;
}
function next_rank($level) {
if ( $level = 1 ) { $next_r = 5; }
elseif ( $level = 2 ) { $next_r = 20; }
elseif ( $level = 3 ) { $next_r = 50; }
elseif ( $level = 4 ) { $next_r = 100; }
elseif ( $level = 5 ) { $next_r = 500; }
elseif ( $level = 6 ) { $next_r = 1000; }
elseif ( $level = 7 ) { $next_r = 2500; }
elseif ( $level = 8 ) { $next_r = 5000; }
return $next_r;
}
?>
Thanks to all who can help.
Replace <?php print get_rank($rank) * next_rank($level) / 2; ?>
by <?php print_r(($rank*100)/(get_rank($rank)+$rank)); ?> should do the job you want.
For function level($rank) if i input level(10) output will be 10 as you are returning $rank without any calculation.

Limit pages numbers on PHP pagination

I have a pagination script with PHP. When page records are some hundreds, the pagination result is too big. How I can limit the page numbers/links?
Example: < 1 | 2 ... 37 | 38 | 39 | 40 | 41 | 42 ... 82 | 83 >
This is my PHP script
<?php
$ppp = 10;
$rows = mysql_num_rows($query);
$nmpages = ceil($rows/$ppp);
// if current page is not 1, draw PREVIOUS link
if ($pg > 1 && $nmpages != 0) {
echo "< ";
}
For($i = 1 ; $i <= $nmpages ; $i++) {
If($i == $pg) {
echo "<b>".$i."</b> ";
} else {
echo "".$i." ";
}
}
// if current page less than max pages, draw NEXT link
if ($pg < $nmpages && $nmpages != 0) {
echo ">";
}
?>
Do you have an ideas how I can do this with the specific PHP script that I have?
Try this :
<?php
$link = "";
$page = $_GET['pg']; // your current page
// $pages=20; // Total number of pages
$limit=5 ; // May be what you are looking for
if ($pages >=1 && $page <= $pages)
{
$counter = 1;
$link = "";
if ($page > ($limit/2))
{ $link .= "1 ... ";}
for ($x=$page; $x<=$pages;$x++)
{
if($counter < $limit)
$link .= "".$x." ";
$counter++;
}
if ($page < $pages - ($limit/2))
{ $link .= "... " . "".$pages." "; }
}
echo $link;
?>
OUTPUT :
//At page=1
1 2 3 4 ... 20
//At page=12
1 ... 12 13 14 15 ... 20
//At page=18
1 ... 18 19 20
An improvement or rather re-write based on #Makesh's code.
function get_pagination_links($current_page, $total_pages, $url)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$links .= "1";
$i = max(2, $current_page - 5);
if ($i > 2)
$links .= " ... ";
for (; $i < min($current_page + 6, $total_pages); $i++) {
$links .= "{$i}";
}
if ($i != $total_pages)
$links .= " ... ";
$links .= "{$total_pages}";
}
return $links;
}
OUTPUT:
page = 1
1 2 3 4 5 6 ... 20
page = 10
1 ... 5 6 7 8 9 10 11 12 13 14 15 ... 20
page = 19
1 ... 14 15 16 17 18 19 20
The answer for this question was basically to visit a page about Digg style pagination which includes code samples.
So that's the answer, but this question is basically a duplicate.
Try to make a page bracket for example 10 less and 10 more than the actual page, change for example the for statement for this:
For($i = $pg-10 ; $i <= $pg+10 ; $i++)
I wanted to get an array of numbers by the current page and total page. So this is what I came up with. I hope this helps others -
Caution - this work if total page is more than 10. I felt if the total
page is less than 10 then just show it in a single for loop.
function getSmartPageNumbers($currentPage, $totalPage)
{
$pageNumbers = [];
$diff = 2;
$firstChunk = [1, 2, 3];
$lastChunk = [$totalPage - 2, $totalPage - 1, $totalPage];
if ($currentPage < $totalPage) {
$loopStartAt = $currentPage - $diff;
if ($loopStartAt < 1) {
$loopStartAt = 1;
}
$loopEndAt = $loopStartAt + ($diff * 2);
if ($loopEndAt > $totalPage) {
$loopEndAt = $totalPage;
$loopStartAt = $loopEndAt - ($diff * 2);
}
if (!in_array($loopStartAt, $firstChunk)) {
foreach ($firstChunk as $i) {
$pageNumbers[] = $i;
}
$pageNumbers[] = '.';
}
for ($i = $loopStartAt; $i <= $loopEndAt; $i++) {
$pageNumbers[] = $i;
}
if (!in_array($loopEndAt, $lastChunk)) {
$pageNumbers[] = '.';
foreach ($lastChunk as $i) {
$pageNumbers[] = $i;
}
}
}
return $pageNumbers;
}
Test:
getSmartPageNumbers(8, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => .
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => .
[10] => 18
[11] => 19
[12] => 20
)
getSmartPageNumbers(1, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => .
[6] => 18
[7] => 19
[8] => 20
)

PHP extract GPS EXIF data

I would like to extract the GPS EXIF tag from pictures using php.
I'm using the exif_read_data() that returns a array of all tags + data :
GPS.GPSLatitudeRef: N
GPS.GPSLatitude:Array ( [0] => 46/1 [1] => 5403/100 [2] => 0/1 )
GPS.GPSLongitudeRef: E
GPS.GPSLongitude:Array ( [0] => 7/1 [1] => 880/100 [2] => 0/1 )
GPS.GPSAltitudeRef:
GPS.GPSAltitude: 634/1
I don't know how to interpret 46/1 5403/100 and 0/1 ? 46 might be 46° but what about the rest especially 0/1 ?
angle/1 5403/100 0/1
What is this structure about ?
How to convert them to "standard" ones (like 46°56′48″N 7°26′39″E from wikipedia) ? I would like to pass thoses coordinates to the google maps api to display the pictures positions on a map !
This is my modified version. The other ones didn't work for me. It will give you the decimal versions of the GPS coordinates.
The code to process the EXIF data:
$exif = exif_read_data($filename);
$lon = getGps($exif["GPSLongitude"], $exif['GPSLongitudeRef']);
$lat = getGps($exif["GPSLatitude"], $exif['GPSLatitudeRef']);
var_dump($lat, $lon);
Prints out in this format:
float(-33.8751666667)
float(151.207166667)
Here are the functions:
function getGps($exifCoord, $hemi) {
$degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
$minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
$seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
$flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
return $flip * ($degrees + $minutes / 60 + $seconds / 3600);
}
function gps2Num($coordPart) {
$parts = explode('/', $coordPart);
if (count($parts) <= 0)
return 0;
if (count($parts) == 1)
return $parts[0];
return floatval($parts[0]) / floatval($parts[1]);
}
This is a refactored version of Gerald Kaszuba's code (currently the most widely accepted answer). The result should be identical, but I've made several micro-optimizations and combined the two separate functions into one. In my benchmark testing, this version shaved about 5 microseconds off the runtime, which is probably negligible for most applications, but might be useful for applications which involve a large number of repeated calculations.
$exif = exif_read_data($filename);
$latitude = gps($exif["GPSLatitude"], $exif['GPSLatitudeRef']);
$longitude = gps($exif["GPSLongitude"], $exif['GPSLongitudeRef']);
function gps($coordinate, $hemisphere) {
if (is_string($coordinate)) {
$coordinate = array_map("trim", explode(",", $coordinate));
}
for ($i = 0; $i < 3; $i++) {
$part = explode('/', $coordinate[$i]);
if (count($part) == 1) {
$coordinate[$i] = $part[0];
} else if (count($part) == 2) {
$coordinate[$i] = floatval($part[0])/floatval($part[1]);
} else {
$coordinate[$i] = 0;
}
}
list($degrees, $minutes, $seconds) = $coordinate;
$sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
return $sign * ($degrees + $minutes/60 + $seconds/3600);
}
According to http://en.wikipedia.org/wiki/Geotagging, ( [0] => 46/1 [1] => 5403/100 [2] => 0/1 ) should mean 46/1 degrees, 5403/100 minutes, 0/1 seconds, i.e. 46°54.03′0″N. Normalizing the seconds gives 46°54′1.8″N.
This code below should work, as long as you don't get negative coordinates (given that you get N/S and E/W as a separate coordinate, you shouldn't ever have negative coordinates). Let me know if there is a bug (I don't have a PHP environment handy at the moment).
//Pass in GPS.GPSLatitude or GPS.GPSLongitude or something in that format
function getGps($exifCoord)
{
$degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
$minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
$seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
//normalize
$minutes += 60 * ($degrees - floor($degrees));
$degrees = floor($degrees);
$seconds += 60 * ($minutes - floor($minutes));
$minutes = floor($minutes);
//extra normalization, probably not necessary unless you get weird data
if($seconds >= 60)
{
$minutes += floor($seconds/60.0);
$seconds -= 60*floor($seconds/60.0);
}
if($minutes >= 60)
{
$degrees += floor($minutes/60.0);
$minutes -= 60*floor($minutes/60.0);
}
return array('degrees' => $degrees, 'minutes' => $minutes, 'seconds' => $seconds);
}
function gps2Num($coordPart)
{
$parts = explode('/', $coordPart);
if(count($parts) <= 0)// jic
return 0;
if(count($parts) == 1)
return $parts[0];
return floatval($parts[0]) / floatval($parts[1]);
}
I know this question has been asked a long time ago, but I came across it while searching in google and the solutions proposed here did not worked for me. So, after further searching, here is what worked for me.
I'm putting it here so that anybody who comes here through some googling, can find different approaches to solve the same problem:
function triphoto_getGPS($fileName, $assoc = false)
{
//get the EXIF
$exif = exif_read_data($fileName);
//get the Hemisphere multiplier
$LatM = 1; $LongM = 1;
if($exif["GPSLatitudeRef"] == 'S')
{
$LatM = -1;
}
if($exif["GPSLongitudeRef"] == 'W')
{
$LongM = -1;
}
//get the GPS data
$gps['LatDegree']=$exif["GPSLatitude"][0];
$gps['LatMinute']=$exif["GPSLatitude"][1];
$gps['LatgSeconds']=$exif["GPSLatitude"][2];
$gps['LongDegree']=$exif["GPSLongitude"][0];
$gps['LongMinute']=$exif["GPSLongitude"][1];
$gps['LongSeconds']=$exif["GPSLongitude"][2];
//convert strings to numbers
foreach($gps as $key => $value)
{
$pos = strpos($value, '/');
if($pos !== false)
{
$temp = explode('/',$value);
$gps[$key] = $temp[0] / $temp[1];
}
}
//calculate the decimal degree
$result['latitude'] = $LatM * ($gps['LatDegree'] + ($gps['LatMinute'] / 60) + ($gps['LatgSeconds'] / 3600));
$result['longitude'] = $LongM * ($gps['LongDegree'] + ($gps['LongMinute'] / 60) + ($gps['LongSeconds'] / 3600));
if($assoc)
{
return $result;
}
return json_encode($result);
}
This is an old question but felt it could use a more eloquent solution (OOP approach and lambda to process the fractional parts)
/**
* Example coordinate values
*
* Latitude - 49/1, 4/1, 2881/100, N
* Longitude - 121/1, 58/1, 4768/100, W
*/
protected function _toDecimal($deg, $min, $sec, $ref) {
$float = function($v) {
return (count($v = explode('/', $v)) > 1) ? $v[0] / $v[1] : $v[0];
};
$d = $float($deg) + (($float($min) / 60) + ($float($sec) / 3600));
return ($ref == 'S' || $ref == 'W') ? $d *= -1 : $d;
}
public function getCoordinates() {
$exif = #exif_read_data('image_with_exif_data.jpeg');
$coord = (isset($exif['GPSLatitude'], $exif['GPSLongitude'])) ? implode(',', array(
'latitude' => sprintf('%.6f', $this->_toDecimal($exif['GPSLatitude'][0], $exif['GPSLatitude'][1], $exif['GPSLatitude'][2], $exif['GPSLatitudeRef'])),
'longitude' => sprintf('%.6f', $this->_toDecimal($exif['GPSLongitude'][0], $exif['GPSLongitude'][1], $exif['GPSLongitude'][2], $exif['GPSLongitudeRef']))
)) : null;
}
The code I've used in the past is something like (in reality, it also checks that the data is vaguely valid):
// Latitude
$northing = -1;
if( $gpsblock['GPSLatitudeRef'] && 'N' == $gpsblock['GPSLatitudeRef'] )
{
$northing = 1;
}
$northing *= defraction( $gpsblock['GPSLatitude'][0] ) + ( defraction($gpsblock['GPSLatitude'][1] ) / 60 ) + ( defraction( $gpsblock['GPSLatitude'][2] ) / 3600 );
// Longitude
$easting = -1;
if( $gpsblock['GPSLongitudeRef'] && 'E' == $gpsblock['GPSLongitudeRef'] )
{
$easting = 1;
}
$easting *= defraction( $gpsblock['GPSLongitude'][0] ) + ( defraction( $gpsblock['GPSLongitude'][1] ) / 60 ) + ( defraction( $gpsblock['GPSLongitude'][2] ) / 3600 );
Where you also have:
function defraction( $fraction )
{
list( $nominator, $denominator ) = explode( "/", $fraction );
if( $denominator )
{
return ( $nominator / $denominator );
}
else
{
return $fraction;
}
}
To get the altitude value, you can use the following 3 lines:
$data = exif_read_data($path_to_your_photo, 0, TRUE);
$alt = explode('/', $data["GPS"]["GPSAltitude"]);
$altitude = (isset($alt[1])) ? ($alt[0] / $alt[1]) : $alt[0];
In case you need a function to read Coordinates from Imagick Exif here we go, I hope it saves you time. Tested under PHP 7.
function create_gps_imagick($coordinate, $hemi) {
$exifCoord = explode(', ', $coordinate);
$degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
$minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
$seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
$flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
return $flip * ($degrees + $minutes / 60 + $seconds / 3600);
}
function gps2Num($coordPart) {
$parts = explode('/', $coordPart);
if (count($parts) <= 0)
return 0;
if (count($parts) == 1)
return $parts[0];
return floatval($parts[0]) / floatval($parts[1]);
}
I'm using the modified version from Gerald Kaszuba but it's not accurate.
so i change the formula a bit.
from:
return $flip * ($degrees + $minutes / 60);
changed to:
return floatval($flip * ($degrees +($minutes/60)+($seconds/3600)));
It works for me.
This is a javascript port of the PHP-code posted #Gerald above. This way you can figure out the location of an image without ever uploading the image, in conjunction with libraries like dropzone.js and Javascript-Load-Image
define(function(){
function parseExif(map) {
var gps = {
lng : getGps(map.get('GPSLongitude'), data.get('GPSLongitudeRef')),
lat : getGps(map.get('GPSLatitude'), data.get('GPSLatitudeRef'))
}
return gps;
}
function getGps(exifCoord, hemi) {
var degrees = exifCoord.length > 0 ? parseFloat(gps2Num(exifCoord[0])) : 0,
minutes = exifCoord.length > 1 ? parseFloat(gps2Num(exifCoord[1])) : 0,
seconds = exifCoord.length > 2 ? parseFloat(gps2Num(exifCoord[2])) : 0,
flip = (/w|s/i.test(hemi)) ? -1 : 1;
return flip * (degrees + (minutes / 60) + (seconds / 3600));
}
function gps2Num(coordPart) {
var parts = (""+coordPart).split('/');
if (parts.length <= 0) {
return 0;
}
if (parts.length === 1) {
return parts[0];
}
return parts[0] / parts[1];
}
return {
parseExif: parseExif
};
});
short story.
First part N
Leave the grade
multiply the minutes with 60
devide the seconds with 100.
count the grades,minuts and seconds with eachother.
Second part E
Leave the grade
multiply the minutes with 60
devide the seconds with ...1000
cöunt the grades, minutes and seconds with each other
i have seen nobody mentioned this: https://pypi.python.org/pypi/LatLon/1.0.2
from fractions import Fraction
from LatLon import LatLon, Longitude, Latitude
latSigned = GPS.GPSLatitudeRef == "N" ? 1 : -1
longSigned = GPS.GPSLongitudeRef == "E" ? 1 : -1
latitudeObj = Latitude(
degree = float(Fraction(GPS.GPSLatitude[0]))*latSigned ,
minute = float(Fraction(GPS.GPSLatitude[0]))*latSigned ,
second = float(Fraction(GPS.GPSLatitude[0])*latSigned)
longitudeObj = Latitude(
degree = float(Fraction(GPS.GPSLongitude[0]))*longSigned ,
minute = float(Fraction(GPS.GPSLongitude[0]))*longSigned ,
second = float(Fraction(GPS.GPSLongitude[0])*longSigned )
Coordonates = LatLon(latitudeObj, longitudeObj )
now using the Coordonates objecct you can do what you want:
Example:
(like 46°56′48″N 7°26′39″E from wikipedia)
print Coordonates.to_string('d%°%m%′%S%″%H')
You than have to convert from ascii, and you are done:
('5\xc2\xb052\xe2\x80\xb259.88\xe2\x80\xb3N', '162\xc2\xb04\xe2\x80\xb259.88\xe2\x80\xb3W')
and than printing example:
print "Latitude:" + Latitude.to_string('d%°%m%′%S%″%H')[0].decode('utf8')
>> Latitude: 5°52′59.88″N

Check if a series of numbers adds up to a set value, and increase if necessary

I have a php script that is run once a minute. The script runs a second script, then sleeps for a given amount of time.
Right now the time is static. 20 seconds each time.
What I need is to randomize the amount of time it sleeps. It can be more than a total of 60 seconds, but not less.
So here is what i have now.
$sleep1 = rand(2,18);
$sleep2 = rand(2,18);
$sleep3 = rand(2,18);
$sleep4 = rand(2,18);
if $sleep1 + $sleep2 + $sleep3 + $sleep4 <= 60 ?
I need to add up all the $sleep variables, and then determine if the total is less than 60. If it is, I need to increase the value of one or more of the variables to make up the difference.
Why not pass that value to rand()?
sleep(rand(60, 120));
If you really need your method, just use max():
sleep(max(60, $sleep1 + $sleep2 + $sleep3 + $sleep4));
If you're not bound to $sleep1,...,$sleep4 but can use something like $sleep[ 0...n ]:
<?php
$threshold = 60;
$sleep = array();
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
if ( array_sum($sleep) < $threshold ) {
$sleep['padding'] = $threshold - array_sum($sleep);
}
foreach($sleep as $i=>$value) {
echo $i, ': ', $value, "\n";
}
edit: if you want to "scale" each of the $sleep-values so that they sum up to 60 (or more):
<?php
$threshold = 60;
$sleep = array();
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
$sleep[] = rand(2,18);
echo 'before: ', join(',', $sleep), '=', array_sum($sleep), "\n";
$sum = array_sum($sleep);
if ( $sum < $threshold ) {
$add = (int)(($threshold-$sum) / count($sleep));
$remainder = $threshold - ($sum+$add*count($sleep));
foreach( $sleep as &$v ) {
$v += (int)$add;
if ( $remainder-- > 0) {
$v += 1;
}
}
}
echo 'after: ', join(',', $sleep), '=', array_sum($sleep), "\n";
prints e.g.
before: 2,13,12,15=42
after: 7,18,16,19=60
before: 10,9,3,12=34
after: 17,16,9,18=60
before: 14,17,16,15=62
after: 14,17,16,15=62
If you can have more than, or do not need exactly 4 $sleep_ variables:
<?php
$time = 0;
$i = 1;
$array = array();
do {
$sleep{$i} = rand(2,18);
$array[] = $sleep{$i};
$time += $sleep{$i};
$i++;
} while ($time <= 60);
print_r($array);
echo $time;
?>
Gives you something like:
Array
(
[0] => 5
[1] => 2
[2] => 18
[3] => 9
[4] => 11
[5] => 10
[6] => 3
[7] => 13
)
71
Is there a reason not to just tack the extra onto $sleep1?
if ( ($sleep1 + $sleep2 + $sleep3 + $sleep4) <= 60) {
$sleep1 += 61 - ($sleep1 + $sleep2 + $sleep3 + $sleep4);
}

Pagination Problem [PHP]

So I'm doing this in PHP but it is a logic issue so I'll try to write it as generically as possible.
To start here's how this pagination script works:
for (draw first three pages links)
if (draw ellipsis (...) if there are pages between #1's pages and #3's pages)
for (draw current page and two pages on each side of it links)
if (draw elipsis (...) if there are pages between #3's pages and #5's pages)
for (draw final three pages links)
The problem is that when there are low amounts of pages (I noticed this when the page count was at 10) there should be an ellipsis but none is drawn.
Onto the code:
$page_count = 10; //in actual code this is set properly
$current_page = 1; //in actual code this is set properly
for ($i = 1;$i <= 3;$i++)
{
if ($page_count >= $i)
echo $i;
}
if ($page_count > 3 && $current_page >= 7)
echo "...";
for ($i = $current_page - 2;$i <= current_page + 2;$i++)
{
if ($i > 3 && $i < $page_count - 2)
echo $i;
}
if ($page_count > 13 && $current_page < $page_count - 5)
echo "...";
for ($i = $page_count - 2;$i <= $page_count;$i++)
{
if ($page_count > 3)
echo $i;
}
So I figure the best idea would to be to modify one of the two ellipsis if statements to include a case like this, however I've tried and am stumped.
Also please note that I condensed this code for readability sake so please don't give tips like "those for loops are ineffective because they will recalculate current_page - 2 for each iteration" because I know :)
For those whom want to see a breakdown of how this logic currently works, here is example output ( modified ) with iterating $page_count and $current_page.
http://rafb.net/p/TNa56h71.html
<?php
/**
* windowsize must be odd
*
* #param int $totalItems
* #param int $currentPage
* #param int $windowSize
* #param int $anchorSize
* #param int $itemsPerPage
* #return void
*/
function paginate($totalItems, $currentPage=1, $windowSize=3, $anchorSize=3, $itemsPerPage=10) {
$halfWindowSize = ($windowSize-1)/2;
$totalPages = ceil($totalItems / $itemsPerPage);
$elipsesCount = 0;
for ($page = 1; $page <= $totalPages; $page++) {
// do we display a link for this page or not?
if ( $page <= $anchorSize ||
$page > $totalPages - $anchorSize ||
($page >= $currentPage - $halfWindowSize &&
$page <= $currentPage + $halfWindowSize) ||
($page == $anchorSize + 1 &&
$page == $currentPage - $halfWindowSize - 1) ||
($page == $totalPages - $anchorSize &&
$page == $currentPage + $halfWindowSize + 1 ))
{
$elipsesCount = 0;
if ($page == $currentPage)
echo ">$page< ";
else
echo "[$page] ";
// if not, have we already shown the elipses?
} elseif ($elipsesCount == 0) {
echo "... ";
$elipsesCount+=1; // make sure we only show it once
}
}
echo "\n";
}
//
// Examples and output
//
paginate(1000, 1, 3, 3);
// >1< [2] [3] ... [98] [99] [100]
paginate(1000, 7, 3, 3);
// [1] [2] [3] ... [6] >7< [8] ... [98] [99] [100]
paginate(1000, 4, 3, 3);
// [1] [2] [3] >4< [5] ... [98] [99] [100]
paginate(1000, 32, 3, 3);
// [1] [2] [3] ... [31] >32< [33] ... [98] [99] [100]
paginate(1000, 42, 7, 2);
// [1] [2] ... [39] [40] [41] >42< [43] [44] [45] ... [99] [100]
This is probably an overcomplicated solution, but it works.
I've used an array here instead of just printing, which lets me "do-over" the logic.
Part of the problem occurs when "left and right of page" happens to coincide with left-and-right shoulders.
function cdotinator ( $current_page, $page_count )
{
$stepsize = 3;
$elipse = '...';
# Simple Case.
if ( $page_count <= 2 * $stepsize )
{
$out = range( 1, $page_count );
$out[$current_page - 1 ] = '*' . $current_page . '*';
return $out;
}
#Complex Case
# 1) Create All Pages
$out = range( 1, $page_count );
# 2 ) Replace "middle" pages with "." placeholder elements
for( $i = $stepsize+1 ; $i <= ( $page_count - $stepsize ) ; $i ++ )
{
$out[ $i - 1 ] = '.' ;
}
# 3.1 ) Insert the pages around the current page
for( $i = max(1,( $current_page - floor($stepsize / 2) )) ;
$i <= min( $page_count,( $current_page + floor( $stepsize/2)));
$i ++ )
{
$out[ $i - 1] = $i;
}
# 3.2 Bold Current Item
$out[ $current_page - 1 ] = '*' . $current_page . '*' ;
# 4 ) Grep out repeated '.' sequences and replace them with elipses
$out2 = array();
foreach( $out as $i => $v )
{
# end, current == peek()
end($out2);
if( current($out2) == $elipse and $v == '.' )
{
continue;
}
if( $v == '.' )
{
$out2[] = $elipse;
continue;
}
$out2[]= $v;
}
return $out2;
}
Output can be seen here: http://dpaste.com/92648/

Categories