Counting nested comments in the loop - php

While looping in get_comments() with these args:
$args = array(
'status' => 'approve',
'order' => 'DESC',
'number' => 5
);
I get more than five comments in the loop because of the nested comments.
I want to get the recent 5 comments even if they are parent or nested without disabling the nested feature from the admin panel.
For example: if the parent comment has 4 nested comments, i wanna just get it with it's nested and counted as 5 instead of counting other parents.

You could use array_slice to return the first 5 items from the returned wordpress array of comments:
$yourArray = $returnedCommentsArray;
$commentData= array_slice($yourArray, 0, 5)
Where returnedCommentsArray is the array of comments that is returned from your original wordpress call.

Related

PHP, loop through dynamic array by count

So I am working on a JQuery Step Form. It is being populated with dynamic information from the database. I have a particular list of items that is 21 items in the array. I would like to break these down into groups of 5 or 6 per step, so the form isn't so long.
I know I could use array_slice for example, but since this list is dynamic, I don't know how many there will always be. It will kind of be paginated in a sense but I don't need pagination links and such. Just a way to return 5 items, then 5 items and if the last iteration only has 2 items then that is it.
So for example:
$array = (
array(
"name" => "Peter Parker",
"alterego" => "Spiderman",
),
array(
"name" => "Clark Kent",
"alterego" => "Superman",
),
array(
"name" => "Bruce Wayne",
"alterego" => "Batman",
),
);
And then I want to break this down to only list 2 items per group.
You can use array_chunk for example:
$chunks = array_chunk($array, 5);
foreach ($chunks as $chunk) {
// display form for these 5 values
}
Andreas' initial advice is great. Maybe here, we might want to design some callback functions based on the pagination sizes that we wish to have:
array_slice($array, getChunkStart($array), getChunkLength($array));
or if we just look for a specific column size:
array_chunk($array, sizeof(array_column($array, "column_key")));

How to use array_merge_recursive with an incremental variable?

I created my array within a foreach loop like so ..
$final_orders[]= array($cake_type_size => array('size' => $cake_size , 'type'=>$cake_type[0]->name, 'qty' => $order_item['qty'] ));
Outside of that foreach loop I would like to use array_merge_recursive so all the keys that are the same will be merged together
This works ..
print_r(array_merge_recursive($final_orders[1],$final_orders[2],$final_orders[3]));
But I have n type of $final_orders I need some way to constantly increment and merge to lump the same keys together.

Return all records that match array of conditions using CakePHPs find

I'm currently attempting to return all records that match an array of conditions that I have. Currently I can get my code to work, but instead of returning all records that match an array of conditions that I've passed, it just returns the first one and then stops, instead of the four that I know exist in the table I'm accessing. This is with the all parameter set for find.
Here's the code snippet for a better view:
$array = implode(',', array('1','2','3','4'));
$a = $this->Assets->find('all', array(
'conditions' => array(
'id' => $array
)
)
);
var_dump($a);
var_dumping $a will just provide the record that has id 1, when there's records that exist for 2, 3, and 4 as well.
That is the expected result.
You are working against the ORMs auto-magic. Passing a string will result in an equality comparison, ie WHERE x = y, and since id is most probably an integer, the casting will turn the string 1,2,3,4 into 1, so ultimately the condition will be WHERE id = 1.
You should pass the array instead
'conditions' => array(
'id' => array(1, 2, 3, 4)
)
that way the ORM will generate an IN condition, ie WHERE id IN (1,2,3,4).
This is also documented in the cookbook:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

Retreving specific index from array in CakePHP

