For example :
$monthly_bill = 700
$paid_amount = 2100
Now I want to use a php loop which will print me 700, 700 and 400 to each line. How can I do this ?
I am doing following code but not working :
<?php
$paid_amount = 1800;
$monthly_bill = 700;
$counting_month = round($paid_amount / $monthly_bill);
for ($x=1; $x<=$counting_month; $x++) {
echo $insert = $paid_amount - $monthly_bill;
$paid_amount = $insert;
echo '<br/>';
}
Don't use for loop. Those run a fixed number of times, and your loop would keep running until well after the money runs out.
Use a do instead, and have its termination clause be when the money runs out:
$remaining = 1800;
$monthly = 700;
do {
echo $monthly;
$remaining -= $monthly;
if ($remaining < $monthly) {
$monthly = $remaining;
}
} while ($remaining > 0);
<?php
$amount = 1800;
$monthlybill = 700;
for ($i=0; $i<$amount/$monthlybill; $i++) {
echo "paid for " . ($i+1) . " month =" . $monthlybill . "<br>\n";
$amount -= $monthlybill;
}
if ($amount>0) {
echo "paid for " . ($i+1) . " month =" . $amount;
}
Related
I have a variable that adds and returns numbers actually this numbers increases with time.
Now I want that if the numbers get to say 2000000 (2 millions) it should remove all the zeros and return 2m.
Any ideas how I can go about this ?
you can use the following function
function millionType($value){
return ($value / 1000000)."m";
}
echo millionType(1500000); // 1.5m
echo "<br/>";
echo millionType(2000000); // 2m
echo "<br/>";
echo millionType(2500000); // 2.5m
echo "<br/>";
echo millionType(3000000); // 3m
Edit: you can use this, you can change it to your desire type.
function millionType($value){
$million = (int)($value / 1000000);
$surplus = ($value % 1000000);
$myType = "";
if($million > 0)
$myType .= $million."m ";
$thousands = (int)($surplus / 1000);
if($thousands > 0)
$myType .= $thousands."k ";
$lastPart = ($surplus % 1000);
if($lastPart > 0)
$myType .= "and ".$lastPart;
echo $myType;
}
echo millionType(1500001); // 1m 500k and 1
echo "<br />";
echo millionType(1000001); // 1m and 1
echo "<br />";
echo millionType(10000540); // 10m and 540
Question is a duplicate of Shorten long numbers to K/M/B?
Neverthless here is my answer:
function shorten($number){
$suffix = ["", "K", "M", "B"];
$percision = 1;
for($i = 0; $i < count($suffix); $i++){
$divide = $number / pow(1000, $i);
if($divide < 1000){
return round($divide, $percision).$suffix[$i];
break;
}
}
}
echo shorten(1000);
I have a problem and that is I want to create a link on a website like people can click the link to show certain products only depending on percentage. like for example, i have a column in my database with discount percentage and it will show min discount and max discount. assuming we have min and max discount. $min=12 and $max=94; and I want to put them in links to show only products with certain discounts only like filtering. below is the example of the link.
<a href="#">12% to 20%</a
21% to 30%
31% to 40% and so on until it reaches
81% to 90% and the last will be
91% to 94%
smallest and largest numbers will be coming from a column from database and they can change frequently. i came up with solution and its working fine but my code is too long and its like I took to many steps which could be done in few lines of code. I have pasted my working code below but I am sure this can be reduced to few lines of code.
$catsql25 = "SELECT MAX(down_percentage) as largest FROM hot_deals";
$catquery25 = mysqli_query($conn, $catsql25);
while ($row25 = mysqli_fetch_array($catquery25, MYSQLI_ASSOC)){
$largest_number = $row25['largest'];
}
$catsql26 = "SELECT MIN(down_percentage) as smallest FROM hot_deals";
$catquery26 = mysqli_query($conn, $catsql26);
while ($row26 = mysqli_fetch_array($catquery26, MYSQLI_ASSOC)){
$smallest_number = $row26['smallest'];
}
$array_tens = array(10,20,30,40,50,60,70,80,90,100);
foreach ($array_tens as $value){
if(($value - $smallest_number <= 10) && ($value - $smallest_number > 0)){
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++){
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
}
foreach ($array_tens as $value2){
if(($largest_number - $value2 < 10) && ($largest_number - $value2 > 0)){
$lsst = $value2 + 1;
if($lsst != $largest_number){
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
}
elseif($lsst == $largest_number){
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
I know its all mess but..
Thanks.
First thing you could do is only one SQL Query :
$catsql = "SELECT MAX(down_percentage) as largest, MIN(down_percentage) as smallest FROM hot_deals";
And then you'll need only one loop :
$catquery = mysqli_query($conn, $catsql);
while ($row = mysqli_fetch_array($catquery, MYSQLI_ASSOC)){
$largest_number = $row['largest'];
$smallest_number = $row['smalest'];
}
After that, you could make only one foreach loop. The two "if" conditions could be in the same loop :
foreach ($array_tens as $value) {
if (($value - $smallest_number <= 10) && ($value - $smallest_number > 0)) {
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++) {
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
if (($largest_number - $value < 10) && ($largest_number - $value > 0)) {
$lsst = $value + 1;
if ($lsst != $largest_number) {
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
} elseif ($lsst == $largest_number) {
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
To make it more readable, you could also comment your code to know what do what.
This and a good indentation and you're right.
Hope it helps.
i want to display 10 instances of an image in a 5x2 square. I coded this in PHP/HTML/CSS:
for ($i = 0; $i <= 9; $i++) {
if ($i >= 5) {
$top_position=780;
echo "1". "<br />";
}else {
$top_position = 430;
echo "2". "<br />";
}
if ($i >= 5) {
$u = 20 + 250 * $i - 250 * 5;
}
else {
$u = 20 + 250 * $i;
}
echo "<style> " . ".background" . $u . "{" . "position: absolute;
left: " . $u . "px;" . "top: " . $top_position . "px;" . "</style>";
echo "<img src=http://localhost/Summoner's%20Index/images/scheme.png class=background" . $u . ">";}
I get an 5x1 in the second row, and as text i get: 1111122222, so it seems as if the programm would work properly. Why do i just get 5images and not 10?
Check out the code below:
for ($i = 1; $i <= 10; $i++) {
echo "<img src=http://localhost/Summoner's%20Index/images/scheme.png class=background" . $u . ">";
if ($i == 5){
echo "<br>";
}
}
A 500 error has occurred - executing long loop with PHP.
I am currently working on a scheduling system to schedule nurses on a hospital ward, I am using a genetic algorithm to carry this out.
so I randomly allocate each nurses to a shift.
and then work out how fit they are for the shift.
I then kill off any allocation which do not meet my fitness level.
I then randomly allocate a new timetable.
Assess that for fitness
kill off any allocation which do not meet my fitness level.
Merge the two timetables, keeping the fitness allocations
I loop through generating random timetable, accessing its fitness and merging the timetables
This works fine while looping through 30 - 100 times
Once I go past the 100 mark it sometimes fails - a 500 error has occurred
This always occurs when it takes over 2:30mins to complete the script
So I'm making the assumption at some point my server times out for taking too long?
I have added <?php set_time_limit(3600);
This is at the top of my file, not inside the constructor, or the class. Is it in the right place?
it still times out at 2 and a half minute,
Here is my code, the loop is the 200 loop
Still need to refractor my code so don't be too judgmental
<?php
set_time_limit(3600);
* Description of scheduler
*
* #author Dela
*/
include ("randomTtAllocation.php");
include ("fittness.php");
class scheduler {
private $randomTimetable;
private $timetable;
private $weight;
private $newTimetable ;
private $newWeight;
function __construct($labs,$students) {
echo "helloworld";
// create random timetable and print it
$r = new randomTtAllocation();
$this->randomTimetable = $r->__randomAllocation($labs, $students);
//echo "<br>" . " ............initial Time Table............." . "<br>" ;
echo "<br>";
echo "<br>";
echo "<br>";
$this->__printTt($this->randomTimetable, $this->randomTimetable);
// work out fittness for the timetable, return fit results and print
$fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());
$this->newWeight = $fit->__getnewWeight();
$this->newTimetable = $fit->__getnewTimetable();;
//echo "<br>" . " ............newTimetable.........newWeight...." . "<br>" ;
//$this->__printTt($this->newTimetable, $this->newWeight );
// sort
$this->__sortTtByWeight();
$this->timetable = $this->newTimetable;
$this->weight = $this->newWeight;
for ($i = 0; $i < 200; $i++) {
// create second time table
$this->randomTimetable = $r->__randomAllocation($labs, $students);
// echo "<br>" . " ............initial Time Table............." . "<br>" ;
// $this->__printTt($this->randomTimetable, $this->randomTimetable);
// work out fittness for the timetable, return fit results and print
$fit = new fittness( $this->randomTimetable, $labs, $students , $r->__getNumOfSessions());
$this->newWeight = $fit->__getnewWeight();
$this->newTimetable = $fit->__getnewTimetable();
//echo "<br>" . " ............tempTimetable.........tempWeight...." . "<br>" ;
//$this->__printTt($this->newTimetable, $this->timetable );
$this->__sortTtByWeight();
// merge timetables
echo "<br>" . " ......old...........new." . "<br>" ;
$this->__printTt($this->timetable,$this->newTimetable );
$this->__mergeTimetables($this->newTimetable, $this->newWeight );
//echo "<br>" . " ........... mergedTimetable.........newWeight...." . "<br>" ;
//$this->__printTt($this->timetable,$this->weight );
// fittness of new timetable
$fit = new fittness( $this->timetable, $labs, $students , $r->__getNumOfSessions());
echo "<br>" . " ......merged.......kulled" . "<br>" ;
$this->__printTt($this->timetable,$fit->__getnewTimetable() );
$this->weight = $fit->__getnewWeight();
$this->timetable = $fit->__getnewTimetable();
$c[$i] = $this->__countSlotsAllocated();
echo "<br> ". $i;
}
print_r($c);
}
// sorts the re allocated time table by weight
function __sortTtByWeight() {
// for each slot
foreach($this->newTimetable as $l => $i_value) {
$size = 0;
// see how many sessions they are taking
while ($this->newTimetable[$l][$size] != "null") {
if ($this->newTimetable[$l][$size] == "0") {
break;
}
$size++;
}
for ($i = 1; $i < $size; $i++) {
$key = $this->newWeight[$l][$i];
$key1 = $this->newTimetable[$l][$i];
$k = $i - 1;
while ($k >= 0 && $this->newWeight[$l][$k] < $key) {
$this->newWeight[$l][$k + 1] = $this->newWeight[$l][$k];
$this->newTimetable[$l][$k + 1] = $this->newTimetable[$l][$k];
$k--;
}
$this->newTimetable[$l][$k + 1] = $key1;
$this->newWeight[$l][$k + 1] = $key;
}
}
}
function __countSlotsAllocated() {
$count = 0;
foreach($this->timetable as $i => $x_value) {
$j = 0;
while (($this->timetable[$i][$j] != "null") && ($this->timetable[$i][$j] != "0")){
$count++;
$j++;
}
}
echo "count " . $count;
return $count;
}
function __mergeTimetables($tempTimeTable,$tempWeight) {
// for each session
foreach($this->newTimetable as $i => $i_value) {
$j = 0;
$k = 0;
// while there are still students
while (($tempTimeTable[$i][$j] != "null") && ($tempTimeTable[$i][$j] != "0")) {
//echo $tempTimeTable[$i][$j];
// System.out.println(timeTable[i][k]);
// see if there is a free gap
while ($this->timetable[$i][$k] != "null") {
// if student is already taking that session
if ($tempTimeTable[$i][$j] == $this->timetable[$i][$k]) {
break;
}
if ($this->timetable[$i][$k] == "0") {
$this->timetable[$i][$k] = $tempTimeTable[$i][$j];
$this->weight[$i][$k] = $tempWeight[$i][$j];
break;
}
$k++;
}
if ($this->timetable[$i][$k] == "null") {
if ($tempWeight[$i][$j] < $this->weight[$i][0]) {
$this->timetable[$i][0] = $tempTimeTable[$i][$j];
$this->weight[$i][0] = $tempWeight[$i][$j];
}
}
$j++;
}
}
}
function __returnTimetable() {
return $this->timetable;
}
function __printTt($timetable, $weight) {
foreach($timetable as $x => $x_value) {
for ($i = 0; $i < 5; $i++) {
echo $timetable[$x][$i] . " ";
}
echo " . . . . .";
for ($i = 0; $i < 5; $i++) {
echo $weight[$x][$i] . " ";
}
echo "<br>";
}
}
}
Normally you shouldn't do all this work within the http request. Instead you should start a background task and have the web page show progress on the job.
The php configuration has a time limit per request which you can adjust, but it's not expected by users to take so long.
I want to display an array (which is already has been sorted) and want to show all rows of the array including some group totals (in this case the totals of one order and the totals per month. Here is the code which I have so far. When I "strip" the code and only do the totals on either order or month is works like a charm, together I don't see the solution...
Here's the code I have so far:
//Put test data in an array [0] = Order, [1] = Month, [2] = Pieces
$data = array(
array("1614-0082","JAN",10),
array("1614-0082","JAN",12),
array("1614-0082","JAN",20),
array("1614-0086","JAN",81),
array("1614-0064","FEB",10),
array("1614-0064","FEB",11),
array("1614-0101","MRT",19),
array("1614-0004","OCT",13),
array("1614-0004","OCT",12),
array("1614-0023","OCT",13),
array("1614-0025","DEC",15),
array("1614-0028","DEC",15),
);
$TotalPcsO = 0; //Total per order
$TotalPcsM = 0; //Total per month
$TotalPcsG = 0; //Grand total
$j = 0;
$i = 0;
$PrevOrder = $data[0][0];
$PrevMonth = $data[0][1];
for($k = 0; $k <= sizeof($data); $k++) {
while ($PrevMonth === $data[$i][1]) {
while ($PrevOrder === $data[$j][0]) {
echo $data[$j][0].' '.$data[$j][1].' '.$data[$j][2];
echo "<br>";
$TotalPcsO += $data[$j][2];
$PrevOrder = $data[$j][0];
$j++;
}
$i = $j;
//Order Totals
echo 'Total of order '.$PrevOrder.': '.$TotalPcsO;
echo "<br>";
echo "<br>";
$TotalPcsM += $TotalPcsO;
$TotalPcsO = 0;
$PrevOrder = $data[$i][0];
$i++;
}
$k = $i;
echo 'Total of month '.$PrevMonth.': '.$TotalPcsM;
echo "<br>";
echo "<br>";
$TotalPcsG += $TotalPcsM;
$TotalPcsM = 0;
$PrevMonth = $data[$k][1];
}
//Grand Totals
echo 'Grand total '.$TotalPcsG;
Try this... it's not the way I would have done it, but based on your code, at least it should work...
$TotalPcsO = 0; //Total per order
$TotalPcsM = 0; //Total per month
$TotalPcsG = 0; //Grand total
$PrevOrder = $data[0][0];
$PrevMonth = $data[0][1];
foreach($data as $row) {
$TotalPcsG+=$row[2];
if($row[0]==$PrevOrder) $TotalPcsO += $row[2];
else {
echo "Total of order $PrevOrder: $TotalPcsO<br /><br />";
$PrevOrder=$row[0];
$TotalPcsO=$row[2];
}
if($row[1]==$PrevMonth) $TotalPcsM += $row[2];
else {
echo "Total of month $PrevOrder: $TotalPcsM<br /><br />";
$PrevMonth=$row[1];
$TotalPcsM=$row[2];
}
echo $row[0].' '.$row[1].' '.$row[2].'<br />';
}
echo "Total of order $PrevOrder: $TotalPcsO<br /><br />";
echo "Total of month $PrevOrder: $TotalPcsM<br /><br />";
echo "Grand total: $TotalPcsG";