Best way to output data from table in yii2

Hello,

I have finished the courses about Activeform, I can now create input fields and get data pass in the database but what I cannot yet work out is how to output basic data from mysql to the front end of the site.

ie:

I have a table called "news" and have a field called "todaysnews", how to pickup this data from the database and output it in a view please?

Thank you,

Ben

have u generated MVC for News table? If not then generate it by Gii.

then u get the news data in locahost/yourweb/news/index

u will get all ur table data in grid.

Modify it as per your requirement.

Hi!

Take a look at the guide:

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

And read the secion: "Displaying Data"

Or like "rups g" said generate CRUD and look at the code:

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

Regards

Hello,

Yes sorry, I did not precise, I have created the model, crud and the links for the news in the nav bar, I can now input the data, in the crud. My problem is not that but output the data "as is" in the front end, I could work this out in pure php but not the way yii does it.

What is the best way to do this please, remember, I simply want to output what is in the database as plain text.

Thank you, I will check your links.

Ben

Looking at the link you gave me here is where I am confused:

$news = News::find()->orderBy(ā€˜nameā€™)->all();

This is exactly what I need to do, I want to output from the table news all the data orderedBy "Name".

But where to put this data? In the model class or the view?

I tried in the view and it does not workā€¦this is the exact problem I am facingā€¦I have no idea where to place this code in order to show $news = News::find()->orderBy(ā€˜nameā€™)->all(); in the view

Could you please help me on this? As soon as I understand this part, my whole learning will advance much faster.

Thanks,

Ben

Iā€™m not entirely sure if I understood your question correct. ;)

But you can basically do:




$news = News::find()->orderBy('name')->all();



Everywhere as long as the model-class is available in that file.

To make it available you need to "use" it.

Simple example for plain-text output.

Put this in your view:




// use the path to your "News-Model" 

// put the use statement at the BEGINNIG of your file

use app/models/News; 


// now you can use "News-Model" to find data

$allNews = News::find()->orderBy('name')->all();


// if you want plain-text you could do for EXAMPLE

foreach ($allNews as $news) {

   echo "$news->name <br />";

}



But this is NOT the best way (like your thread-title suggests).

Why do you want it in plain-text?

Yii provides you nice dataproviders and widgets to display your data:

Dataproviders: http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

Widgets: http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html

Also - as far as I understood calling all that in your VIEW is not a good practise. Actually your query belongs into your NewsController.php and passes the result onto the related view.

Hope this helps.

Regards

MetaCrawler, thank you so much for this wonderful explanation which is really unlocking a big chunk of where I was stuck.

The use\ā€¦ was my problem, I could not yet understand why it was there, now I understand, it enables access to the given model which can then be called.

The reason I was asking about text file(I meant html) is because I just wanted to use the advanced template and record in a crud a welcome text text for the front end of a website then output it in a bootstrap widget.

Just enabling me to do this, is already a big step forward in my learningā€¦

I am now ok with creating data in the database, have relations, create model and crud via Gii, my last thing was to understand ths "use" keyword.

I will of course keep asking a lot of questions in this forum for the synthax as it is the most difficult for me at the moment,

Thank you so much!

Ben

1 Like

Glad I could help ;)

Regarding the "use" statements:

Google for ā€œphp namespacesā€ to get a better understanding. ;)

Regards

Thanks MetaCrawler!

Ben

Quick question for you, your solution is working above, I tested and it is great.

But when I try to only output 1 details as:





        <?php 

$allbooks = Book::findOne(1);


   echo "$allbooks->title <br />";


   ?> 



I get the error:

PHP Notice ā€“ yii\base\ErrorException

Trying to get property of non-object

I then tried this from the doc:




     <?php 

$allbooks = Book::find()->where(['title' => 1])->one();


   echo "$allbooks->title <br />";


   ?>



But same problemā€¦

Any idea why please? I am only trying to pull 1 record from the row "title" from the Book model, it should work no?

Thank you,

Ben

Ok I understand my error, this code only pulled a specific id, unfortunately I did not have id 1 in the database but it started at id 8 which now works.

So does anyone knows the synthax to pull for example the first 10 or 20 results from the database please according to a specific filter like id, or name or first nameā€¦

Thank you,

Ben

I tested the exact same code with one of my models and it works.

The problem must be somewhere elseā€¦

Are you sure there is a "Book" with Primary ID = 1?

For example when I try:




$category = Category::findOne(1); 

echo $category->name; 



That works without any problems.

But when I do following:




// I only have 6 Categories in DB - so category ID=100 does not exist. 

$category = Category::findOne(100); 

echo $category->name; 



I get the same error like you.

Thanks MetaCrawler yes I already replied above your post, I found that I did not have an id of 1 in my database which caused the error.

Any idea how to pull the first 10 records with a filter like "OrderBy" please? I cannot find it in the docs.

Thank you

Ben

Yes - all you want is described in the guide here:

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

You can look for any attribute.

Lets say you want 10 records from "Books".




        $books = Book::find()->orderBy('title ASC')->each(10);


        foreach ($books as $book) {

            echo $book->title."<br />";

        }



In the linked Doc you find examples how to look for a special attribute like title etc.

Regards

I see, got it, yes you are right, I just checked the "Retrieving Data in Batches"

I missed this one outā€¦ouch.

Thank you so much my friend!!!!

Ben