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?
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.
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.
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:
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.
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,
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ā¦