Outputting to index.php from db

Hello,

I would like to output some data from mysql to my frontend/views/site/index.php some data i have recorded in mysql.

I created a model called backend/models/PageLayout.php

I have a controller called backend/controllerss/PageLayoutController.php

In my backend/models/PageLayout.php model I have added this:




     public function section1()

    {

return $model = PageLayout::find()->all();       

    }  



In my frontend/views/site/index.php I am trying to output this function using:




//At the top:

namespace backend\models;

use Yii;


<a class="page-scroll" href="#LINK1"><?php $model = section1::find()->all(); ?></a>

I have also tried this:

<a class="page-scroll" href="#LINK1"><?php echo $this->section1(); ?></a>



But I am getting this error:




PHP Fatal Error – yii\base\ErrorException


Class 'section1' not found



What am I actually missing there please?

Thank you,

Hi!

Sorry,

but it seems to me that you have not understood the basic concepts yet…

Please read the guide step by step:

http://www.yiiframework.com/doc-2.0/guide-README.html

Your Model is called "PageLayout" not secion1 …

For better understanding your mistakes, you could try this:

backend/models/PageLayout.php




public static function hello(){

   return "Hello World!"

}



In backend/controllers/PageLayoutController.php




// at the top of your view file 

use backend/models/PageLayout; 

... 

echo PageLayout::hello();



Best Regards

Would probably not hurt to read about classes in PHP as well :)

Thank you.

MVC is new to me so that’s why I am trailing a bit.

Ok understood the concept to pass straight data from the model to the view.

MetaCrawler I think you meant this:

put this code




public static function hello(){

   return "Hello World!"

}



Into backend/models/PageLayout.php

And put echo this code, not in the controller but in the view




<?php echo PageLayout::hello(); ?>



And for the top of the file, I had to change the direction of the slashes from:




use backend/models/PageLayout; 



to




use backend\models\PageLayout; 



All I want is simply write my sql queries manually and pass them into the views, I think it will be ok now…will give it a shot. At least you both made me understand the concept of passing data from a model to a view, thanks to both.

Found this, it looks quiet nice:

http://www.bsourcecode.com/yiiframework2/select-query-model/

Backslash is when specifying namespaces.

Instead of using a namespace, you could have written:


<?php echo backend\models\PageLayout::hello(); ?>



or, using the php shorttag for echo:


<?= backend\models\PageLayout::hello(); ?>

I see, this is cool to know, thank you Jacmoe.

I have a quick question regarding simple select queries, I am trying to follow the demos from here;

http://www.bsourcecode.com/yiiframework2/select-query-model/

in my PageLayout model I have added this:




    public static function section1()

    {

         

  $sql = 'SELECT section1 FROM page_layout WHERE id=1';

  return

  $model = PageLayout::findBySql($sql)->all();  


    }



The query works fine in phpmyadmin.

I was hoping to call the function by doing this in the view(like the first example):




<?php echo PageLayout::section1(); ?>



but it did not work as I am getting this message:




PHP Notice – yii\base\ErrorException


Array to string conversion



I did a var_dump() and got this:




array(1) {

  [0]=&gt;

  object(backend\models\PageLayout)#69 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> {

    ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;

    array(1) {

      ["section1"]=&gt;

      string(6) "Page 1"

    }

    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;

    array(1) {

      ["section1"]=&gt;

      string(6) "Page 1"

    }

    ["_related":"yii\db\BaseActiveRecord":private]=&gt;

    array(0) {

    }

    ["_errors":"yii\base\Model":private]=&gt;

    NULL

    ["_validators":"yii\base\Model":private]=&gt;

    NULL

    ["_scenario":"yii\base\Model":private]=&gt;

    string(7) "default"

    ["_events":"yii\base\Component":private]=&gt;

    array(0) {

    }

    ["_behaviors":"yii\base\Component":private]=&gt;

    array(0) {

    }

  }

}




That’s all pretty much I need to work out for my project, how to echo my select statements in the view, then that’s it I will be on the go… as I have created all the cruds,controllers and models.

your fuction section1() returning array so you have to print_r or foreach loop get its data


 

   $model_data = PageLayout::section1();

print_r($model_data);


foreach($model_data as $model){

 echo $model->id;

}