Jump to content

Newb: Database rows to grid of items


Mercury
 Share

Recommended Posts

Hello,

I am a total newb to processwire but have experience with other cms and frameworks. I saw a video and got a basic understanding of how processwired is used for simple webpages. If i want to get rows from a table i found i can query them like this 

$result = $this->db->query("SELECT id, name, title, url FROM yourtablename WHERE id=$id");
and i know i could then loop and print the results on a grid with the html.

I was wondering is there a way to use partial views in this? Should i use the fields or simply echo these and create fields only for things i fill by hand?

If they are links and the page i go to has the respective data how do i go about it? Do i query again for only one row using the id from the url and echo the values? Do i use the fields of process wire in this case? 

Even if there are different ways to do it i would like the proper one so that i dont regret it later or when i do some expansion.

Any advice or link to guides are welcome.

Link to comment
Share on other sites

Hello @Mercury and welcome to the forums,

Do you really want to use MySQL queries? Why not the API instead? See, for example:

https://processwire.com/api/selectors/

I do not know what you have read/watched so far, but probably the best article to start with is this one:

https://www.smashingmagazine.com/2016/07/the-aesthetic-of-non-opinionated-content-management-a-beginners-guide-to-processwire/

A tutorial that I can also recommend (among other things it utilizes the twig template engine which you might not need, but other important concepts are also demonstrated):

http://blog.mauriziobonani.com/processwire-basic-website-workflow-part-1/

 

  • Like 7
Link to comment
Share on other sites

Hello again,

I read some more and i understood some things.

Lets say i want to have a list of cars. So that when a car is clicked i go to its individual page.

Templates are linked with fields. 

So i can create an empty template car with all the fields i would put in a database. Then create one page for each car i want.

Then for the grid i create a grid template and call the $pages->find returning all cars and displaying them as a grid. First problem, the grid shows less info than all the fields so i get useless info with this call also this grid template uses no fields as far as cars are involved its simply loop-prints and has only one page.

Each car item is a link to an individual page. I can use a template for car-display that simply checks the url parameter and $pages->find one car displaying its information. Again no fields on this template as far as cars are involved it simply prints one car information it has retrieved and has only one page.

Am i on the right track on this? Is there a better more proper way? Am i tottaly of track?

Thanks for all the guidance.

Link to comment
Share on other sites

1. Create all the fields you need relating to a single car. One field you don't need to create is "Title" because that is a global field that exists by default.

2. Create a template "car". To this template add the fields you created in step 1. This is the template that will output the fields for a single car.

3. Create a template "cars". This is the template that will output the list of cars. If it only outputs the list of cars it may not need any fields added to it apart from the default "Title".

4. Create a page "Cars" that uses the template "cars".

5. Create a child page of "Cars" for your first car that uses the template "car". Fill out the fields. Repeat this step for all your cars (see end of post for a time-saving tip).

6. Create template file "car.php" in /site/templates/. In this file output your fields as needed, getting each field as $page->my_field_name

7. Create template file "cars.php" in /site/templates/. In this file you will get a PageArray of all the cars and then loop over them to create links to the individual car pages.

// get PageArray of all cars
$cars = $pages->find("template=car");
// in this example you could also get $cars with: $cars = $page->children();

// Output list of links...
if(count($cars)) {
    echo "<ul>";
    foreach ($cars as $car) {
        // $car is the Page object for an individual car
        // You can get any field of the car with $car->my_field_name
        echo "<li><a href='{$car->url}'>{$car->title}</a></li>";
    }
    echo "</ul>";
}

 

Time-saving tip: you could import data for your car pages in bulk from a CSV using this module.

  • Like 5
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...