У меня есть проект codeigniter, который в простейшей форме описывает следующие таблицы
таблица tbl_direct_fuel
+----------------+------------+------------+
| direct_fuel_id | vehicle_id | issue_date |
+----------------+------------+------------+
| 1 | 1000 | 2019-10-01 |
| 2 | 1001 | 2019-10-02 |
+----------------+------------+------------+
таблица tbl_direct_fuel_details
+-----------------------+----------------+---------+----------+
| direct_fuel_detail_id | direct_fuel_id | item_id | fuel_qty |
+-----------------------+----------------+---------+----------+
| 100 | 1 | 50 | 1.00 |
| 101 | 2 | 60 | 2.00 |
+-----------------------+----------------+---------+----------+
таблица store_item
+---------+-----------+
| item_id | item_name |
+---------+-----------+
| 50 | DOT 3 |
| 60 | DOT 4 |
| 70 | DOT 5 |
+---------+-----------+
Я пытался напечатать fuel_qty в цифрах, а также словами следующим образом, используя мое представление
1.00 One and Cents Zero Only
2.00 Two and Cents Zero Only
Модель
public function directFuelQtyById($id) {
$this->db->select('tbl_direct_fuel_details.fuel_qty');
$this->db->from('tbl_direct_fuel_details');
$this->db->join('tbl_direct_fuel', 'tbl_direct_fuel_details.direct_fuel_id=tbl_direct_fuel.direct_fuel_id', 'inner');
$this->db->where('tbl_direct_fuel.status=1 and tbl_direct_fuel.direct_fuel_id="'.$id.'"');
$this->db->order_by('tbl_direct_fuel.direct_fuel_id','DESC');
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
Контроллер
public function convert_number_to_words($number)
{
$hyphen = ' ';
$conjunction = ' and ';
$separator = ' ';
$negative = 'negative ';
$decimal = ' and Cents ';
$dictionary = array(
0 => 'Zero',
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
10 => 'Ten',
11 => 'Eleven',
12 => 'Twelve',
13 => 'Thirteen',
14 => 'Fourteen',
15 => 'Fifteen',
16 => 'Sixteen',
17 => 'Seventeen',
18 => 'Eighteen',
19 => 'Nineteen',
20 => 'Twenty',
30 => 'Thirty',
40 => 'Fourty',
50 => 'Fifty',
60 => 'Sixty',
70 => 'Seventy',
80 => 'Eighty',
90 => 'Ninety',
100 => 'Hundred',
1000 => 'Thousand',
1000000 => 'Million',
);
if (!is_numeric($number)) {
return false;
}
if ($number < 0) {
return $negative . $this->convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int)($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . $this->convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int)($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= $this->convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string)$fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
/////////////////////
public function printFuel($id){
$this->data['printData']=$this->Fuel_model->directFuelById($id);
$fuel = $this->Fuel_model->directFuelQtyById($id);
foreach($fuel as $obj)
{
$this->amount_word = $this->convert_number_to_words($obj->fuel_qty) . '<br>'." Only";
}
$this->load->view('/template/directFuel/printFuel', $this->data);
}
View (printFuel)
<html>
<head>
<title>Fuel Order</title>
</head>
<body onload="window.print()">
<body>
<div class="col-xs-12 table-responsive">
<table class="table table-bordered" style="font-size: 13px; ">
<tbody>
<?php
if (!empty($printData)) {
$offset=1; // cm
$cnt=0;
foreach ($printData as $item) {
?>
<tr>
<td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 12cm"><?=$item->fuel_qty?> Litres</p></td>
<td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 8cm"><?=$this->amount_word?></p></td>
</tr>
<?php $cnt++;
} // close your foreach HERE
?>
</tbody>
</table>
</div>
<?php
}
?>
</body>
</html>
Но при преобразовании в слова функция печатает только последний элемент для <?=$this->amount_word?>
, как показано ниже:
1.00 Two and Cents Zero Only
2.00 Two and Cents Zero Only
Как получить желаемый вывод?
1.00 One and Cents Zero Only
2.00 Two and Cents Zero Only