Jump to content

Adding more than one data to a field


Cengiz Deniz
 Share

Recommended Posts

Hello, I am newbee. This is my first message. I am trying to learn PW. After modx, I loved it very much.

My question:

I am tryin to make a site which shows people and their aforism. I made a field but just one aphorism can input.

How can I add more data to a field ?

This is my previous page which made with modx : https://cdeniz.com/k105/Nazim-Hikmet.html

 

Quote

 

ÖzlüSözleri

1. "Yaşamak bir ağaç gibi tek ve hür ve bir orman gibi kardeşçesine."

2. "Sen yanmasan, ben yanmasam, biz yanmasak nasıl çıkar karanlıklar aydınlığa?"

 

 

Thank you

Link to comment
Share on other sites

Welcome to the forum @Cengiz Deniz

 

About your question, you could use a Repeater for the aphorisms in your template.

First read this doc :  http://processwire.com/api/fieldtypes/repeaters/ ,  you should get an idea on how repeaters works.

 

So for your example, you could make a new text field called aphorism_title and a new repeater called aphorism_repeater. Finally you add the text field 'aphorism_title' to the repeater. You should end up with something like that :

Capture.PNG.6aa33b7604165bd60d771e04c254c1ff.PNG

 

In your page template, you add the repeater field you just created, and add some aphorism to it :

Capture.thumb.PNG.9301b18045190ebb3e2a36bc1734202f.PNG

 

Then in the template code, a simple foreach loop will do the job to show all the aphorisms in a list:

<h3>ÖzlüSözleri</h3>
<?php
     $aphorisms_list = '';
     foreach($page->aphorism_repeater as $aphorism) 
     {
          $aphorisms_list .= "<li>{$aphorism->aphorism_title}</li>"    
     }
     
?>
<ol>
<?php echo $aphorisms_list; ?>
</ol>  

 

Hope its clear enough, you can adapt it depending the output strategy you use, but do not hesitate to ask more information.

Good luck!

 

 

Edited by flydev
typo / code
  • Like 5
Link to comment
Share on other sites

19 hours ago, Cengiz Deniz said:

Every aphorism should have one or more topics fields. Is it possible ?

Surely, but I don't get what you mean by one or more topics fields, could you explain a bit further whats your needs and/or show us what you already tried ?

 

Maybe are you talking having an aphorism with a reference to a page in the PageTree ?

Link to comment
Share on other sites

Keep in mind that there a are other better ways to achieve this. You should read this tutorial made by @kongondo

 

Anyway, to continue with the repeater and to add tags to your aforisms you could create a new template "aforisms_tags" and a page in the PageTree called "Aforisms Tags", assign the template "aforisms_tags" to this new page; And add some children to this page: tag1, tag2, tag3...

Then you create a new field of type PageReference , set the option Input Field Type to "AsmSelect" and set the option Parent to the new created page "Aforisms Tags". Then you add the field to your "aforisms_repeater".

Illustration :

58abe64200d7e_Capturedecran2017-02-21a08_03_12.png.1cd7d1578beabe3c209e1d568b009ef7.png                      58abe5cae5899_Capturedecran2017-02-21a07_57_11.thumb.png.271426935dee81492698a5a2a1ad92ae.png

 

There is the code :

$aphorisms_list = '';
$tags = '';
foreach($page->aforisms_repeater as $aforism)
{
    $aphorisms_list .= "<li>";
    $aphorisms_list .= $aforism->aforisms_title;

    foreach ($aforism->aforisms_tags as $tag) {
        $tags .= "<span class='tag' style='margin: 3px; color: red;'>";
        $tags .= $tag->title;
        $tags .= '</span>';
    }
    $aphorisms_list .= "{$tags}</li>";
}

$content .= "<ol>{$aphorisms_list}</ol>";

 

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