简体   繁体   中英

CodeIgniter not pushing variable to View

So I'm building an application for users to be able to upload rides to a website and have them be viewable to other users. The only problem is that my controller is apparently not properly pushing the variables to the view making me have an error that reads:

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

I'm not exactly sure what I'm doing wrong but here is the code. Also, cause I know it will be asked, yes, my model is returning real data. Also the var_dump() in the view returns null. And the ridesArray() function is just one that makes a neat array with the information gathered from the model and it works. The problem here is that the variable is not getting sent to the view

UPDATE: I've added the code for the function ridesArray(). Also I forgot to mention I am also getting this error:

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

Controller:

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;
}

Model:

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

    return $result;
}

View:

<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>

This is exactly what Sparky said in his comment.

Change

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

to

$ridesArray['ridesArray'] = $rideData;

That will get rid of the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM