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