簡體   English   中英

CodeIgniter沒有將變量推送到視圖

[英]CodeIgniter not pushing variable to View

因此,我正在為用戶構建一個應用程序,使其能夠將游樂設施上傳到網站,並使其他用戶可以看到它們。 唯一的問題是我的控制器顯然沒有正確地將變量推入視圖,從而使我出現以下錯誤:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: ridesArray
Filename: views/rides.php
Line Number: 93

我不確定自己在做什么錯,但這是代碼。 另外,因為我知道會被問到,是的,我的模型正在返回真實數據。 此外,視圖中的var_dump()返回null。 ridesArray()函數只是一個從模型收集的信息組成一個整齊的數組,它可以工作。 這里的問題是變量沒有被發送到視圖

更新:我已經添加了函數ridesArray()的代碼。 我也忘了提我也遇到這個錯誤:

Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/rides.php
Line Number: 96

控制器:

function index(){
    $userrides = $this->RidesModel->getAllRides();
    $rideData = $this->ridesArray($userrides);
    $ridesArray = array('ridesArray' => $rideData);
    $this->load->view('rides', $ridesArray);
}
function ridesArray($data){
    $newarray = array();
    foreach($data as $object){
        $array = json_decode(json_encode($object), true);

        $id = (int)$array['id'];
        $title = $array['title'];
        $date = $array['date'];
        $ridetime = $array['ridetime'];
        $saddress = $array['StartAddress'];
        $scity = $array['StartCity'];
        $sstate = $array['StartState'];
        $szip = $array['StartZip'];
        $ezip = $array['EndZipcode'];
        $ecity = $array['EndCity'];
        $image = $array['image'];

        $newdata = array(
            'id' => $id,
            'title' => $title,
            'date' => $date,
            'ridetime' => $ridetime,
            'StartAddress' => $saddress,
            'StartCity' => $scity,
            'StartState' => $sstate,
            'StartZip' => $szip,
            'EndZipcode' => $ezip,
            'EndCity' => $ecity,
            'image' => $image
        );
        array_push($newarray, $newdata);
    }
    return $newarray;
}

模型:

    function getAllRides(){
    $sql = "select * from rides order by date";
    $query = $this->db->query($sql);
    $result = $query->result();

    return $result;
}

視圖:

<div class="container col-lg-12 col-md-12 col-sm-12 user-cont">
<ul class="container col-lg-10 col-md-10 col-sm-10 col-lg-offset-1 col-md-offset-1 col-sm-offset-1">

    <?php
    var_dump($ridesArray);

    if(isset($ridesArray)){
        foreach($ridesArray as $out){
            echo "<li>";
            echo "<h3>".$out['title']."</h3>";
            echo "<img class='img-responsive img-thumbnail' src=". base_url(). "uploads/".$out['image']."/>";
            echo "<p>".$out['date']." At ".$out['ridetime']. "</p>";
            echo "<p>Starts At ".$out['StartAddress']." ".$out['StartCity']."    ".$out['StartZip']." "."</p>";
            echo "<p>Ends At ".$out['EndCity']." ".$out['EndZipode']."</p>";
            echo "<span class='glyphicon glyphicon-plus'></span>";
            echo "</li>";
        }
    }
    ?>
</ul>

這正是Sparky在其評論中所說的。

更改

$ridesArray = array('ridesArray' => $rideData);

$ridesArray['ridesArray'] = $rideData;

那將消除錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM