Jump to content

How to get Data From Database and Fields


Timberwolf
 Share

Recommended Posts

Hello,

i'm new to processwire, but not to php and all the LAMP Stuff.

To answer all Pro's questions, yes i used the Search but found nothing that fits to my Issue.

 

Current i try to build ym own Blog/news Page. I created  ym own template file called news.php and pinned it at my Template in the Backend. For now all fine. 

I want to use twig as template Engine. I also found my testentry for nws with phpmyadmin in the Database.

My Issue is that if use the $page or $pages i'll never get any Data from DB or Pages or else.

Could anyone please tip with a finger on my false Code?

<?php namespace ProcessWire;

$urlString = $page->url();
$twigPage = "";

switch($urlString){
    case "news_feed";
        $pages->get("/presse/")->find("template=news");
        
        echo $twig->render('pages/news_feed.twig', $tpl_vars);
        break;
    case "news";
        $resultSet = $pages->find("template=news, news_id=1");
        
        echo $twig->render('pages/news.twig', $tpl_vars);
        break;
    case "newsstart";
        
        $resultSet = $page->get("template=news, sort=-news_created, limit=3");
        if ($resultSet) {
            while ($row = $resultSet->fetch()) {
                $nachricht = message($row);
                array_push($rows,$nachricht);
            }
        }
        
        echo $twig->render('pages/newsstart.twig', $tpl_vars);
        break;
    case "newsarchive";
        $resultSet = $page->get("template=news, sort=-news_created");
        
        echo $twig->render('pages/newsarchive.twig', $tpl_vars);
        break;
    default:
        $resultSet = $pages->get("/presse/")->find("template=news");
        
        echo $twig->render('pages/news_feed.twig', $tpl_vars);
        break;
}

Kind Regards

Link to comment
Share on other sites

hi timberwolf, welcome to the forum :)

3 hours ago, Timberwolf said:

To answer all Pro's questions, yes i used the Search but found nothing that fits to my Issue.

2 hints here:

  1. many of us are using google to easily search the forum: https://processwire.com/talk/topic/6196-easy-search-on-pw-forums-with-google/
  2. maybe you did'nt find anything because getting data from the database is just too simple with processwire ;) that's a common problem when starting with processwire: you look for problems/compexity that you know from other systems but that simply don't exist in the pw world :)
3 hours ago, Timberwolf said:

news.php and pinned it at my Template

here it already starts sounding strange... also in your code example you are mixing some things up and are making things more complex than they need to be!

give yourself 20minutes and do the https://processwire.com/docs/tutorials/hello-worlds/ tutorial step by step. I'm sure it'll make "click" after that ;)

PS: you are mixing up $pages->find() and $page->get() in your example! see https://processwire.com/api/ref/pages/find/ and https://processwire.com/api/ref/page/get/

  • Thanks 1
Link to comment
Share on other sites

You have a few peculiar selectors in there. I'm not sure what you are trying to do but it looks to me like there is some confusion between $pages and $page, and between get() and find().

$pages is all the published (and not hidden) pages in the site (that the current user can view).

$page is the current page only.

get($selector) will return the first one item matching $selector.

find($selector) will return all items matching $selector.

Take a look at http://cheatsheet.processwire.com/ 

  • Like 2
Link to comment
Share on other sites

Hi Dave,

thank you for reply. I checked the site, and yes there was a little confusion about $page and $pages. Thank you for that.

But are i'm right that i have to use "template=news" to select the right table?  I have in my backend 4 fields which are filled and which i try to select.

First i had written all of that in normal Database queries but that seems not good and again coding standards.

Link to comment
Share on other sites

7 minutes ago, Timberwolf said:

Hi Bernhard,

maybe it is to easy and maybe i try to find more complexity as it is.

What i ment with "pinned" attached as screen. 

I know what you meant ;) And that's why I said it sounds weird. Because it seems you are doing it "wrong".

I say it again: Take yourself 20minutes and do the Hello Worlds Tutorial. Step by Step. And I mean DO it, not just read. Especially when you have some programming background the concepts of ProcessWire will be different than you expect. And I bet when you are done with the tutorial you can answer your questions from the first post yourself.

I don't want to sound lecturing...

1 minute ago, Timberwolf said:

you was right. thanks.

I thinks this can be closed and marked as solved

haha, I knew it :D and I just wanted to add: Invest that 20minutes and have fun for the next years enjoying the ease of pw ;)

PS: Seems it made click in 7 minutes, not in 20 ^^

  • Haha 1
Link to comment
Share on other sites

1 minute ago, bernhard said:

I know what you meant ;) And that's why I said it sounds weird. Because it seems you are doing it "wrong".

I say it again: Take yourself 20minutes and do the Hello Worlds Tutorial. Step by Step. And I mean DO it, not just read. Especially when you have some programming background the concepts of ProcessWire will be different than you expect. And I bet when you are done with the tutorial you can answer your questions from the first post yourself.

I don't want to sound lecturing...

haha, I knew it :D and I just wanted to add: Invest that 20minutes and have fun for the next years enjoying the ease of pw ;)

PS: Seems it made click in 7 minutes, not in 20 ^^

yes but now i'm pointed to the next questoins... i'l try to find it with you google link ;)

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...