我是一个新手,他一直在寻找有关在HTML中插入PHP foreach输出的解决方案,
有我的查询代码:
import React, { Component } from "react";
import { connect } from "react-redux";
import { createUser } from "../store/actions/userActions";
class UserForm extends Component {
constructor(props) {
super(props);
this.state = { form: [], alert: false, alertData: {} };
this.submitMessage = this.submitMessage.bind(this);
}
submitMessage = e => {
e.preventDefault();
const params = {
name: this.inputName.value,
email: this.inputEmail.value,
city: this.inputCity.value,
age: this.inputAge.value
};
this.setState({ form: params });
console.log(params);
console.log("-----------");
console.log(this.state);
this.props.createUser(this.state);
};
render() {
return (
<div className="container">
<div
className="row"
>
User
</div>
<div className="container" style={{ padding: `10px 0px` }} />
<div className="row">
<div className="col-sm-4">
<h2>Contact Form</h2>
<form onSubmit={this.submitMessage} ref="contactForm">
<div className="form-group">
<label htmlFor="name">Name</label>
<input
type="text"
className="form-control"
id="name"
placeholder="Name"
ref={name => (this.inputName = name)}
/>
</div>
<div className="form-group">
<label htmlFor="emai1">Email</label>
<input
type="email"
className="form-control"
id="email"
placeholder="Email"
ref={email => (this.inputEmail = email)}
/>
</div>
<div className="form-group">
<label htmlFor="city">City</label>
<select
className="form-control"
id="city"
ref={city => (this.inputCity = city)}
>
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="Japan">Japan</option>
<option value="Germany">Germany</option>
</select>
</div>
<div className="form-group">
<label htmlFor="age">Age</label>
<input
type="number"
className="form-control"
id="age"
placeholder="Age"
ref={age => (this.inputAge = age)}
/>
</div>
<button type="submit" className="btn btn-primary">
Send
</button>
</form>
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => {
return {};
};
const mapDispatchToProps = dispatch => {
return {
createUser: user => dispatch(createUser)
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(UserForm);
并且有我的html代码:
$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();
foreach ($coaTitle as $coa) {
echo '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coaTitle[0]['COA_CODE'].'</span></td><br>';
}
问题是如何在HTML中插入foreach?
答案 0 :(得分:1)
在您的foreach
中,而不是echo
将值构建为字符串
(为每个部分添加.=
),然后可以将其插入到较大的HTML中...
$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();
$htmlTable = "";
foreach ($coaTitle as $coa) {
$htmlTable .= '<tr>';
$htmlTable .= '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td><br>';
// Continue adding fields using `$coa['FIELD NAME']` and $htmlTable .= ...
$htmlTable .= '</tr>';
}
$detailJournal = '<!DOCTYPE html>
<html>
<table>
<tr>
<table border = 0 cellspacing = 0 cellpadding = 0 align = center>'
.$htmlTable.'<br><br>
</table>
</html>';
return $detailJournal;
}
您只需要使用该行
$htmlTable .= '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td><br>';
作为要添加到其他列中的模板。
答案 1 :(得分:0)
尝试一下:
$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();
foreach ($coaTitle as $coa) {
echo '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa[0]['COA_CODE'].'</span></td><br>';
}
在foreach($ coaTitle作为$ coa)中,您必须使用 $ coa 代替$ coaTitle
答案 2 :(得分:0)
如果您想在html文件中添加php代码,则可以通过添加php标签直接添加,并以 .php 扩展名保存文件
$detailJournal = '<!DOCTYPE html>
<html>
<table>
<tr>
<table border = 0 cellspacing = 0 cellpadding = 0 align = center>
<?php
foreach ($coaTitle as $coa) {
?>
<tr>
<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td>
<td width=170><span style=font-size:10pt; line-height:19px; text-align:center>'.$coa[0]['COA_TITLE'].'</span></td>
<td width=55><span style=font-size:10pt; line-height:19px; text-align:center>IDR</span></td>
<td width=81><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['ORIG'].'</span></td>
<td width=89><span style=font-size:10pt; line-height:19px; text-align:right>0</span></td>
<td width=89><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['SUM'].'</span></td>
</tr>
<?php
}
?>
</table>
</tr>
</table>
</html>