Fuelphp related limit - php

I am currenty working on a story board, and i upload images there telling a story.
So the problem is i just want to limit the related query.
The story board has a has_manyrelation for images. and on the list where the story boards appear i just only want to show the first image, and when someone clicks it it will show all, the show all is fine, but i can limit just the related images in my view.
And im totally confused with it because i read lot of infos about it in the forum, tried may variations, the limit is just ignored, or i get errors
so here is my code
controller
$stories = Model_Storyboard::find('all', array('related' => array('storyboardimage')));
$this->template->title = "Sotry Board | " . Config::get('site_name');
$this->template->content = View::forge('storyboard/index', array('stories' => $stories));
So how can i limit the storyboardimage to show the first image for each storyboard?
Sorry if im askin to much, but i would be really happy if someone could give me a working example beacuse i tried lots of variations and nothing works...

You can add criteria onto the relations, i.e. where, order_by, limit etc.
For example (note this is untested)
$stories = Model_Storyboard::find('all', array(
'related' => array(
'storyboardimage' => array(
'limit' => 1,
'order_by' => array(
'field_indicative_of_first_image' => 'desc'
),
)
)
));
In the example above I've added in field_indicative_of_first_image which simply means replace this with a column which you can use to get the first image. This might be for example a created_at column or a weight or sort_order column. Without knowing your database schema, I can't tell you which you'd need.

Related

Cakephp - Getting items from next page in current page?

I'm currently working on a gallery system using cakephp. In it, we have paginated each image album so each page contains a set number of images at most. I have attained this by using:
$this->paginate = array(
'conditions' => array(
'Item.album_id' => $id
),
'order' => array(
'Item.added' => 'desc'
),
'limit' => '50'
);
On my view controller. With this, I can show all the items on each page with no problems.
I'm currently, however, facing a single issue: I need to show, after all the items in the current page, a button that leads to the next page. This is not a problem, except that by design the button that says NEXT PAGE should have an item from the next page as its background.
I have looked around, trying to figure out a way to pull an item from the next page of a paginated system in cakephp without luck. Has anyone done this, or does anyone have a clue how to go about it?
You have to query the preview image manually in the current page.
To access the page number you can use $this->params in the action. Then you have to query images with 'limit' and 'page' parameters as mentioned in the documentation.
After that set the image URL like this:
$this->set('preview_image_url', $queried_url);
Then in the view use inline styling to set the background for the next button.
With Alto's help, I ended up doing this (Putting it here just in case anyone wonders exactly how I did it):
$CurrPage = $this->params['paging']['Item']['page'];
$NextParams = array(
'limit' => 1,
//'page' => $CurrPage + 1,
'order' => array(
'Item.added' => 'desc'
),
'offset' => $CurrPage*50
//'order' =>('rand()')
);
$NextImage = $this->Item->find('first', $NextParams);
$this->set('NextImage', $NextImage);
This gives me the first item from the following page, which is good enough for me. Note the commented 'order' =>('rand()') - I tried to grab a random item from the following page but, alas, it seems like Cake first scrambles all items and THEN paginates. There's probably a way around this, but for now this one did it for me.
Also, using 'page' => $CurrPage + 1 didn't seem to work, with the system instead returning a seemingly randomly chosen image. That's why I instead defaulted to using 'offset' => $CurrPage*50, where 50 is the amount of items per page I'm showing.

Trying to filter bigcommerce results by category

