Jump to content

function and output trouble


bwakad
 Share

Recommended Posts

I have this function to limit words from body field in list view

function limit_words($string, $word_limit) {
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}
# declare field for limit output
$intro = limit_words($page->body,20);

then I call child pages though my template file with this:

foreach($pages->find("parent=/all/") as $child) {
        echo "<a href='{$child->url}'>{$child->title}</a>";
        echo $child->$intro;

echo $child->intro // is obvious, just displays full text

echo $child->$intro // produces nothing

echo $intro // does nothing

Maybe I'm simply not seeing it clear at the moment - so if anyone knows, please give a tip...
 

Link to comment
Share on other sites

I don't understand anything of the logic here..

If $child is a Page object why do you try to attach string data from the current page to it.

Limiting to an amount of words, doesn't make that much sense.

Pneumonoultramicroscopicsilicovolcanoconiosis, Supercalifragilisticexpialidocious, Pneumonoultramicroscopicsilicovolcanoconiosis are long words  

word count: 6

It's better to limit to an x amount of charachters, but break at a word.

Link to comment
Share on other sites

Well, I have not met anyone using that long words. lol. But to explain:

Look at the skyscrapers. The titel, body, and other fields make up the list of items.

root

- listview

- all

- - item 1

- - item 2

My currentpage (in this topic) is called - listview

And I am trying to display the all/items on that listview page.

Or do I have to use the parent page for that (am I doing it wrong?)

And if limit characters would be the way, how you end it with a whole word 'what ...' instead of 'wh ...' ?

Link to comment
Share on other sites

I can tel you out of experience, if you're using Lorem Ipsum and later on you've to replace it with dutch, (What's not unlikely if I see your info)

You'll end with much longer words, and maybe breaking your design.

My currentpage (in this topic) is called - listview

Current Page is always $page or wire('page') or $this->page, that depends on the context. ( Why do you want to have 2 instances of Page )

 Or do I have to use the parent page for that (am I doing it wrong?)

 ??

And if limit characters would be the way, how you end it with a whole word 'what ...' instead of 'wh ...' ?

have you even read the post ? please look up again

Link to comment
Share on other sites

It's simple: You have that function 

function limit_words($string, $word_limit) {
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}

A simple call to use this function would be

$shorty = limit_words($child->body, 20);
  • Like 2
Link to comment
Share on other sites

Current Page is always $page or wire('page') or $this->page, that depends on the context. ( Why do you want to have 2 instances of Page )

Here I am totally loosing you.

have you even read the post ? please look up again

it seems we made about the same time a reply. I did read it after, took the code, just not sure how to attach it to the $child->body

But just to clarify, when I display this, I am talking about my page tree in back-end :

root

- listview    // this is the page where I now try to retreive all items from under /all/

- all           // or do I have to use this page to retrieve all items under it, is that the more logic way?

- - item 1

- - item 2

on this listview (page) - i have this code:

    <?php foreach($pages->find("parent=/all/") as $child) {

        echo "<a href='{$child->url}'>{$child->title}</a>";

        echo $shorty; // using soma's solution also displays nothing

        echo $child->branche->title;

   } ?>

Link to comment
Share on other sites

$page: current page

function trunc($string, $length) {
    return substr($string, 0, strpos(wordwrap($string, $length), "\n"));
}

echo trunc($page->body, 100); 
// string 100 characters or little less, body field from the current page, cut-of on word

it seems we made about the same time a reply. I did read it after, took the code, just not sure how to attach it to the $child->body

This happens all the time, not problem...

echo trunc($child->body, 100); ??

Edited by Martijn Geerts
  • Like 2
Link to comment
Share on other sites

Thanks Martijn, for pointing to $page: current page, and I did not know I had to use functionname in front.

I simply had to use $child, since that was used in the foreach as a '$page e.g. current page'.

After your previous replies, I have to ask, if this is the way to make a listing view page, or are there other more used techniques?

Link to comment
Share on other sites

Nothing wrong with foreach...

root
   |--listview    // here we are 
   |
   `--all         // template products
      |           
      |-- item 1  // template product
      `-- item 2  // template product

$products = $pages->find('template=product, parent.template=products'); 
// it's better to use template in a selector then a name/path. (most of the time) 
// template defines behaviour, less likely to change then a path or a name 

if (count($products)) { // only if we have products 
    
    $out = "<ul>"; 
    foreach ($products as $product) { 
        $out .= "<li>" . 
            "<a href='$product->url'>" . 
            $product->title . // fieldname title 
            "</a><br>" . 
            $product->summary . // fieldname summary 
            "</li>";
    } 
    $out .= "</ul>";

    echo $out; 
}

There are some people that tell outputting strings is quicker with single quotes, I never tested it, but they are probably right.

I do prefer the double quotes, as PHP get not processed inside single quote enclosed strings. I ended up in the past, replacing single quotes so many times to double ones that I get tired of it. (lazy)

ProcessWire made me change my quote style technic. Now I always use single quotes for HTML attributes, so I can double quotes around a HTML string.

Edited by Martijn Geerts
  • Like 1
Link to comment
Share on other sites

I don't understand anything of the logic here..

If $child is a Page object why do you try to attach string data from the current page to it.

Limiting to an amount of words, doesn't make that much sense.

Pneumonoultramicroscopicsilicovolcanoconiosis, Supercalifragilisticexpialidocious, Pneumonoultramicroscopicsilicovolcanoconiosis are long words  

I am so glad those weren't on any of my spelling tests... 

  • Like 1
Link to comment
Share on other sites

I changed the code from martijn a litle because it did not let through a string that was shorter then what was set in trunc($child->body,180).

It works for me, but I am not totally convinced about the two } at the end. Anyway...

Code function:

function trunc($string, $length) {
    $length = 180; //modify for desired width
    if (strlen($string) > $length) {
        return substr($string, 0, strpos(wordwrap($string, $length), "\n"));
    } else {
        return($string);
    }
}

Code in template $child can be a $page:

trunc($child->body,$length);

echo "<div>"{trunc($child->body,$length)}"</div>" did not work for me. But echo with dots instead of brackets did?!

Link to comment
Share on other sites

Indeed, my function was not outputting, when smaller then the desired length. Sorry for that.

// you don't want this line
$length = 180; //modify for desired width

and in the template file probably also change : trunc($child->body,$length); to trunc($child->body,180); // what you said before...

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