Jump to content

PW newbie with a few questions


GuruMeditation
 Share

Recommended Posts

Hi all,

Firstly, thanks for PW. It truly is a joy to work with, and I'm learning new things on a daily basis.

I've been looking through the forums and the documentation and I've noticed that people seem to code things in many different ways. I'm new to PHP and PW, so I would appreciate it if the more knowledgeable members could look at my code and tell me if I'm doing it the right or the wrong way. The way I've coded it feels right to me, and I'm sure there are more efficient ways, but it seems to be working ok. Apologies for the formatting, it messed up a bit when I pasted it.

If there are any major flaws I can change my coding style. This is the code for my homepage.

<?php include('_header.inc'); ?>

	<!-- Body -->
        <br>

	<div class="content">
     
     <!-- Find out if there are any enabled blocks for the sidebar. -->
     <!-- If there are change the column to be full width -->

     <?php 
       $blocks = $page->homeblocks;
       $blockcount = count($blocks);
       
        // If there are blocks we will create a smaller column.
        // Otherwise we will make it the full width.
	
        if($blockcount) {
          echo "<div class='medium-9 large-9 column primary'>";
        } else {
          echo "<div class='medium-12 large-12 column primary'>";
        }
        
          // Display the homepage body
          echo "{$page->body}";
          
          // Display the article image and summary
          $articles = $pages->find("parent=/content/articles/, limit=15, sort=-created");
            foreach($articles as $article) {
            
               echo "<hr>" .
                        "<h3>{$article->title}</h3>" .
                        "<h6>Published: {$article->date}</h6>";
              
              // Only show the thumbnail if there is an actual image.
              if($article->cropimage) {
                $thumb = $article->cropimage->getThumb('thumbnail');
                echo "<a class='th' href='{$article->cropimage->url}'><img src='{$thumb}' alt='{$article->cropimage->description}' /></a>" .
                         "<br><br>";
              }
              
              echo "{$article->summarytext}";
              
              // If there is a body section, add a link to it.
              if($article->body) {
                echo "<h6><a href='{$article->url}'>Read More</a></h6>";
              }

            }
          
          
          // End of the column divs above           
          echo "</div>";
     ?>

     <?php 
       // If we have any blocks, display them.
       if($blockcount) {		
        echo "<div class='medium-3 large-3 column'>";

          foreach($blocks as $block) {
              echo "<div class='panel'>" .
                       "<h5>{$block->title}</h5>" .
                       "{$block->body}" .
                       "</div>";
          }

			  echo "</div>";
       } 
     ?>

	</div>
	
	<!-- End Body -->

<?php include('_footer.inc'); ?>

Finally, I'm stuck trying to retrieve some tags. I've set up a page field in my articles template so that I can add new pages (tags) via the Allow new pages to be created from field? option. The problem is, I can't seem to work out how to retrieve the selected options from within my homepage template. I'm know I'm overlooking something simple, but can't work out what.

My page field is simply named tags and I can get ID numbers when using the following $tags = $article->get('tags'); but I need the names (titles) of those tags that are currently selected rather than the ID numbers.

Thanks in advance.

Link to comment
Share on other sites

Welcome to yhe forum Guru!

I'm on mobile, so I won't be very detailed.

The code seems fine to me. You will develop some preferences when coding more and after seeing some more examples, but in general it looks ok. One thing I noticed is that you used this form in more than one place:

echo "{$page->body}";

this could (should) be replaced by:

echo $page->body;

since the quotes are needed only to work with strings and not variables, and the curly brackets are needed only to use variables inside the quotes. You can use both though, if you like doing and undoing things :)

--

Concerning the tags question: because multiple pages fields return a pageArray, you will have to iterate them to reach the individual pages. This can be done with a foreach()

foreach($tags as $tag) {

echo $tag->title;

}

//quite chalenging to code on my small android

--

by the way, you will realize that it is easier to learn php than to learn how to fornat code on the forum editor :P

  • Like 5
Link to comment
Share on other sites

Welcome to yhe forum Guru!

I'm on mobile, so I won't be very detailed.

The code seems fine to me. You will develop some preferences when coding more and after seeing some more examples, but in general it looks ok. One thing I noticed is that you used this form in more than one place:

echo "{$page->body}";

this could (should) be replaced by:

echo $page->body;

since the quotes are needed only to work with strings and not variables, and the curly brackets are needed only to use variables inside the quotes. You can use both though, if you like doing and undoing things :)

--

Concerning the tags question: because multiple pages fields return a pageArray, you will have to iterate them to reach the individual pages. This can be done with a foreach()

foreach($tags as $tag) {

echo $tag->title;

}

//quite chalenging to code on my small android

--

by the way, you will realize that it is easier to learn php than to learn how to fornat code on the forum editor :P

Thanks for the tip regarding the echo command. I've now changed my code accordingly. Your code also worked, although I'm sure I tried it earlier today, so I'm confused as to why it didn't work back then :unsure: Thanks anyway.

Welcome. Wasn't 'Guru Meditation' the old Amiga's blue screen of death?

It was indeed. The black screen with the red flashing text.  :lol: 

Thanks, I will have a look at your links and start learning.

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