kohana get next elements of a collection - orm

I have a collection of elements :
$products_asc =Model::factory('product')->sale($sale_id)->category($category_id)->order_by('product_id', 'asc')->find_all();
and I want to make a next-previous navigator into this elements. (of course, for getting the previous ones I will have a $products_desc ordering)
My problem is: I want to make a function having the signature:
public function get_next_product($category, $sale, $id_product)
that will always give me the next product id. but I don't know how to do it. How exactly can i get the next element of the collection having only all the elements sorted ascending by the :
$products_asc =Model::factory('product')->sale($sale_id)->category($category_id)->order_by('product_id', 'asc')->find_all();

i have found the solution - for those who may also be in need:
$previous = Model::factory('product')->sale($sale_id)->order_by('product_id','desc')->where('product_id', '<', $id)->find();
$next = Model::factory('product')->sale($sale_id)->where('product_id', '>', $id)->find();

Database_Result is a kind of Iterator object. Why dont use next($products_asc) and prev($products_asc) functions?

Related

How to implement Linked List Recursion in KOtlin

I am learning linked list and came across a problem where you are required to reverse a linked list using recursion. Here is the code I wrote:
fun reverseRecurseTraverse1(firstNode: Node<Int>?): Node<Int>? {
if (firstNode?.next == null) {
return firstNode
} else {
val finalNode = reverseRecurseTraverse1(firstNode.next)
finalNode?.next = firstNode
firstNode.next = null
return finalNode
}
}
Input:
01234
Output:
40
I get the desired output If I change the line from
finalNode?.next = firstNode
to
firstNode.next!!.next = firstNode
What am I doing wrong here?
When I try to whiteboard finalNode?.next = firstNode makes perfect sense to me and based on my understanding those two lines are essentially doing the same thing.
Kindly help me understand this.
It would be useful if you share your "whiteboard logic" and why do you assume finalNode?.next = firstNode is correct and functionally the same as firstNode.next!!.next = firstNode.
Let's get your example: 01234. In the first step firstNode is 0 and after reversing of the remaining sublist, we get 4321. finalNode points at the first element of this reversed sublist, so at 4. Then you add 0 after 4 which is wrong. You should add 0 after 1.
On the other hand, firstNode.next is still pointing at the item that was next to 0 in the original list. It points at 1. By assigning to its next we add 0 where it should be - after 1.
You are almost there, but one line is wrong.
What you are doing is:
If your item is null, return it. That seems okay.
If your item is already the last item, return it as first item. Also looks okay.
If your item is not the last item, reverse the rest of the list. Okay.
Take the old last and now first item, and append your old first now last item to it? That doesn't seem right. Don't you want to append your old first and now last item to the last item of the reversed rest?
It would probably help to write a function
fun Node.last(): Node? = if(next == null) this else next!!.last()
that provides the last item of a linked list.
You could use that to append your previously first item to the last item of the reversed rest.
Considering a call reverseRecurseTraverse1(x), can we maybe always tell what reverseRecurseTraverse1(x).last() is without calculating it?

Dgrid selection mix-in issue with order

I've been having issues with the dgrid selection mix-in with multi selects.
Using the selection property (for example)
var selected = Object.keys(datatable.selection)
it returns an array of row ids as expected. However the ORDER of those ids seems to be "arbitrary". It seems perhaps that the order of selecting has an affect.
In any event, in the datatable, I want the selected rows to be returned in order that they display in the list, and they do not.
I can get them in the proper order using dojo.query(".dgrid-selected", datatable.domNode), and use the HTML element to get the row data, but this seems like a hack.
I cannot find a proper method to do this on the SitePen docs. Anyone?
I don't think that there is a direct way to do that. The Object.keys(datatable.selection) returns the array of ids in the order in which the rows are selected. You can use some built-in functions of d-grid and JS to achieve this. Below are the steps:
Get the id by Object.keys.
var selected = Object.keys(datatable.selection)
Create a list of objects comprising of id and rowIndex of element
Code:
var dataList= [];
for(var i=0; i< selected.length; i++){
dataList.push({id: selected[i], index: datatable.row(selected[i]).element.rowIndex});
}
Sort the list using index as the attribute:
dataList.sort(function(a, b){ return a.index- b.index; })
The resulting dataList would have the list of objects in order in which they appear in the grid.

PDO fetchColumn() and fetchObject() which is better and proper usage

