Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 标题中db中的行,设置为在所有页面中显示_Php_Mysql_Database_Codeigniter - Fatal编程技术网

Php 标题中db中的行,设置为在所有页面中显示

Php 标题中db中的行,设置为在所有页面中显示,php,mysql,database,codeigniter,Php,Mysql,Database,Codeigniter,在CodeIgniter中,我尝试使用DB中的代码制作标题,我的控制器代码: public function index() { $this->load->model('main_model'); $data['result'] = $this->main_model->get_tipsters(); $this->load->view('template/header_view',$data); } 和标题视图: <?php

在CodeIgniter中,我尝试使用DB中的代码制作标题,我的控制器代码:

public function index()
{
    $this->load->model('main_model');
    $data['result'] = $this->main_model->get_tipsters();
    $this->load->view('template/header_view',$data);
}
和标题视图:

<?php foreach($result->result() as $row): ?>
    <div id="tipster"><a href="<?=site_url();?>/bets/author/<?=$row->name;?>"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></a></div>
<?php endforeach; ?>
获取此错误

A PHP Error was encountered    
Severity: Notice
Message: Undefined variable: result
Filename: template/header_view.php
Line Number: 14

Fatal error: Call to a member function result() on a non-object in /home/user/domains/test.com/public_html/application/views/template/header_view.php on line 14

第14行是foreach,我已经提前复制了。

因为您的头视图文件总是期望
$result
,所以您需要为所有控制器方法提供它:

$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$data['main'] = $this->main_model->get_main_data(); //example
$this->load->view('template/header_view',$data);
$this->load->view("bets/index",$data);
$this->load->view('template/footer_view');

<>这可能会变得麻烦,所以考虑创建一个扩展了CIIOrthult../P>< p>的MyOx控制器文件,您可以在普通模型中创建一个函数。 对于头文件上的错误结果

并通过在视图文件中调用该函数直接从该函数中获取结果

common_model.php

function your_function()
{
// code for fetching data for header
/// return your result here
}
在视图文件中直接调用此函数,如下所示

$rs = $this->Common_model->your_function();

注意:公共模型默认为加载。如果禁用,则需要在视图文件中加载模型。

加载视图元素文件(标题视图)上的
CI
对象以及使用
CI
对象加载和调用模型方法的最佳方法

$CI = & get_instance()
$CI->load->model('main_model');
$result = $CI->main_model->get_tipsters();
<?php foreach($result->result() as $row): ?>
    <div id="tipster"><a href="<?=site_url();?>/bets/author/<?=$row->name;?>"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></a></div>
<?php endforeach; ?>
$CI=&get\u instance()
$CI->load->model('main_model');
$result=$CI->main_model->get_tipsters();

并从控制器中删除代码

这可能是因为您试图访问“赌注”的方式文件夹
/bets/index
-您可以尝试一个完整的系统路径,而不是
/var/user/You/httpdocs/bets/index
或一个相对路径
。/bets/index
,这取决于您执行的位置。@Fred ii-但是/bets/index/is视图文件夹:)系统知道它在哪里,然后尝试将它应用到您的
视图('template/header\u view')
等等。这肯定是一个路径问题。@Fred ii-这不是视图问题,更新了第一篇博文。你用谷歌搜索错误了吗?我在堆栈上发现了这个相关的Q&A,所以很可能是您的查询失败了。我将该代码放入索引中,但仍然得到errorDoes
$this->main_model->get_tipsters()实际返回任何结果吗?是的,当我在cotroller中查看site.com/index.php/main/时会返回此代码:
。。。如果您将此代码放在同一个主类中的另一个方法中,它会给出一个错误?在主控制器中,我将$data从db插入到我的header_视图中,该视图包含在所有页面中,但它仅适用于主控制器函数url..:s
$CI = & get_instance()
$CI->load->model('main_model');
$result = $CI->main_model->get_tipsters();
<?php foreach($result->result() as $row): ?>
    <div id="tipster"><a href="<?=site_url();?>/bets/author/<?=$row->name;?>"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></a></div>
<?php endforeach; ?>