My bigcommerce code for filtering begins like this:
for($x=1;$x<$count;$x++){
$filter = array('category' => 54, 'limit' => 200, 'page' => $x);
$products = Bigcommerce::getProducts($filter);
This doesn't work quite right, and I'm not sure if it is meant to.
Here is the bigcommerce api in php.
Can anyone tell me the correct way to pull only the results that have a category id of 54 using $filter?
$filter = array("category" => 54, "limit" => 200);
$products = Bigcommerce::getProducts($filter);
foreach ($products as $product) {
//do something with results
}
I am not sure about the page option you used but this is the code that I use to filter products by category id. It should work the same adding in the page as well. You can also use a variable for the category id something like $_POST['catid'] where 'catid' comes from html page or from url: category.html?catid=54. This is how I display products in my store when no subcategories exist for a certain category id it will use that cat id to find all the products in that category. In my for each loop it creates the html for the list of products. You can use $product->name, etc. to access the product resources available and use html that is sent back to your main page using java script. Let me know if you have any other questions I had a hard time finding any help on this when I went through it so I am more than happy to help if I can. I hope this helps.

OpenCart categories are not showing up

The categories have:
The "top" option marked already
They're assigned to a store
They're assigned to a manufacturer
And they're still not being shown. I don't think this is a code issue as it was working just fine with the default products/categories and I haven't touched the code yet.
I've researched a lot, but people always say that marking the "Top" option should solve the issue.
Please take a look into the below screenshots.
http://postimg.org/image/yv6p186kf/
http://postimg.org/image/wcl00jku7/
postimg.org/image/pbx07rj27
Thanks
Edit:
I'm using OpenCart v1.5.6
Edit 2:
It is a parent category.
For those who are still researching this, please read my answer.
It seems to be an OpenCart bug.
Here is what I've done to fix the issue:
Instead of leaving the "Parent" field as blank, try to type a invalid name in the field so it will show the -- None -- option, select it and click on save. It solved the problem in my case.
Please see the screen shot.
http://i.stack.imgur.com/WWPBY.png
Do the following steps:
Open and edit your category:
In tab Data:
Find and Check:
Top: Display in the top menu bar. Only works for the top parent
categories.
I was not getting the child categories in the form field so I did this.
Create the category without parent
Add this category to the products that you want.
Now edit the category and add the parent to it.
The component is written to return just 5 top results, the issue arises after there is a categories overload
to Fix the issue goto file
/admin/controller/catalog/category.php
function autocomplete()
{
$json = array();
if (isset($this->request->get['filter_name']))
{
$this->load->model('catalog/category');
$filter_data = array(
'filter_name' => $this->request->get['filter_name'],
'sort' => 'name',
'order' => 'ASC',
'start' => 0,
'limit' => 5
);
$results = $this->model_catalog_category->getCategories($filter_data);
foreach($results as $result)
{
$json[] = array(
'category_id' => $result['category_id'],
'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
);
}
}
$sort_order = array();
foreach($json as $key => $value)
{
$sort_order[$key] = $value['name'];
}
array_multisort($sort_order, SORT_ASC, $json);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
and change limit from 5 to a higher number, preferably 20 in
$filter_data = array(
'filter_name' => $this->request->get['filter_name'],
'sort' => 'name',
'order' => 'ASC',
'start' => 0,
'limit' => 20
);
Here is the issue I ran into. When you are in the admin area and trying to select the category that your product belongs to... Make sure you fully click. I dont have a mouse, so if you have a mouse or trackball this may not address your issue. My Asus Laptop finger pad allows me to lightly tap or fully click the pad to navigate. I had to make sure I clicked my category choices. If I only tapped the pad lightly it pretended to make a selection and the category would not register. So my issue was probably related to a javascript/hardware quirk.
Wasted an hour with that one.... LOL.
Hope this helps someone else. I am using Opencart version 3.0.3.8
The powers that be at stack exchange may not have the same hardware as me or some of us. Please dont flag this post because you don't understand this post. This will help someone else. These people think they know everything. And probably filter out some good answers.

How do I add another attribute to order_aggregated_created table?

I’ve been scratching my head for days on this one. Any help or a push in the right direction would be greatly appreciated.
I’ve extended the sales report under Reports->Sales->Orders and created my own custom filters to filer the report by channels.
Each order has a ‘channel_name’ attribute to identify whether the order came from eBay, Amazon, etc.
Now I cannot for the life of me figure out how sales/order_aggregated_created table that is used to generate the reports is created. Where does the magic happen? I want to add ‘channel_name’ to the order_aggregated_created table to be able to filter by this attribute, but I cannot figure out how to this.
Diagram for order_aggregated_created table with its attributes:
http://www.magento-exchange.com/wp-content/uploads/2010/11/MAGENTO-SALES-ORDER-ER.png
Mage_Sales_Model_Resource_Report_Order_Collection is where the magic starts in retrieving the sales totals, particularly if I understood this correctly inside
protected function _getSelectedColumns(){...}
if (!$this->isTotals()) {
$this->_selectedColumns = array(
'period' => $this->_periodFormat,
'orders_count' => 'SUM(orders_count)',
'total_qty_ordered' => 'SUM(total_qty_ordered)',
'total_qty_invoiced' => 'SUM(total_qty_invoiced)',
'total_income_amount' => 'SUM(total_income_amount)',
'total_revenue_amount' => 'SUM(total_revenue_amount)',
'total_profit_amount' => 'SUM(total_profit_amount)',
'total_invoiced_amount' => 'SUM(total_invoiced_amount)',
'total_canceled_amount' => 'SUM(total_canceled_amount)',
'total_paid_amount' => 'SUM(total_paid_amount)',
'total_refunded_amount' => 'SUM(total_refunded_amount)',
'total_tax_amount' => 'SUM(total_tax_amount)',
'total_tax_amount_actual' => 'SUM(total_tax_amount_actual)',
'total_shipping_amount' => 'SUM(total_shipping_amount)',
'total_shipping_amount_actual' => 'SUM(total_shipping_amount_actual)',
'total_discount_amount' => 'SUM(total_discount_amount)',
'total_discount_amount_actual' => 'SUM(total_discount_amount_actual)',
);
}
If would be awesome if I can just ‘channel_name’ =>$this->_channelName, and be on my merry way.
After a lot and lot of research it turns out that sales/order_aggregated_created table gets generated in here:
Mage_Sales_Model_Resource_Report_Order_Createdat
now I've looked here before seemed like exactly what I needed, but any changes I made would not reflect in Magento reports especially inside the sales/order_aggregated_created table.
I found out that Mage_Sales_Model_Resource_Report_Order_Createdat only gets called when you refresh the statistics inside Reports->Sales->Orders, only then a NEW sales/order_aggregated_created table is generated! So for anyone looking to filter the order sales report by a custom attribute, look inside: /app/code/core/Mage/Sales/Model/Resource/Report/Order/Createat.php

Trying to add records to a table with MYSQL and php, with a forced unique identifier

I am trying to modify a script that was developed to import article records from a Joomla (1.5.x) database into a Wordpress 3.2.1 table for posts. It is a script that migrates content from Joomla to Wordpress.
The issue I had with the script is that it did not maintain the unique identifier ('id' in Joomla, and 'ID' in Wordpress). Based on my understanding, this makes it a lot more complicated (much more work) to deal with redirecting all the Joomla permalinks over to the new (and obviously different) Wordpress permalinks. If the ID was the same in WP as it was in Joomla then some basic rewrite rules in htaccess would be enough to perform the redirections.
So I want to see if I can modify the code to force the ID rather than it being generated in consecutive order as records are inserted into the table.
The script I am modifying is available here: http://wordpress.org/extend/plugins/joomla-to-wordpress-migrator/
The file in question is called: joomla2wp-mig.php
The array is being created at around line 1049 and 1081.
At line 1049 it is:
$wp_posts[] = array(
'ID' => $R->id, //I ADDED THIS IN
'post_author' => $user_id,
'post_category' => array($wp_cat_id),
'post_content' => $post_content,
'post_date' => $R->created,
'post_date_gmt' => $R->created,
'post_modified' => $R->modified,
'post_modified_gmt' => $R->modified,
'post_title' => $R->title,
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_name' => $R->alias,
'tags_input' => $R->metakey,
'post_type' => 'post'
);
And at line 1081 it is:
$array = array(
"ID" => $item['ID'], //I ADDED THIS IN
"post_author" => $user_id,
"post_parent" => intval($wp_cat_id),
"post_content" => $item['post_content'],
"post_date" => $item['post_date'],
"post_date_gmt" => $item['post_date_gmt'],
"post_modified" => $item['post_modified'],
"post_modified_gmt" => $item['post_modified_gmt'],
"post_title" => $item['post_title'],
"post_status" => $item['post_status'],
"comment_status" => $item['comment_status'],
"ping_status" => $item['ping_status'],
"post_name" => $item['post_name'],
"post_type" => $item['post_type']
);
I have commented the ID line which I have added into the top of each of these bits of array code.
The INSERT command is being implimented around line 1097
The INSERT command is put together like this:
$insert_sql = "INSERT INTO " . $j2wp_wp_tb_prefix . "posts" . " set ";
$inserted = 0;
foreach ($array as $k => $v)
{
if($k AND $v)
{
if($inserted > 0)
$insert_sql .= ",";
$insert_sql .= " ".$k." = '".mysql_escape_string(str_replace("`","",$v))."'";
++$inserted;
}
}
$sql_query[] = $insert_sql;
}
It uses the MYSQL function INSERT INTO... SET (as opposed to INSERT INTO... VALUE)
The challenge I have is this:
The array did not include the ID, so I have added this in.
Having made this change, when I run the script it will appear (at the Wordpress UI end) to run fine, but no records are inserted, even though it says it was successful.
I found I could get around that issue by setting up a fresh wp_posts table with X number of blank records. Let's say I am importing 100 articles, then I would put 100 records into the table, and they would have ID 1 to 100. When I run my modified code it will happily update and populate these existing records. What I don't understand is why it will not create new records when I force the unique identifier (ID) to what I want it as.
I am wondering if I need to use the INSERT INTO... VALUE command instead of INSERT INTO... SET
I was going to test that out, but to be honest I am not a programmer and am just winging it as I go along. So I had not idea how to rewrite the PHP in order to impliment the structure required for the VALUE command in place of SET.
Any help or suggestions would be greatly appreciate.
I gather I have to rewrite the last bit of code I provded above.
There is some discussion on this matter at the wordpress support forums. One user (spiff06) very kindly helped troubleshoot the issue with me. We came unstuck around getting the code to insert new records with a forced identifier, and I went with what we referred to as the "messy" option (which is the method I mentioned above... setting up a table with the required number of blank records).
Even though I've used that "messy" method for my own site, it is my wish to make this process work cleanly for other users who are on Joomla 1.5.x and are switching to WP instead of upgrading to a newer Joomla release (which is a big process, so many are just jumping ot WP, like me).
With much thanks...
Jonathan
You can try the following:
Change the structure of the imported mysql table (post#wordpress). Change the id field so it is not anymore an autoincrement field. Let it being just an integer field.
Import the values. This way you can put any values in the field ID without limitations at all.
After the importation change again the structure of the table to set the ID field to be again an autoincrement field.
I never found a truly automated / scripted way of doing this. I ended up doing a workaround:
For now I've imported all my posts the "messy" way, by prepopulating the table.
THE WORKSROUND METHOD
Prepopulate the wp_posts table in the WP database with as many records as you require (look in Joomla to see how many records you have). I had 398, so I added 398 records to wp_posts.
HOW? I did it by exporting the emtpy wp_posts table to a .csv file. I then opened this in Excel (Numbers, or OpenOffice would also do). In the spreadsheet application it was easy to autofill 1 to 398 in the ID column.
I then reimported that .csv file into wp_posts. This gave me a wp_posts with 398 record rows with 1 to 398 in the ID field.
I then ran version 1.5.4 of Mambo/Joomla to WordPress migrator, which can be installed from within WordPress.
End result?
All posts have the same ID as the original Joomla articles.

Categories