A loop suddenly ceases to iterate? - php

I have this php code:
<?php
$numarray = trim($_GET['num']);
$i = strlen($numarray);
$result = "";
$numneedarray = array(
90 => 'ninety',
80 => 'eighty',
70 => 'seventy',
60 => 'sixty',
50 => 'fifty',
40 => 'forty',
30 => 'thirty',
20 => 'twenty',
19 => 'nineteen',
18 => 'eighteen',
17 => 'seventeen',
16 => 'sixteen',
15 => 'fifteen',
14 => 'fourteen',
13 => 'thirteen',
12 => 'twelve',
11 => 'eleven',
10 => 'ten',
9 => 'nine',
8 => 'eight',
7 => 'seven',
6 => 'six',
5 => 'five',
4 => 'four',
3 => 'three',
2 => 'two',
1 => 'one'
);
for ($v = 1; $v <= $i; $v++) {
if ($i > 10) {
exit("Has to be 10-digit or less.");
}
if ($i == 3) {
$result .= ", " . $numneedarray[$numarray[strlen($numarray) - 3]] . " hundred";
if ($numarray % 100 == 0) {
echo "Hi95";
break;
}
} elseif ($i == 2) {
if ($numarray[$v] == 1) {
$othernum = $numarray[strlen($numarray) - 1];
$othernum2 = $numarray[strlen($numarray) - 2] * 10;
$othernum3 = $othernum2 + $othernum;
if (strlen($numarray) > 2) {
$result .= " and ";
}
$result .= $numneedarray[$othernum3];
echo "Hi107";
} else {
$othernum = $numarray[strlen($numarray) - 2] * 10;
$othernum2 = $numarray[strlen($numarray) - 1];
$result .= " and " . $numneedarray[$othernum] . " " . $numneedarray[$othernum2];
echo "Hi112";
}
}
if ($i == 10) {
$digit = substr($numarray, 0, 1);
$result .= $numneedarray[$digit] . " billion";
if ($numarray % 1000000000 == 0) {
break;
}
} elseif ($i == 9) {
$number = substr($numarray, 1, 3);
$digit1 = substr($number, 0, 1);
$digit1con = $numneedarray[$digit1] . " hundred";
$digit2 = substr($number, 1, 1);
$noneed = false;
if ($digit2 != 1) {
$digit2con = $numneedarray[$digit2 * 10];
} else {
$digit23 = substr($number, 1, 2);
$digit23con = $numneedarray[$digit23];
$noneed = true;
}
$digit3 = substr($number, -1);
$digit3con = $numneedarray[$digit3];
if ($noneed == true) {
$result .= ", " . $digit1con . " and " . $digit23con . " million";
} else {
$result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " million";
}
if ($numarray % 100000000 == 0) {
echo "Hi";
break;
}
} elseif ($i == 6) {
$number = substr($numarray, 4, 3);
$digit1 = substr($number, 0, 1);
$digit1con = $numneedarray[$digit1] . " hundred";
$digit2 = substr($number, 1, 1);
$noneed = false;
if ($digit2 != 1) {
$digit2con = $numneedarray[$digit2 * 10];
} else {
$digit23 = substr($number, 1, 2);
$digit23con = $numneedarray[$digit23];
$noneed = true;
}
$digit3 = substr($number, -1);
$digit3con = $numneedarray[$digit3];
if ($noneed == true) {
$result .= ", " . $digit1con . " and " . $digit23con . " thousand";
} else {
$result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " thousand";
}
if ($numarray % 100000 == 0) {
echo "Hi89";
break;
}
}
echo $i;
$i = $i - 1;
}
if (strlen($numarray) == 1) {
echo $numneedarray[$numarray];
}
echo $result;
?>
The num value is equal to 1234567890. When I refresh the page, the value of $i only goes from 10 -> 9 -> 8 -> 7 -> 6 and then suddenly stops. Why would the loop stop running?

