Jump to content

Breaking Up Numbers In A Loop


tomtimms

Recommended Posts

I have a table something like below. I need to display these results into a table however any number that is greater than 10,000 I need it split up so the number cannot be greater than 10,000. I am not sure where to begin or how to do this as I would need it to look like the example below.

 

Table:

 

ID : Amount

1 10,400

2 8,000

3 10,300

4 5,000

5 1,000

 

Example Result/Display

 

1 5,000

1 5,400

2 8,000

3 5,000

3 5,300

4 5,000

5 1,000

 

Is this possible? If anyone could assist or give a helping hand on where to start I would be very thankful.

Link to comment
https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/
Share on other sites

Here are two ways:

<?php
$amt = 34020;
while ($amt > 10000) {
   echo "10,000<br>\n";
   $amt -= 10000;
}
echo $amt . "<br>\n";
?>

and

<?php
$amt = 38598;
if ($amt > 10000) {
   $d = intval($amt/10000);
   $r = $amt % 10000;
   for ($i = 0;$i < $d;++$i) {
      echo "10,000<br>\n";
   }
   echo $r . "<br>\n";
} else {
   echo $amt . "<br>\n";
}?>

 

Ken

I thought I had it. How can I make this so that only "NET" is looped then I can put it all into a table?

 

$sql = "SELECT company,net FROM accounts";

$result = mysql_query($result);

 

while ($array = mysql_fetch_array($result) {

 

foreach ($array AS $i => $amt) {

if ($amt > 10000) {

$d = intval($amt/10000);

$r = $amt % 10000;

for ($i = 0;$i < $d;++$i) {

echo "10,000<br>\n";

}

echo $r . "<br>\n";

} else {

echo $amt . "<br>\n";

}

}

}

If you only want one item to be looked at, look at only that item:

<?php
$sql = "SELECT company,net FROM accounts";
$result = mysql_query($result);
while ($row = mysql_fetch_assoc($result) {
   $amt = $row['net'];
   if ($amt > 10000) {
      $d = intval($amt/10000);
      $r = $amt % 10000;
      for ($i = 0;$i < $d;++$i) {
           echo $row['company'] . " 10,000<br>\n";
      }
      echo $row['company'] . ' ' . $r . "<br>\n";
   } else {
      echo $row['company'] . ' ' . $amt . "<br>\n";
   }
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.