CodeIgniter:未定义的属性:stdClass :: $ CategoryID

时间:2016-03-17 11:31:33

标签: php mysql codeigniter

我在这里内部加入两个数据库,当单独使用单独的视图时它们工作正常,数据库也是如此,我认为这是内连接,但是将查询直接放入phpmyadmin并替换$带有数字的categoryID可以使用。

某些行加载,例如图像,但连接的变量未定义,导致变量未定义的原因是什么?

型号:

function DisplayBySpecificCategory($categoryID) 
{
    $query = $this->db->query("SELECT * FROM CIUploads
                               INNER JOIN CIUsers ON CIUploads.UserID = CIUsers.UserID
                               WHERE CIUploads.UploadCategoryID = $categoryID 
                               ORDER BY CIUploads.UploadTime DESC");
    return $query->result();
}

控制器:

function SpecificCategory()
    {
        $categoryID=$this->uri->segment(3);

        $this->load->model('Previews_model');
        $this->load->model('Category_model');      
        $this->load->view('../includes/header.php'); 
        $data['category'] = $this->Category_model->GenerateCategoryHeader($categoryID);
        $data['allwork'] = $this->Previews_model->DisplayBySpecificCategory($categoryID);
        $this->load->view('headers/Specific_category_header', $data);
        $this->load->view('previews/Previews_view', $data); 
        $this->load->view('../includes/footer.php');
    }

标题视图:

<?php foreach ($category as $value2){ ?> 
    <header style="background-color:#<?php echo $value2->CategoryColor ?>">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <div class="page-intro">
                        <h1 class="inverted text-center"><?php echo $value2->CategoryTitle ?></h1>
                        <p class="inverted text-center"><?php echo $value2->CategoryDescription ?></p>
                    </div>
                </div>
            </div>
        </div>
    </header>
<?php } ?>

预览视图:                                       

                <?php
                    if($value->UploadCategoryID == 2){
                        echo '<video muted class="video-responsive fit-centered">'; 
                            echo '<source src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" type="video/mp4">Your browser does not support the video tag.'; 
                        echo '</video>'; 
                    }

                     else if($value->UploadCategoryID == 3){ 
                        echo '<iframe class="fit-centered" src="http://'.$value->UploadedAt.'" scrolling="no"></iframe>';
                     }

                    else{
                        echo '<img class="img-responsive fit-centered" src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" alt="'.$value->UploadTitle.'">';
                    } ?>

                <div class="overlay">
                    <p>
                        <a href="<?php echo site_url("/Work_controller/ViewWork/".$value->UploadID) ?>" class="inverted"><i class="fa fa-eye fa-3x ease-color"></i></a>
                    </p>
                </div>
            </div>

            <div class="hovereffect-information no-bounding">
                <span class="category-tab pull-right text-center inverted" style="background-color:#<?php echo $value->CategoryColor ?>"><i class="fa <?php echo $value->CategoryIcon ?>"></i></span>
                <p class="pull-left no-bounding text-uppercase preview-info"><?php echo $value->UploadTitle ?></p>
                <a href="<?php echo site_url("/Profile_controller/ViewProfile/".$value->UserID)?>"><img class="img-responsive small-profile-img pull-right hover-dim fit-centered" src="<?php echo base_url("/public/uploads/profile-images/")."/".$value->ProfileImg ?>" alt="<?php echo $value->Username ?>"/></a>
                <p class="pull-left no-bounding text-uppercase preview-info">
                    <a href="<?php echo site_url("/Category_controller/ViewCategory/".$value->CategoryID) ?>"></a> 
                    <span class="preview-span"> <i class="fa fa-eye"></i> <?php echo $value->UploadViews ?> </span> |
                    <span class="preview-span"> <i class="fa fa-heart"></i> <?php echo $value->UploadLikes ?> </span> |
                    <span class="preview-span"> <i class="fa fa-comments"></i> <?php echo $value->UploadComments ?> </span> 
                </p>
            </div>
        </div>
    <?php } ?> 

错误:

  

严重性:注意

     

消息:未定义属性:stdClass :: $ CategoryIcon

     

文件名:previews / Previews_view.php

     

行号:31

     

回溯:

     

文件:   /web/stud/u1263522/cmw/application/views/previews/Previews_view.php   行:31功能:_error_handler

     

文件:   /web/stud/u1263522/cmw/application/controllers/Views_controller.php   行:64功能:视图

     

文件:/web/stud/u1263522/cmw/index.php行:292功能:   require_once

2 个答案:

答案 0 :(得分:0)

在您提到的问题中:未定义的属性:stdClass :: $ CategoryID

,但错误显示:未定义的属性:stdClass :: $ CategoryIcon

如果$ CategoryIcon未定义,则表示您的任何表中都没有名为CategoryIcon的列。

你能提一下表格的细节吗?

在查询中提及列名而不是*。

总是一个好习惯

答案 1 :(得分:0)

在Controller中调用之前添加您的加载模型,检查名称是否正确,请确保(model = Class)中的某些名称

$this->load->model('model_name'); // 

或者在Confige.php中