I am a newbie to Codeigniter framework, I have created a simple app that will fetch data from database and then display with condition in Views. I'm trying to display specific data from database to my View, So far, I have no luck using the code below.
我是Codeigniter框架的新手,我创建了一个简单的应用程序,它将从数据库中获取数据,然后在视图中显示条件。我正在尝试将数据库中的特定数据显示到我的View中,到目前为止,我没有运气使用下面的代码。
Model (Datamodel.php)
function getData() {
$this->db->select("name, value");
$this->db->from('settings');
$query = $this->db->get();
return $query->result_array();
}
Controller (maincontroller.php)
function index()
{
$data['metadata'] = $this->Datamodel->getData();
$this->load->view('viewdata', $data);
}
View
<h3>Site Information</h3>
<?php foreach($metadata as $data) {?>
<h4>Site Title: <?php echo $data['site_title']; ?></h4>
<h4>Site Description: <?php echo $data['site_description']; ?></h4>
<h4>Site keyword: <?php echo $data['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $data['site_year']; ?></h4>
<?php }?>
I want to display something like these on browser:
我想在浏览器上显示类似的内容:
Site Information
Site Title: My first codeigniter app
Site Description: This is sample ci application
Site keyword: simple ci app, first app
Site Year: 2015
网站标题:我的第一个codeigniter app网站描述:这是示例ci应用程序网站关键字:simple ci app,first app Site年份:2015
{ Any help & suggestion is accepted. thanks! pls see this image [ http://prntscr.com/722hsm ] for my database structure !}
{接受任何帮助和建议。谢谢!请参阅此图像[http://prntscr.com/722hsm]了解我的数据库结构!}
2 个解决方案
#1
Appreciate that you are taking initiative.
感谢您采取主动。
Just some modifications:
只是一些修改:
Model
<?php
function getData() {
$siteData = array();
$this->db->select("name, value");
$this->db->from('settings');
$query = $this->db->get();
if ($query->num_rows()) {
foreach ($query->result_array() as $row) {
// Your data is coming from multiple rows, so need to loop on it.
$siteData[$row['name']] = $row['value'];
}
}
return $siteData;
}
View:
<h3>Site Information</h3>
<?php foreach($metadata as $dataName => $dataValue) {?>
<!-- You are getting the names and values in loop, so,
use it in loop. Also, use ucwords() and str_replace() to beautify your
output. for more informtion on this, read php.net documentation.
-->
<h4><?php echo ucwords(str_replace('-', ' ', $dataName))?>: <?php echo $dataValue; ?></h4>
<?php }?>
SECOND APPROACH (WITHOUT FOREACH):
第二种方法(没有前提):
<h3>Site Information</h3>
<h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
<h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
<h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $metadata['site_year']; ?></h4>
#2
Just load your model in controller:
只需在控制器中加载模型:
function index()
{
$this->load->model('Datamodel');
$data['metadata'] = $this->Datamodel->getData();
$this->load->view('viewdata', $data);
}
try this on your views
试试看你的观点吧
<h3>Site Information</h3>
<h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
<h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
<h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $metadata['site_year']; ?></h4>
#1
Appreciate that you are taking initiative.
感谢您采取主动。
Just some modifications:
只是一些修改:
Model
<?php
function getData() {
$siteData = array();
$this->db->select("name, value");
$this->db->from('settings');
$query = $this->db->get();
if ($query->num_rows()) {
foreach ($query->result_array() as $row) {
// Your data is coming from multiple rows, so need to loop on it.
$siteData[$row['name']] = $row['value'];
}
}
return $siteData;
}
View:
<h3>Site Information</h3>
<?php foreach($metadata as $dataName => $dataValue) {?>
<!-- You are getting the names and values in loop, so,
use it in loop. Also, use ucwords() and str_replace() to beautify your
output. for more informtion on this, read php.net documentation.
-->
<h4><?php echo ucwords(str_replace('-', ' ', $dataName))?>: <?php echo $dataValue; ?></h4>
<?php }?>
SECOND APPROACH (WITHOUT FOREACH):
第二种方法(没有前提):
<h3>Site Information</h3>
<h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
<h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
<h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $metadata['site_year']; ?></h4>
#2
Just load your model in controller:
只需在控制器中加载模型:
function index()
{
$this->load->model('Datamodel');
$data['metadata'] = $this->Datamodel->getData();
$this->load->view('viewdata', $data);
}
try this on your views
试试看你的观点吧
<h3>Site Information</h3>
<h4>Site Title: <?php echo $metadata['site_title']; ?></h4>
<h4>Site Description: <?php echo $metadata['site_description']; ?></h4>
<h4>Site keyword: <?php echo $metadata['site_keyword']; ?></h4>
<h4>Site Year: <?php echo $metadata['site_year']; ?></h4>