php - 使用 codeigniter 从数据库中检索图像
问题描述
我想将存储在我的数据库中的内容提取到网页上,它可以工作,但我从下面的代码中面临的问题是,所有的行都被提取了。但相反,我想在每个不同的部分获取数据库的每一行。我的意思是,第一行数据库的内容应该显示在我的视图页面的第一行部分,第二行数据库的内容应该显示在我的第二行部分。请任何人都可以帮助我。我不知道我哪里出错了
<!--controller-->
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Contact_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
function deesha()
{
$this->load->model("Contact_model");
$data['results'] = $this->Contact_model->getAllRecords10();
$this->load->view('homeview',$data);
}
}
?>
<!--model-->
<?php
class Contact_model extends CI_Model
{
function getAllRecords10()
{
//$this->load->library("database");
$results = array();
$this->db->select('title,content');
$this->db->from('test');
//$this->db->limit(1);
$q = $this->db->get();
if($q->num_rows() > 0)
{
$results= $q->result();
}
return $results;
}
}
?>
<!--view-->
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<div class="row">
<div class="col-sm-4">
<!-- first row should be displyed-->
<table>
<?php
if( !empty($results) ) {
foreach($results as $row) {
echo '<tr>';
echo '<h3>'.$row->title.'</h3>';
echo '<p>'.$row->content.'</p>';
echo '</tr>';
}
}
?>
</table>
</div>
<div class="col-sm-4">
<!-- second row should be displyed -->
<table>
<?php
if( !empty($results) ) {
foreach($results as $row) {
echo '<tr>';
echo '<h3>'.$row->title.'</h3>';
echo '<p>'.$row->content.'</p>';
echo '</tr>';
}
}
?>
</table>
</div>
</div>
</div>
</body>
解决方案
是的,这个逻辑是正确的。但是,如果您还在 table 中存储了图像,那么您必须将 img 标签放在 foreach() 循环中。
推荐阅读
- python - Python:ValueError:无法使用长度与值不同的多索引选择索引器进行设置
- c - Realloc 返回 NULL 但为什么呢?
- spring - 具有不同服务中的变量的共同路径
- amazon-web-services - 为什么我的 OpsWorks 堆栈的一个实例会跳过我的部署?
- javascript - form.io 在 Javascript 中提交查表组件的搜索值
- reactjs - 使用时刻的 React.JS 中两个日期之间的天数差异
- reactjs - 在 PRN 应用程序中引用 PostgreSQL 查询的最佳方式
- python - Pandas groupy 将特定函数聚合/应用到特定列(np.sum、sum)
- docker - 如何将容器的内容正确绑定到主机文件夹?
- reactjs - 在 React 的选择列表选项中使用 TypeScript 类型?