gpt4 book ai didi

mysql - 打印 PHP 数组 HTML View 中的所有元素

转载 作者:行者123 更新时间:2023-11-29 07:17:21 24 4
gpt4 key购买 nike

我有一个 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 |
+---------+-----------+

我尝试使用我的 View 以数字和文字打印 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;
}

Controller

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);

}

查看(打印燃料)

<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

最佳答案

在您的函数 printFuel($id) 中,您将用每个新数组值覆盖 $this->amount_word。因此,您只能获得最后一个值。

因此,只需使用函数 convert_number_to_words() 在你的 Controller 中:

public function printFuel($id){
$arr=$this->Fuel_model->directFuelById($id);
foreach($arr as $key=>$obj)
{
$str=$this->convert_number_to_words($obj->fuel_qty) . '<br>'." Only";
$arr[$key]->fuel_qty_str=$str;
}
$data['printData']=$arr;
$this->load->view('/template/directFuel/printFuel', $data);

}

然后在你的 View 中输出数组:

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">
<?=$item->fuel_qty_str?>
</p>
</td>
</tr>
<?php $cnt++;
}

关于mysql - 打印 PHP 数组 HTML View 中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58479660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com