Fetching data from database to display in Index Page

In the index.php file I have the static content repeated 16 times with different image, price, location, etc…


<div id="slider" class="nivoSlider"> <img src="images/tab_image1.png" title="#caption1" /> <img src="images/tab_image2.png" title="#caption2"/> <img src="images/tab_image3.png" title="#caption3" /> <img src="images/tab_image4.png" title="#caption4" /> </div>

          <div id="caption1" class="nivo-html-caption">

            <div class="text12w text_cont_flats"> <span>Dubai Marina 1<br />

              1 Bedroom Apartment<br />

              AED 955,000</span> <span><a href="#" class="view_more"></a></span> </div>

          </div>

I’m trying to make it dynamic by fetching image, location price, bedrooms, etc… from the local database with the where condition as WHERE district = ‘some district name’ AND bedrooms >= 2; I think I need to create a function to pass it the district argument.

In the SiteController.php file what do I need to add in the actionIndex() function ?


public function actionIndex()

	{

		// renders the view file 'protected/views/site/index.php'

		// using the default layout 'protected/views/layouts/main.php'

		$this->render('index');

	}

I have created UnitListings.php file in the models folder. What do I need to add in this file ?


<?php


class UnitListings extends CActiveRecord

{

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

 

    public function tableName()

    {

        return 'unitlistings';

    }

}

Where is the district coming from? Get parameters or form submission?

In your actionIndex() you have to query the database such as:




$model = UnitListings::model()->findAll(" district = ':district' AND bedrooms >= '2'", array(':district' => $_GET['district']));



For more info on DB quering read this http://www.yiiframework.com/doc/guide/1.1/en/database.ar

and then pass the results from the query to the view such as




$this->render('index', array('model' => $model));



In your view you should have $model variable which contains an array of objects which you can iterate with foreach. Happy coding. If you need more help let me know

there is no form submission. In the index page there are 4 slider boxes for diplaying image, price, bedrooms, etc… in the index page. Each box rotating data of it’s own single district. In the SiteController.php file’s actionIndex() method, do I need to make 4 calls to model function say public function getDetail($district) ?

you have to fetch all data from DB and insert it into the slider so it can be rotated

u may just find all the details from the action index, and pass it like bettor said


$this->render('index', array('model' => $model));

u will get that $model in ur index page…just use a foreach…very simple??

u will get anything that u pass using render

for Eg.

$this->render(‘index’, array(‘shaani’ => $model));

u will get that $shaani in ur index page…

try this…dont need to call function everytime.

Since there are 4 different district boxes, do I need to call this line 4 times ?


$model = UnitListings::model()->findAll(" district = ':district' AND bedrooms >= '2'", array(':district' => $_GET['district']));

i.e.


$model = UnitListings::model()->findAll(" district = ':district1' AND bedrooms >= '2'", array(':district1' => District1);

$model = UnitListings::model()->findAll(" district = ':district2' AND bedrooms >= '2'", array(':district2' => District2);

$model = UnitListings::model()->findAll(" district = ':district3' AND bedrooms >= '2'", array(':district3' => District3);

$model = UnitListings::model()->findAll(" district = ':district4' AND bedrooms >= '2'", array(':district4' => District4);

this line is not giving me 4 rows. It returns all rows


$model = UnitListings::model()->findAll("District = 'Dubai Marina' AND Sellprice > '0' AND Bedrooms > '0' AND Status = 'available'", array('limit' => '4'));

But this when I use this


$model = UnitListings::model()->findAll(array('limit' => '4'));

, then I get 4 rows.

How can I make the first code to give me 4 rows only

I have an error Class ‘frontend \ controllers \ UnitListings’ not found Can you help me finish it and if possible the way to iterate it thanks

I am new in Yii. I try to display data from database in view but every time ‘Trying to get property of non-object’ error shows…

here is my code

in StudentController.php

public function actionStudent_profile($userid)

{


    


    &#036;studentInfo = Student::find()-&gt;where(['userid'=&gt;&#036;userid])-&gt;all();


    // print_r(&#036;studentInfo);


    return &#036;this-&gt;render('viewProfile', [


        'model'=&gt;&#036;studentInfo,


    ]);


 }

in viewProfile.php


<?php

use yii\helpers\Html;

use yii\widgets\DetailView;

/* @var $this yii\web\View */

/* @var $model app\models\Student */

?>

<?= Html::encode($model->mobile); ?>

note: my databse table(student) has following coloums username,first_name,mobile…