I have an array and I would to break it up with out using a foreach loop. I have a query like this:
$pending = $this->Article->find('all', array(
'conditions' => array('Article.status' => 'pending'),
'order' => array('Article.created' => 'desc'), 'limit' =>5
));
Now this brings me back the most recent 5 articles. What I would normally do here is just loop through them and display them, but I want to grab the specific fields for each index separately. How exactly do I do this?
I assumed it was something like:
<?php
echo $this->Html->link($pending['Article'['title'],array('controller'=>
'articles','action'=>'view',$pending['Article']['id'][0]));?>
Where I just add the index to the end of the field I want for that specific element. This doesn't return the right data. How can I do this?
UPDATE:
I was able to figure this out, to grab the specific index from an array in cakephp, you just put the index after your array variable so in this case it would be:
<?php
echo $this->Html->link($pending[0]['Article'['title'],array('controller'=>
'articles','action'=>'view',$pending[0]['Article']['id']));?>
This will go the the numbered index and allow you to grab any part of the array you would like.

Code Separation Paradox: Create HTML Tree from Multi-Dimensional Array AND Keep the HTML Outside of the Recursive Function

This working code seems to be the typical solution to this problem.
It takes a multi-dimensional array that holds categories and their subcategories (with no implied limitation on how many levels deep it goes) and creates an HTML unordered list from it, echoing it out onto the page from inside a recursive function.
Sub-levels are traversed by passing the value for each array element's 'children' key to array_walk() recursively, from inside the original callback function named _category_list()_.
How can this method of output be modified so that all HTML code would exist in the template, outside the function?
Here's the rundown of the code:
This multi-dimensional array holds the multi-level category tree.
Important keys to use in the HTML are 'category_id', 'name', and 'children'. Other keys have been purged from the array below for simplicity sake, but if they are useful they are: 'parent_id' and 'level' (starting with level 1).
<?php
// the array containing the tree
$categories = array (
'category_id' => '2',
'name' => 'Top Category Name',
'children' => array (
0 => array (
'category_id' => '188',
'name' => 'Category Name',
'children' => array (
0 => array (
'category_id' => '159',
'name' => 'Category Name',
'children' => array (),
),
1 => array (
'category_id' => '160',
'name' => 'Category Name',
'children' => array (),
),
2 => array (
'category_id' => '166',
'name' => 'Category Name',
'children' => array (),
),
),
),
1 => array (
'category_id' => '4',
'name' => 'Category Name',
'children' => array (
0 => array (
'category_id' => '141',
'name' => 'Category Name',
'children' => array (),
),
1 => array (
'category_id' => '142',
'name' => 'Category Name',
'children' => array (),
),
),
),
),
)
?>
.
This next function produces the majority of the HTML output, but it locks the HTML inside itself.
However, instead of echoing it right from the function, I'm looking for a way to pass this data back to the view template in a manner that is friendly for designers to customize.
<?php
// separate the HTML from this function,
// passing $v to the view template for handling
function category_list($v, $k){
switch ($k) {
case 'category_id':
echo "<li id="$v">";
break;
case 'name':
echo "$v";
break;
case 'children':
if(count($v) > 0){
echo "<ul>";
foreach($v as $k=>$v)
array_walk($v, 'category_list');
echo "</ul>";
}
echo "</li>";
break;
}
}
?>
.
The next block of code is the current template html/php with the call to traverse the first level of the array via array_walk() and referencing the recursive function above. The function itself then handles the recursion and iteration of deeper categories with 1 or more child. Of course, this is the typical approach.
This code should have all HTML tags, rather than just the outer tags.
<ul>
<?php array_walk($tree,'category_list'); ?>
</ul>
.
The Ideal Solution:
The end goal here is to find a way for template designers to create their ideal navigation structure without having to create or modify the recursion function (which isn't accessible), nor require use of a foreach loop for each level of the multi-dimensional array. The solution should not be tied to any specific depth limitations.
Examples of HTML customizations could range from placing additional attributes inside the ul/li tags, or even wrapping new tags around the output text, such as span tags, which are commonly used in navigations to achieve a sliding-doors effect with CSS. So I think the appropriate solution will need to support those case scenarios at a minimum.
Iterating through the array from the template using array_walk() would still be okay, as long as it can be used in such a way that the callback function passes the desired vars back to the template for use with the designer's HTML.
Ideally, if array_walk_recursive() knew how many levels deep its iterator actually is, I think this feat would be much easier to solve. But unless someone knows a workaround to that issue, the solution may be different entirely.
I also want to avoid using javascript methods of building the tree. And if there's a way to avoid using the switch, I'm open to suggestions there too.
Have you thought of writing a Class that manages and stores the info you want to pass back. You function could alter an instance of that class as it goes though and in the end pass back the filled object.
Your content would be encapsulated inside the class and you can write all the methods and utilities for the user to manipulate and output the data. Utility methods can also be written inside the class to show the number of levels, etc.
I haven't tried it myself but this is where I would start since the Class allows me to refine and expand on what my users would want and they wouldn't need to know the internal details of what was happening.

Categories