Php 如何使用codeigniter从数据库查询中保存字符串
我试图将数据保存到一个字符串中,以便以后使用 这是迄今为止我所掌握的代码:Php 如何使用codeigniter从数据库查询中保存字符串,php,codeigniter,Php,Codeigniter,我试图将数据保存到一个字符串中,以便以后使用 这是迄今为止我所掌握的代码: $phone_numbers = $this->phone_model->get_all()->result(); $string = ''; foreach($phone_numbers as $cel){ $string .= 'Phone #: '; $string .= $cel->cel_num; $string .= '<br/>'; } $dat
$phone_numbers = $this->phone_model->get_all()->result();
$string = '';
foreach($phone_numbers as $cel){
$string .= 'Phone #: ';
$string .= $cel->cel_num;
$string .= '<br/>';
}
$data['string'] = $string;
可能有更多或更少
然后在我看来,我只是将其作为echo$string
来回显数据
$html .='
<br/>
<br/>
<br/>
<br/>
<br/>
<table width="90%" align="center" style="border-bottom: 1px solid #000;">
<tr>
<th style="font-size:14px;" align="center">'.$contact->name.'<br />
<div style="font-size:10px">
'.$contact->addr.'<br />
////////// HERE IS WHERE I WANT TO ECHO THE NUMBERS/////////
'.$string.'
</div>
</th>
</tr>
<tr>
<th style="font-size:12px; font-weight:bold;" align="left">
PHONE BOOK</th></tr>
</table>';
$html.='
“.$contact->name”。
“.$contact->addr.”
//////////我想在这里重复这些数字/////////
“.$string。”
电话簿
';
您有一个数组,这就是为什么需要foreach循环。这是您需要的代码。我会把它改成更简单的
控制器功能
function display_numbers()
{
$data['phone_numbers'] = $this->phone_model->get_all();
//I made this into an array and removed the result,
//we will be getting that from the database.
//I removed the String part for it is maybe not necessary, I'll echo it in the view
$this->load->view('my_view', $data);
}
function get_all()
{
$this->db->select('*');
$this->db->from('your_table');
$query = $this->db->get();
return $query->result();
//This way we return this result automatically to the call
}
模型功能
function display_numbers()
{
$data['phone_numbers'] = $this->phone_model->get_all();
//I made this into an array and removed the result,
//we will be getting that from the database.
//I removed the String part for it is maybe not necessary, I'll echo it in the view
$this->load->view('my_view', $data);
}
function get_all()
{
$this->db->select('*');
$this->db->from('your_table');
$query = $this->db->get();
return $query->result();
//This way we return this result automatically to the call
}
查看
重要提示,不要将php与html混合使用,或者如果您不能过度使用的话
“.$contact->name”。
-
电话簿
';
祝你好运 更改您的:
$data["string"] = $string
对此
$data["string"][] = $string
什么不起作用?当我尝试在我的视图中回显它们时,进行回显的代码在哪里?@pmahome代码已更新!!:)这其中有没有被重复,或者仅仅是
$string
不起作用?虽然这个代码片段可以解决这个问题,但确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满您的代码,因为这会降低代码和解释的可读性!