It's been bugging me, I have a query which returns a single row and I need to get their corresponding column value.
//Retrieve Ticket Information to Database
$r = db_query("SELECT title, description, terms_cond, image, social_status, sched_stat FROM giveaway_table WHERE ticket_id = :ticket_id",
array(
':ticket_id' => $ticket_id
));
There are two ways that I can get data which is, by using fetchColumn() and fetchObject()
fetchObject()
$object = $r->fetchObject();
$ticket_info[] = $object->title;
$ticket_info[] = $object->description;
$ticket_info[] = $object->terms_cond;
$ticket_info[] = $object->image;
$ticket_info[] = $object->social_status;
$ticket_info[] = $object->sched_stat;
fetchColumn()
$title = $r->fetchColumn() //Returns title column value
$description = $r->fetchColumn(1) //Returns description column value
Was wondering, which one is better, or are there any pros and cons about this stuff?
if possible, can you guys also suggest the best way (if there's any) on how to retrieve all columns that's been selected in a query and store it into an array with less line of code.
There are two ways that I can get data which is, by using fetchColumn() and fetchObject()
really? what about fetch()?
There is a PDO tag wiki where you can find everything you need
I don't know pros and cons of using it. In my project I often used fetching as array rather than object. It was more comfortable. But if you make ORM projects then maybe it would be better to use fetchObject and make it your object not a std_class. You could make a contructor that has one parametr which is stdClass and make your object from this class
Answering your other question you can fetch all columns using fetchAll();
Follow this link to learn more about this function http://www.php.net/manual/en/pdostatement.fetchall.php
More about abstract database layer you can find here -> http://www.doctrine-project.org/

Magento - How do I exclude a category from product collection?

I'm trying to create some functionality that allows me to arbitrarily show a collection of products based on criteria such as Category and Attribute values and have this working as intended, however I'd also like to be able to exclude certain Categories so that I could then include the 'Default Category' and then exclude any sub categories I don't want. There doesn't appear to be a method such as the addCategoryFilter($catid) for this.
I found a similar question that had been answered but couldn't get it to work for me.
$catId = 9;
/* I'm almost positive 'e' is the alias used for catalog_product_entity, check your
query with echo (string) $products->getSelect(); if it doesn't work */
$products->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id');
$products->getSelect()->where('cats.category_id', array('neq' => $catId));
Has anyone else had any experience of doing this kind of thing?
I don't think there is a native way of doing it. But you can easily make a quick module that extends
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
and then you can create your own method, and change the joinCondition to suit
Check my solution:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addUrlRewrite();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);
$_productCollection->load();
$_productCollection->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id');
$_productCollection->getSelect()->where('cats.category_id not in (41)');

JMeter regex extractor forEach controller

I'm creating some tests with JMeter, the situation is very simple, I have a search page with a list of results, and I have to retrieve from this results some values to use in the next request.
Those results are around 350, I don't need them all.
I used the RegexExtractor to retrieve those results and it works (I setted it to retrieve just 10 results), but now I don't know how to access the results inside a LoopCounter.
The extractor put the results into a variable named Result.
The problem is that I don't know hot to build dinamically the name of a variable.
Do I have to use some function like _p()??
I can access the variable just putting the static name Result_0_g1
Inside the LoopCounter I putted also a Counter to store the loop count into the variable index
Thank you
EDIT:
SOLVED I have to write:
${__V(Result_${index}_g1)
You have to reference the variable with the function:
${__V(Result_${index}_g1)
...Just for collection.
See also this post for another implementation (case without using ForEach Controller):
ThreadGroup
HttpSampler
Regex Extractor (variableName = links)
WhileController(${__javaScript(${C} < ${links_matchNr})})
HTTPSampler use ${__V(links_${C})} to access the current result
Counter (start=1, increment=1, maximum=${links_matchNr}, referenceName=C)
Use the ForEach Controller - it's specifically designed for this purpose and does exactly what you want.
You may use ForEach Controller:
ThreadGroup
YourSampler
Regular Expression Extractor (match -1, any template)
Foreach controller
Counter(Maximum -> ${Result_matchNr} , Rf Name -> index)
LinkSamplerUsingParsedData(use -> ${__V(Result_${index}_g1)}
Now, if you want to iterate to all groups, you need another foreach to do that. As you know which group represent what value, you can use this way.