You try to check each digit:
for ($v = 1; $v <= $i; $v++) {
But then you decrease $i in the loop:
$i = $i - 1;
That's why you see less loops than you expected.

I hope below post will help you achieve what you're looking for:
http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

not optimal - but change
$i = strlen($numarray);
to
$i = strlen($numarray);
$count = $i;
and
for ($v = 1; $v <= $i; $v++)
to
for ($v = 1; $v <= $count; $v++)

Related

Creating two triangle using php

Someone please help me to create a two triangle patter using PHP. I'm already code but the output didn't as expected below.
expected output
My code:
function generatePattern($num) {
for ($id1 = 0; $id1 <= $num; $id1 = $id1 + 1) {
for ($id2 = $num; $id2 >= $id1; $id2 = $id2 - 1) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3 = $id3 + 1) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
for ($id1 = 0; $id1 <= $num-1; $id1 = $id1 + 1) {
echo str_repeat(' ', $num - 1);
for($id3 = $num-1; $id3 >= $id1; $id3 = $id3 - 1){
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
if ($id1 % 4 == 3) {
echo "o";
} else if ($id1 % 2 == 0) {
echo " ";
} else if ($id1 % 2 == 1) {
echo "x";
} else {
echo "x";
}
} else if ($id3 == $id1){
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
}
generatePattern(4);
And my current output like this (the bottom triangle still messed up)
output
Do the required changes for space between o and x
function generatePattern($num) {
if($num % 2 == 0)
{
$num1 = $num + 1;
}else{
$num1 = $num;
$num = $num - 1;
}
for ($id1 = 1; $id1 <= $num; $id1++) {
for ($id2 = $num; $id2 >= $id1; $id2--) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3++) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
$str = str_repeat('x o ', ceil(($num1*2)/4));
echo substr($str, 0, $num1*2);
echo "\n";
$j = $num;
for($id1 = $num; $id1 >=1; $id1 = $id1 - 2)
{
for($id2 = 2; $id2 >= 1; $id2--)
{
if($j % 2 == 0)
{
$pattern = [' ', 'x', ' ', 'o',];
}else{
$pattern = [' ', 'o', ' ', 'x',];
}
echo str_repeat(' ', ($id2%2 == 0) ? $num: $num - 1);
$design = implode('', $pattern);
do{
$design .= implode('', $pattern);
}while(strlen($design) < $id1);
echo substr($design, 0, $id1);
echo "\n";
}
$j--;
}
}
generatePattern(14);

PHP how to convert number (year) to text ex. 1996 = Ninteen Ninty six, and 2004 = Two thousand four

I have a certificate I want to convert year to text but in the given format
convertYearToText(1994){
return "Ninteen hundred ninty six";
}
convertYearToText(2004){
return "two thousand four";
}
I have a function but it gives me One Thousand Nine Hundred Ninety-Six
numberTowords(1996)
{
$num = str_replace(array(',', ' '), '' , trim($num));
if(! $num) {
return false;
}
$num = (int) $num;
$words = array();
$list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
);
$list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
$list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
);
$num_length = strlen($num);
$levels = (int) (($num_length + 2) / 3);
$max_length = $levels * 3;
$num = substr('00' . $num, -$max_length);
$num_levels = str_split($num, 3);
for ($i = 0; $i < count($num_levels); $i++) {
$levels--;
$hundreds = (int) ($num_levels[$i] / 100);
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ' ' : '');
$tens = (int) ($num_levels[$i] % 100);
$singles = '';
if ( $tens < 20 ) {
$tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );
} else {
$tens = (int)($tens / 10);
$tens = ' ' . $list2[$tens] . ' ';
$singles = (int) ($num_levels[$i] % 10);
$singles = ' ' . $list1[$singles] . ' ';
}
$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
} //end for loop
$commas = count($words);
if ($commas > 1) {
$commas = $commas - 1;
}
return implode(' ', $words);
}
I need the return result to be "nineteen hundred ninety-six"
please help
I took the liberty of rewriting and cleaning up your function, adding one crucial part: the conversion from
(a) One Thousand Nine Hundred Ninety Six to (b) Nineteen Hundred Ninety Six.
The function now accepts a optional second argument $year. If set to true, it will return (b), otherwise (a).
numberTowords(1996) will give (a)
numberTowords(1996,true) will give (b)
The comments in de code show what I've changed
function numberTowords($num,$year=false){
$num = str_replace(array(',', ' '), '' , trim($num));
if($num==='')return false;
$num = (int) $num;
$words = [];
$list=[
1=>['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
2=>['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred'],
3=>['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion','octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion','quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion']
];
//$levels = number of parts of 3
$levels = ceil(strlen($num)/3);
if($levels===0)return false;
//$parts = parts of 3, first one left-padded with zeros
$parts = str_split(substr('00' . $num, -$levels*3), 3);
$part_count=count($parts);
//THE IMPORTANT YEAR BIT
//only if year-flag = true
//only if no more then 2 parts
//only if year < 9999
// EXAMPLE: 1986
$change = false; //<< === added flag
if($year===true && $part_count===2 && $num<9999){
//get the first digit of last part (ex: 9)
$x=substr($parts[$part_count-1],0,1);
//if first digit = 0 : skip
//else: remove from last part and add to part before
// ex: 001 => 0019 and 986 => 86
if($x!=='0'){
$parts[$part_count-2]=$parts[$part_count-2].$x;
$parts[$part_count-1]=substr($parts[$part_count-1],1);
}
}
//LOOP THE PARTS
for ($i=0; $i < $part_count; $i++) {
$w=[];
//get the (int) of part
$part_num=(int)$parts[$i];
//HUNDREDS
if($part_num >= 100){
$w[]=$list[1][(int)substr($parts[$i],0,1)];
$w[]=$list[2][10];
}
//TENS and SINGLES
$remainder=$part_num%100;
if($remainder>19){
$w[]=$list[2][floor($remainder/10)];
$w[]=$list[1][$remainder%10];
}
else{
$w[]=$list[1][$remainder];
}
// << TEST FOR FLAG
if($change===true && $i===0)
$w[]=$list[2][10];
else
$w[]=$list[3][$part_count - $i -1];
$words[]=implode(' ',$w);
} //end for loop
return implode(' ', $words);
}
Could you not just use
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(1994);
This may also help if you don't want to use a plugin
function convertNumber($number)
{
list($integer, $fraction) = explode(".", (string) $number);
$output = "";
if ($integer{0} == "-")
{
$output = "negative ";
$integer = ltrim($integer, "-");
}
else if ($integer{0} == "+")
{
$output = "positive ";
$integer = ltrim($integer, "+");
}
if ($integer{0} == "0")
{
$output .= "zero";
}
else
{
$integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
$group = rtrim(chunk_split($integer, 3, " "), " ");
$groups = explode(" ", $group);
$groups2 = array();
foreach ($groups as $g)
{
$groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
}
for ($z = 0; $z < count($groups2); $z++)
{
if ($groups2[$z] != "")
{
$output .= $groups2[$z] . convertGroup(11 - $z) . (
$z < 11
&& !array_search('', array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ''
&& $groups[11]{0} == '0'
? " and "
: ", "
);
}
}
$output = rtrim($output, ", ");
}
if ($fraction > 0)
{
$output .= " point";
for ($i = 0; $i < strlen($fraction); $i++)
{
$output .= " " . convertDigit($fraction{$i});
}
}
return $output;
}
function convertGroup($index)
{
switch ($index)
{
case 11:
return " decillion";
case 10:
return " nonillion";
case 9:
return " octillion";
case 8:
return " septillion";
case 7:
return " sextillion";
case 6:
return " quintrillion";
case 5:
return " quadrillion";
case 4:
return " trillion";
case 3:
return " billion";
case 2:
return " million";
case 1:
return " thousand";
case 0:
return "";
}
}
function convertThreeDigit($digit1, $digit2, $digit3)
{
$buffer = "";
if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
{
return "";
}
if ($digit1 != "0")
{
$buffer .= convertDigit($digit1) . " hundred";
if ($digit2 != "0" || $digit3 != "0")
{
$buffer .= " and ";
}
}
if ($digit2 != "0")
{
$buffer .= convertTwoDigit($digit2, $digit3);
}
else if ($digit3 != "0")
{
$buffer .= convertDigit($digit3);
}
return $buffer;
}
function convertTwoDigit($digit1, $digit2)
{
if ($digit2 == "0")
{
switch ($digit1)
{
case "1":
return "ten";
case "2":
return "twenty";
case "3":
return "thirty";
case "4":
return "forty";
case "5":
return "fifty";
case "6":
return "sixty";
case "7":
return "seventy";
case "8":
return "eighty";
case "9":
return "ninety";
}
} else if ($digit1 == "1")
{
switch ($digit2)
{
case "1":
return "eleven";
case "2":
return "twelve";
case "3":
return "thirteen";
case "4":
return "fourteen";
case "5":
return "fifteen";
case "6":
return "sixteen";
case "7":
return "seventeen";
case "8":
return "eighteen";
case "9":
return "nineteen";
}
} else
{
$temp = convertDigit($digit2);
switch ($digit1)
{
case "2":
return "twenty-$temp";
case "3":
return "thirty-$temp";
case "4":
return "forty-$temp";
case "5":
return "fifty-$temp";
case "6":
return "sixty-$temp";
case "7":
return "seventy-$temp";
case "8":
return "eighty-$temp";
case "9":
return "ninety-$temp";
}
}
}
function convertDigit($digit)
{
switch ($digit)
{
case "0":
return "zero";
case "1":
return "one";
case "2":
return "two";
case "3":
return "three";
case "4":
return "four";
case "5":
return "five";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "eight";
case "9":
return "nine";
}
}
I will assume you want to follow grammar and spelling rules:
It is ninety and nineteen, not ninty or ninteen.
A hyphen is required for writing out numbers between 21 and 99 that are not a multiple of 10.
Also, your function deals with numbers that are way out of the practical range of years, so I would suggest a function that specifically deals with numbers in the range 1 to 9999, taking the two points above into consideration:
function convertBelow100($pair) { // Helper function
$text = ["", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty",
"forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
return $pair < 20 ? $text[$pair]
: $text[18+floor($pair/10)] . ($pair%10 ? "-" . $text[$pair%10] : "");
}
function convertYearToText($year) { // Main algorithm
$century = floor($year/100);
return trim(($century%10 ? convertBelow100($century) . " hundred "
: ($year > 999 ? convertBelow100(floor($year/1000)) . " thousand " : "")
) . convertBelow100($year%100));
}

PHP - Round Robin and 3rd person (2 players and one writer / refugee)

I have the following code
poule = ['Jason', 'Raymond', 'Rupert', 'Mike', 'Simon', 'Jeremy'];
$games = [];
$cnt_players = count($poule);
$players = $poule;
if ($cnt_players % 2 != 0)
{
array_push($players, ['name' => 'bye', 'uid' => FALSE, 'alias' => NumToChar($cnt_players + 1), TRUE]);
$cnt_players++;
}
$away = array_splice($players, $cnt_players / 2);
$home = $players;
$write = [];
for ($i = 0; $i < count($home) + count($away) - 1; $i++)
{
for ($j = 0; $j < count($home); $j++)
{
//Get the writer
$writer = $this->GetWriter($home, $away, $j, $i);
//Remove the dummy games (where one player is bye)
if ($home[$j]['name'] !== 'bye' && $away[$j]['name'] !== 'bye')
{
$games[] = [['uid' => $home[$j]['uid'], 'name' => $home[$j]['name'], 'alias' => $home[$j]['alias']], ['uid' => $away[$j]['uid'], 'name' => $away[$j]['name'], 'alias' => $away[$j]['alias']], $writer];
}
//echo 'I:' . $i . ' - J: ' . $j . ' - ' . $home[$j]['alias'] . ' : ' . $home[$j]['name'] . ' - ' . $away[$j]['alias'] . ' : ' . $away[$j]['name'] . ' ==> ' . $writer['alias'] . ' : ' . $writer['name'] . "\n\r";
$write[$writer['name']][] = $writer['name'];
}
if (count($home) + count($away) - 1 > 2)
{
array_unshift($away, current(array_splice($home, 1, 1)));
array_push($home, array_pop($away));
}
}
//print_r($write);
return $games;
--
The function GetWriter should give us the player who will note the scores for that particulair game.
private function GetWriter($home, $away, $j, $i)
{
if ($j > 0)
{
if ($j == 1)
{
$writer = (isset($home[$j + 1]['alias']) ? $home[$j + 1] : $home[$j + 1]);
}
else
{
$writer = (isset($home[$j - 1]['alias']) ? $home[$j - 1] : $home[$j + 1]);
}
//Check if writer is a bye, this is not possible
if ($writer['name'] == 'bye')
{
$writer = (isset($away[$j - 2]['alias']) ? $away[$j - 2] : $home[$j - 1]);
}
}
else
{
$writer = (isset($home[$j + 1]['alias']) ? $home[$j + 1] : $away[$j]);
if ($writer['name'] == 'bye')
{
$writer = (isset($away[$j + 1]['alias']) ? $away[$j + 1] : $home[$j]);
}
}
return $writer;
}
Above code gives me all games with each player playing once to another player (round robin). However I need to find a third player who will be the writer / refugee. Like in Darts, 1 player is the person who writes the scores on the scoreboard. I do get a writer, but it isn't nicely divided per player.
Is there an formula to get the correct player who is writer / refugee ?
Some examples
Jason vs Mike Raymond
Raymond vs Simon Rupert
Rupert vs Jeremy Mike
etc
So Jason plays against Mike and Raymond notes the scores.
Raymond plays against Simon and Rupert notes the scores etc etc
My GetWriter returns writes, but not well divided.
Eg: Player Jason writes never and Raymond writes the most scores.
I have the following solution which works well.
Because player A and player B always start, we set the key as default index 3.
Now we loop with GetCounter though all players and get the writer / refugee which is first available.
$games = [];
$cnt_players = count($poule);
$players = $poule;
$key = 3;
if ($cnt_players % 2 != 0)
{
array_push($players, ['name' => 'bye', 'uid' => FALSE, 'alias' => NumToChar($cnt_players + 1, TRUE), TRUE]);
$cnt_players++;
}
$away = array_splice($players, $cnt_players / 2);
$home = $players;
for ($i = 0; $i < count($home) + count($away) - 1; $i++)
{
for ($j = 0; $j < count($home); $j++)
{
//Remove the dummy games (where one player is bye)
if ($home[$j]['name'] != 'bye' && $away[$j]['name'] != 'bye')
{
//Get the writer
$writer = $this->GetCounter($home, $away, $j, $poule, $key);
$games[] = [['uid' => $home[$j]['uid'], 'name' => $home[$j]['name'], 'alias' => $home[$j]['alias']], ['uid' => $away[$j]['uid'], 'name' => $away[$j]['name'], 'alias' => $away[$j]['alias']], $writer['array']];
$key = $writer['key'];
}
$key++;
}
if (count($home) + count($away) - 1 > 2)
{
array_unshift($away, current(array_splice($home, 1, 1)));
array_push($home, array_pop($away));
}
}
return $games;
And the GetCounter
private function GetCounter($home, $away, $j, $writers, $key)
{
if (isset($writers[$key]['alias']))
{
$writer['array'] = $writers[$key];
$writer['key'] = $key;
if ($home[$j]['alias'] == $writer['array']['alias'] || $away[$j]['alias'] == $writer['array']['alias'])
{
$key++;
return $this->GetCounter($home, $away, $j, $writers, $key);
}
}
else {
$key = 0;
return $this->GetCounter($home, $away, $j, $writers, $key);
}
return $writer;
}

how can I sort and display dynamic data under dymanic table headers?

The project is a web calendar/scheduler using php. The idea is that a user can schedule a job that is assigned to specific machine at a specific time and others can see the job schedule and which 'asset' the job is assigned to. The scheduling works, however, I cannot tie the data to header/columns they belong to. It looks like this:
<?php
include_once("HTML/TABLE.PHP");
$data = array( 0=>array(1,'asset1','2013-07-24 10:00:00', '2013-07-24 12:00:00','red',2),
1=>array(2,'asset1','2013-07-24 12:00:00', '2013-07-24 13:00:00','green',3),
2=>array(3,'asset2','2013-07-24 11:00:00', '2013-07-24 12:00:00','blue', 4),
3=>array(4,'asset2','2013-07-24 12:00:00', '2013-07-24 14:00:00','red', 2),
4=>array(5,'asset3','2013-07-24 11:30:00', '2013-07-24 12:00:00','green', 4),
5=>array(6,'asset4','2013-07-24 12:00:00', '2013-07-24 14:00:00','blue', 3),
6=>array(7,'asset1','2013-07-24 11:45:00', '2013-07-24 13:00:00','red', 1),
7=>array(8,'asset4','2013-07-24 13:00:00', '2013-07-24 15:00:00','yellow', 5)
);
$attrs = array( 'class' => 'main',
'id' => 'main_id',
'width' => '100%',
'border' => '1',
'cellspacing' => '0',
'cellpadding' => '0');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill('n/a');
$heads = array( array('asset1','asset2','asset3','asset4'));
$i = 1;
foreach($heads as $val)
{
$table->setHeaderContents(0, $i++, $val);
unset($val);
}
$now = date('U');
$offset = ($now % 900);
$now = $now-$offset;
for ($i = 0;$i < 33; $i++)
{
$table->setHeaderContents($i,0, date('g:i',$now));
$now += 900;
}
$cellPosition = 1;
$rowCounter = 1;
for ($i=0;$i < count($data);$i++)
{
$table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4]. ' rowspan=' . $data[$i][5]);
$table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
$cellPosition++;
$rowCounter =1;
}
echo $table->display();
?>
How can I tie the information to only the column it belongs to?
I have gotten this far, but I get odd results if the first column is true:
$cellPosition = 0;
$rowCounter = 1;
for ($x=0;$x <= count($heads);$x++)
{
for ($i=0;$i < count($data);$i++)
{
if ($data[$i][1] == $table->getCellContents(0,$x))
{
$table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4]. ' rowspan=' . $data[$i][5]);
$table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
//$cellPosition++;
//echo ;
echo "<br> The current count of x = : ". $x;
echo "<br>" . $table->getCellContents(0,$x) . " Matches " . $table->getCellContents(0,$x) . " at index " . $i;
$rowCounter += $data[$i][5];
}
else
{
$rowCounter = 1;
}
}
$cellPosition++;
Ok this is working, but only if the array results are in order by asset. If not the row is reset to row 1 and the second entry overlays the previous.
$cellPosition = 0;
$rowCounter = 1;
for ($x=0;$x <= count($heads);$x++)
{
for ($i=0;$i < count($data);$i++)
{
if ($data[$i][1] == $table->getCellContents(0,$x))
{
$table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4]. ' rowspan=' . $data[$i][5]);
$table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
echo "<br> The current count of x = : ". $x;
echo "<br>" . $table->getCellContents(0,$x) . " Matches " . $data[$i][1] . " at index " . $i;
$rowCounter += $data[$i][5];
}
else
{
$rowCounter = 1;
}
}
$cellPosition++;
}
My solution is to presort the records using sql order by. I don't know how elegant that is but for the moment it is working and I can move further into this project. Any suggestions would be welcome. Thanks!

