gpt4 book ai didi

php - Codeigniter PHP MVC 困惑(新的 CI 小子)

转载 作者:搜寻专家 更新时间:2023-10-31 21:57:47 25 4
gpt4 key购买 nike

这是我第一次使用 Codeigniter,需要一些帮助。我需要按照 CI 3 教程和 Web 资源使用 JOINS 和 WHERE 语句进行 Db 查找,但是遇到了困难,我不能 100% 确定我在哪里和哪里做错了......

我的 Categorylis_model.php 如下所示: 类 Categorylist_model 扩展 CI_Model {

    public function __construct()
{
$this->load->database();
}
public function get_categoryads()
{

$this->db->select('ads.id AS id, ad_image.image AS image, ads.userid AS userid, ads.adnr AS adnr,
ads.location AS town, search_town.townFileName AS townlink, search_prov.provLabel AS province,
search_prov.subLink AS provlink,text,
adcat.name AS catname, adcat.clinkname AS clinkname, adsubcat.name AS subcatname, adsubcat.linkname AS subcatlink,
adcat.id AS catid, adsubcat.id AS subcatid, ads.r_option AS r_option, ads.R_rand AS R_rand,
adcat.catcol1 AS catcol1, adcat.catcol2 AS catcol2, search_town.townId,ads.townId');
$this->db->from('ads');
$this->db->join('adsubcat', 'adsubcat.id=ads.subcatid');
$this->db->join('adcat', 'adcat.id=ads.catid');
$this->db->join('search_town', 'search_town.townId=ads.townId');
$this->db->join('search_region', 'search_region.regionId=search_town.relRegionId');
$this->db->join('search_prov', 'search_prov.provId=search_region.relProvId');
$this->db->join('ad_image', 'ad_image.adid=ads.id');
$this->db->where("adcat.id='2' AND ads.subcatid!='843' AND scam=0 AND adactive=1 AND ads.userid=ad_image.user AND
ad_image.picorder=1 AND ad_image.aproved =1 AND ads.subcatid !='843' AND ads.subcatid !='841'");
$this->db->group_by("ads.userid");
$this->db->order_by('addate','desc');
$this->db->limit(10);
return $query->row_array();
}

}

Categories.php Controller 如下所示:

  defined('BASEPATH') OR exit('No direct script access allowed');

class Categories extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('categorylist_model');
}
public function index()
{
$this->load->helper('url');
$data = array('sitename'=>'CategoryAds', 'page_title' => 'Categories');
$this->load->view('templates/header', $data);
$this->load->view('templates/navbar');
$this->load->view('templates/breadcrumbs');
//$this->load->view('templates/category_selector');
$this->load->view('content_categories');
$this->load->view('templates/footer');


}
public function view()
{
$data['categories'] = $this->categorieslist_model->get_categoryads();
}

}

然后我的观点如下:

<?php foreach ($categories as $ad_item): ?>
<?php echo $ad_item['text'] ?>
<?php endforeach ?>

查看页面时出现以下错误。

        A PHP Error was encountered
Severity: Notice
Message: Undefined variable: categories

Filename: views/content_categories.php
Line Number: 114

Backtrace:
File: /usr/www/XXXXXXX/ci/application/views/content_categories.php
Line: 114
Function: _error_handler

File: /usr/www/XXXXXXX/ci/application/controllers/Categories.php
Line: 18
Function: view

File: /usr/www/XXXXXXX/ci/index.php
Line: 292
Function: require_once

最佳答案

您的 Controller 将如下所示 -

public function index() {
$this->load->helper('url');
$data['metadata'] = array('sitename'=>'CategoryAds', 'page_title' => 'Categories');
//directly
$data['categories'] = $this->categorylist_model->get_categoryads();

$this->load->view('templates/header', $data);
$this->load->view('templates/navbar');
$this->load->view('templates/breadcrumbs');
$this->load->view('content_categories', $data);
$this->load->view('templates/footer');


}
/** No need I think
public function view() {
$data['categories'] = $this->categorieslist_model->get_categoryads();
} **/

您的 View 将是 - content_categories.php

<?php foreach ($categories as $ad_item): ?>
<?php echo $ad_item['text'] ?>
<?php endforeach ?>

我使用 Codeigniter 的过程如下-

模型目录下的Cat_model.php

public function get_category() {
// active records code with return
}

Controller 目录下的CatCon.php

function __construct() {
parent::__construct();
$this->load->model('cat_model');
}
function index() {
$data['categories'] = $this->cat_model->get_category();
// pass the data variable to view
$this->load->view('content_categories', $data);
}

View 目录下的content_categories.php

print_r($categories);

关于php - Codeigniter PHP MVC 困惑(新的 CI 小子),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31795546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com