Jump to content

function does not work


Cengiz Deniz
 Share

Recommended Posts

Hi,

I can not run following function code. Help please. Thank you

 

function konular($template,$catPageId,$ColCount,$ColSize,$ImgX,$ImgY)  {
$x = 0;
$category = wire('pages')->get($catPageId);
echo '<h2><a href="'.$category->url.'">'.$category->title.'</a></h2>';

$selector= 'template='.$template.', limit=4, sort=random';
$subs = wire('pages')->find("$selector");

foreach ($subs->children as $tree_item) {
	$item_image=$tree_item->images->first()->size($ImgX,$ImgY);	
    if($x % $ColCount == 0) echo '<div class="row">';

      echo '<div class="col-sm-'.$ColSize.'"  style="margin-bottom:10px; padding-top:10px;padding-bottom:10px; ">';
			echo '<h3><a href="'.$tree_item->url.'">'.$tree_item->title.'</a> </h3>';        
			echo'<a href="'.$tree_item->url.'"><img src="'.$item_image->url.'"  class="img-responsive"  style="width:100%"></a>';

			echo '<p>'.$tree_item->summary.'</p>';
        echo '</div>';

    $x++;
    if($x % $ColCount == 0) echo '</div>';
}
}

$subs query and category title are working well. But then query results do not appears.

 

Capture.JPG

Link to comment
Share on other sites

9 minutes ago, Cengiz Deniz said:

Hi,

I can not run following function code. Help please. Thank you

Can you please be a little more descriptive of what problems you are having, so someone can adequately help you (i.e. what errors are you getting)?   Thanks

  • Like 2
Link to comment
Share on other sites

How do you know the $subs query is working? 

Install TracyDebugger and do a:

bd($subs->each("title"));

and see what pages are being returned.

If none, then

bd($selector); 

to see what the actual contents of the selector are - mostly it would be good to know what $template is actually returning.

  • Like 1
Link to comment
Share on other sites

$selector= 'template='.$template.', limit=4, sort=random';
$subs = wire('pages')->find("$selector"); // $subs is a PageArray

foreach ($subs->children as $tree_item) { // A PageArray can't have children, only a Page can have children

 

  • Like 3
Link to comment
Share on other sites

Merhaba @Cengiz Deniz,

The problem is you're trying to iterate over children of some PageArray ($pages->find() returns PageArray), but only Page objects can have children like @Robin S pointed out. You should directly iterate over them like foreach($subs as $tree_item){} and you should be ok. Another problem would be that you're surrounding your selector inside double quotes.

<?php
$selector= 'template='.$template.', limit=4, sort=random';
$subs = wire('pages')->find("$selector"); // your selector would become "'template=...'"
// you should
$subs = wire('pages')->find($selector);
// or just use double quotes for variable interpolation to strings
$subs = wire()->pages->find("template=$template, limit=4, sort=random");

Now, I'll show you a different, but better (IMHO) way to tackle this. Instead of using a function, I prefer refactoring this to its own partial template whose only purpose is to render given category. And instead of manually counting columns, I prefer using array_chunk().

<?php namespace ProcessWire;
// inside /site/templates/home.php 
// (or whereever you're building your categories)
$category = $pages->get($catPageId);
$categoryItems = $pages->find("template=$template, limit=4, sort=random");
$rendered = wireRenderFile('partials/category', [
    'category' => $category,
    'categoryItems' => categoryItems,
    'colCount' => 4 // or whatever
]);
// then echo your rendered output anywhere
echo $rendered;




// Inside /site/templates/partials/category.php
$colCount = $colCount ?? 2; // if not provided, default col count is 2

// divide posts into arrays of $colCount items.
// you need standard PHP arrays, which you can get using getArray() method
$chunked = array_chunk($categoryItems->getArray(), $colCount);

?>
<style>
    /* separate your styles from markup, and even better, into its own CSS file */
    .item {
        margin-bottom: 10px;
        padding-top: 10px;
        padding-bottom: 10px;
    }
</style>
<div class="category">
    <!-- Render anything about category here -->
    <h2><a href="<?= $category->url ?>"><?= $category->title ?></a></h2>

    <!-- Then rows -->
    <?php foreach($chunked as $row):?>
        <div class="row">
        <!-- Then items inside rows -->
        <?php foreach($row as $item): ?>

            <div class="item col-sm-<?= $colCount ?>">
                <h3 class="item__title">
                    <a href="<?= $item->url ?>"><?= $item->title ?></a>
                </h3>            
                <p class="item__summary"><?= $item->summary ?></p>
            </div>

        <?php endforeach ?>

        <div class="row"><!-- remember to close your rows -->
    <?php endforeach ?>
</div>

Hope this was helpful. Ask away if you have further questions.

  • Like 1
Link to comment
Share on other sites

thank you for all processwire heros  ^-^. with your help i found the problem:

foreach ($subs->children as $tree_item) 

fixed like this:

foreach ($subs as $tree_item) 

proccesswire is magical and its community is wonderfull.

thanks again to you and its founder @ryan

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