PHP: Express Number in Words [duplicate]

This question already has answers here:
Is there an easy way to convert a number to a word in PHP?
(14 answers)
Closed 6 years ago.
Is there a function that will express any given number in words?
For example:
If a number is 1432, then this function will return "One thousand four hundred and thirty two".
Use the NumberFormatter class that are in php ;)
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(1432);
That would output "one thousand four hundred thirty-two"
You can do this in many ways I am mentioning here two ways by using The NumberFormatter class as mentioned in Martindilling answer (if you have php version 5.3.0 or higher and also PECL extension 1.0.0 or higher) or by using the following custom function.
function convertNumberToWord($num = false)
{
$num = str_replace(array(',', ' '), '' , trim($num));
if(! $num) {
return false;
}
$num = (int) $num;
$words = array();
$list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
);
$list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
$list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
);
$num_length = strlen($num);
$levels = (int) (($num_length + 2) / 3);
$max_length = $levels * 3;
$num = substr('00' . $num, -$max_length);
$num_levels = str_split($num, 3);
for ($i = 0; $i < count($num_levels); $i++) {
$levels--;
$hundreds = (int) ($num_levels[$i] / 100);
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ' ' : '');
$tens = (int) ($num_levels[$i] % 100);
$singles = '';
if ( $tens < 20 ) {
$tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );
} else {
$tens = (int)($tens / 10);
$tens = ' ' . $list2[$tens] . ' ';
$singles = (int) ($num_levels[$i] % 10);
$singles = ' ' . $list1[$singles] . ' ';
}
$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
} //end for loop
$commas = count($words);
if ($commas > 1) {
$commas = $commas - 1;
}
return implode(' ', $words);
}
Yes, http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
However this is pretty dirty example. Please use NumberFormatter from intl (this will work from PHP 5.3)
$f = new NumberFormatter("in", NumberFormatter::SPELLOUT);
echo $f->format(123456);
In case of using Yii2 you can do this as simple as the following:
$sum = 100500;
echo Yii::$app->formatter->asSpellout($sum);
This prints spelled out $sum in your app's language.
Docs reference

Categories