Jump to content

Migrate a custom table (accordion) to PW


pwFoo
 Share

Recommended Posts

Hi,

I'm new with PW and thinking about migrate a site with a custom table. So maybe You could help me with some information about how to do it with PW. 

The table contents (4 columns and additional a category column) are grouped by category and styled as accordion (categories = accordion headers).

1) How could/ should it done with PW? A custom table or the core repeater module?

2) How can I import the table content to PW?

Best regards

Link to comment
Share on other sites

Xeto, welcome to the forums. You have a few options:

1. Leave the data in your tables and just query it from ProcessWire using $this->database (PDO) or $this->db (mysqli). 

2. Export your tables to a spreadsheet (CSV) and import using the ImportPagesCSV module. 

3. Query or export your data out of your existing tables and create new pages from the API. This is the route I usually take. See the cheatsheet. Also see the Data Conversion section (at the top) of the CMSCritic case study, which describes how to query data out of an existing table and create new pages from it. 

  • Like 2
Link to comment
Share on other sites

Hi Ryan,

thanks for the reply! Good to get an answer here as a beginner!

After read some topics and articles about PW and some tests later, I've understand that also categories and tags should/ could be done with (hidden) pages.

I'll try it with ImportPagesCSV which I found as import solution some days ago. Yesterday in the afternoon I found the categories/ tag solution :)

Thanks!

  • Like 1
Link to comment
Share on other sites

Export, import and store data should be no problem, but I get a new question in my mind...

How to output/ render the data?

Data structure

- category 1
-- data row 1
-- data row 2
...
- category 2
-- data row 3
...
- category 3
...

Categories are like tags in a blog (PageAutocomplete field). Each row also is a page

I thinking about use the PW template class at the template file to fill a "category" and "datarow" view. What's best practise to output/ render content with my data structure?

Link to comment
Share on other sites

It looks like data rows are under category rows. What are data rows? Do these represents the pages having that category or something else? If they are the pages having that category, consider using page references and moving the pages to their own container. That way your pages can have multiple categories, if they need it. Though if multiple categories aren't ever needed, then the structure is just fine. 

Also your questions relate very closely to what the Blog profile does, so I'd suggest  looking at that too. 

Link to comment
Share on other sites

Data rows and categories are pages with own container. Categories referenced via page field.

Yes, I need multiple categories. So my setup should be fine ;)

But how to output the nested categories -> category data rows? Any easy way with PW API or have to use nested loops?

[EDIT]

I'll try it loop through my categories and load and output all the related pages.

Should work and I haven't found any better solution (if exists...)

.[/EDIT]

Link to comment
Share on other sites

But how to output the nested categories -> category data rows? Any easy way with PW API or have to use nested loops?

I'm not sure I know enough about this particular case to suggest code for it. But PW's API is PHP based so you can pretty much do anything. I don't know for sure what you mean by nested categories, but if you mean something like a category/subcategory/ structure, where you wanted to output the pages having each of the subcategories you could do it like this:

foreach($pages->get('/categories/')->children as $category) {
  if(!$category->numChildren) continue; 
  echo "<h1>$category->title</h1>";
  foreach($categories->children as $subcategory) {
    $items = $pages->find("subcategories=$subcategory"); 
    if(!count($items)) continue; 
    echo "<h2>$subcategory->title</h2><ul>";
    foreach($items as $item) {
       echo "<li><a href='$item->url'>$item->title</a></li>";
    }
    echo "</ul>";
  }
}
Link to comment
Share on other sites

Hi Ryan,

thank You for the code example! Great to see how it should be done with PW! :)

Without subcategories something like that should be work/ used?

foreach($pages->get('/categories/')->children as $category) {
  echo "<h1>$category->title</h1><ul>";
  $items = $pages->find("categories=$category");
  foreach($items as $item) {
     echo "<li><a href='$item->url'>$item->title</a></li>";
  }
  echo "</ul>";
}

I'll try both soon. Thanks :)

*UPDATE*

Successfully tested the code above without subcategories. 

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