tomtimms Posted May 27, 2010 Share Posted May 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/ Share on other sites More sharing options...
salathe Posted May 27, 2010 Share Posted May 27, 2010 Why are (or what is the logic for) the large values split to 5,000 + 5500 and 5,000 + 5,300 instead of taking up the maximum value 10,000 + 500 and 10,000 + 300? Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064095 Share on other sites More sharing options...
Zane Posted May 27, 2010 Share Posted May 27, 2010 Yeah... what's wrong with also dividing them by 2. Getting 5200 for 10,400.. etc Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064097 Share on other sites More sharing options...
tomtimms Posted May 27, 2010 Author Share Posted May 27, 2010 It is for payments, no payment can be more than 10k. So if I have a payment going out that is more 10,000 it has to be broken down to numbers less than 10,000 so they can be sent. So if I had a payment of 25,000 I would need 10,000 - 10,000 - 5,000. Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064111 Share on other sites More sharing options...
kenrbnsn Posted May 27, 2010 Share Posted May 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064116 Share on other sites More sharing options...
tomtimms Posted May 27, 2010 Author Share Posted May 27, 2010 Thanks KEN the second one worked, however how could I use this when I am using mysql_fetch_array to get values from my database? Would I add a while loop before the foreach loop to get all the values? Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064133 Share on other sites More sharing options...
Alex Posted May 27, 2010 Share Posted May 27, 2010 $amt = 24578; $payments = $amt > 10000 ? array_fill(0, floor($amt / 10000), 10000) : array(); $payments[] = $amt % 10000; print_r($payments); Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064135 Share on other sites More sharing options...
tomtimms Posted May 27, 2010 Author Share Posted May 27, 2010 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"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064222 Share on other sites More sharing options...
kenrbnsn Posted May 27, 2010 Share Posted May 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203078-breaking-up-numbers-in-a-loop/#findComment-1064272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.