Pagination without default order - php

I have the code below:
$firm_ids = array(81, 96, 18, 5, 105);
$this->paginate = array(
'conditions' => array('Firm.id' => $firm_ids),
'limit' => 10,
);
$this->set('firms', $this->paginate('Firm'));
In the results I have the ordering in:
5, 18, 81, 95, 105
How do I disable the default order if I want to order as initial array ordering?

I found a workaround on some forum, see if this works:
$this->paginate = array(
'conditions' => array('Firm.id' => $firm_ids),
'limit' => 10,
'order' => array(
'FIELD(Firm.id,' . implode(',',$firm_ids) . ')'
)
);

what you need is order by field. here's the link.
Order By Field

Related

PHP via shortcode always on top of page

I got a plugin for wordpress to show the most popular posts...
But when I add it via shortcode it always gets to the top of the page and not where I placed it... I changed every echo in the plugin PHPs to return but it didn't helped... Here is my shortcode in functions.php:
function top_news(){
$args = array(
'limit' => 15,
'range' => 'daily',
'freshness' => 1,
'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'stats_author' => 1,
'stats_date' => 1,
'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
'wpp_end' => '</table>',
'stats_date_format' => 'd',
'excerpt_by_words' => 1,
'excerpt_length' => 35,
'title_length' => 66,
'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
<summary>{title}<span class="plus">+</span></summary>{summary}<br>Weiterlesen</details></td><td class="views">{views}</td></tr>'
);
wpp_get_mostpopular( $args );
return $args;
}
add_shortcode( 'topnews', 'top_news' );
Do you know what I can do?
Thanks,
Till
Reading documentation of wpp_get_mostpopular, it states that the function actually prints the popular posts. Which means your popular posts are printed out before it returns anything and since all shortcodes are processed before the posts content is being printed that's why your popular posts always prints before (at top) of the post content.
So, what you can do is catch all the popular posts in a buffer.
function top_news(){
$args = array (
'limit' => 15,
'range' => 'daily',
'freshness' => 1,
'order_by' => 'views',
'post_type' => 'post',
'stats_views' => 1,
'stats_author' => 1,
'stats_date' => 1,
'wpp_start' => '<table class="topnachrichten"><tr><th>Datum</th><th>Top Nachrichten von Heute</th><th>Leser</th></tr>',
'wpp_end' => '</table>',
'stats_date_format' => 'd',
'excerpt_by_words' => 1,
'excerpt_length' => 35,
'title_length' => 66,
'post_html' => '<tr><td class="datum">{date}. Aug</td><td class="stext"><details>
<summary>{title}<span class="plus">+</span></summary>{summary}<br>Weiterlesen</details></td><td class="views">{views}</td></tr>'
);
ob_start();
wpp_get_mostpopular( $args );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'topnews', 'top_news' );

PHP MongoDB find with multiple "AND" conditions?

After hours of experimentations and readings, I cannot find a solution to this problem:
I want to do a MongoDB->find($query) with multiple AND conditions.
For instance, say I want id = 5 and a < 6 and a > 2 and b > 10 and b < 20
I was expecting $query to be:
$query = array("id" => 5,
"a" => array('$gt' => 2,
'$lt' => 6),
"b" => array('$gt' => 10,
'$lt' => 20))
But this returns empty results with my DB
I tried various syntaxes such as:
$query = array("id" => 5,
array( "a" => array('$gt' => 2,
'$lt' => 6),
"b" => array('$gt' => 10,
'$lt' => 20)))
But this fails too.
Also tried with "$AND" variants, no luck.
Is it possible to "mix" several AND conditions in PHP-MongoDB find() requests?
I've just tested this using MongoDB PHP driver v1.6.11 (PHP-5.5.9). The test data are as below
db.collection.insert({id:5, a:4, b:15})
db.collection.insert({id:9, a:4, b:15})
db.collection.insert({id:5, a:4, b:20})
Using PHP code snippet:
$condition = array(
'$and' => array(
array(
"id" => 5,
"a" => array('$gt' => 2, '$lt' => 6),
"b" => array('$gt' => 10, '$lt' => 20)
)
)
);
$docs = $coll->find($condition);
foreach( $docs as $o=> $doc) {
echo json_encode($doc);
}
The above returns only the first document sample. This indicates that $and should work as expected. I've also tested without $and, i.e. :
$condition = array(
"id" => 5,
"a" => array('$gt' => 2, '$lt' => 6),
"b" => array('$gt' => 10, '$lt' => 20)
);
Which also works the same. Try checking your dataset, whether there is a document matching your criteria.
This issue is closed: bad value types in the DB (string instead of float/double). Works as expected when updating to correct types in DB.

Add condition to join table in pagination query (CakePHP 2.0.4)

I am new to cakephp. I have to do some improvements in the cakephp based application. In that, there are categories and images models. In the images table has lots of images. Default category images and other category related images details are maintaining in that table. It will display following queries in the 2nd page (pagination) of list categories.
SELECT `Category`.`id`, `Category`.`name`, `Category`.`slug`, `Category`.`meta_description`, `Category`.`meta_keywords`, `Category`.`sort`, `Category`.`color`, `Category`.`bgcolor`, `Category`.`modified`, `Category`.`created` FROM `categories` AS `Category` WHERE 1 = 1 ORDER BY `sort` ASC LIMIT 20, 20
SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)
But actually, the second query needs to be like:
SELECT `Image`.`id`, `Image`.`path`, `Image`.`size`, `Image`.`tags`, `Image`.`default`, `Image`.`category_id`, `Image`.`modified`, `Image`.`created` FROM `images` AS `Image` WHERE `Image`.`default` = 1 AND `Image`.`category_id` IN (60, 17, 9, 33, 71, 18, 73, 30, 58, 54, 3, 44, 64, 66, 67, 11, 53, 16, 23, 68)
There should be
Image.default = 1
in WHERE clause.
My CategoriesController.php index action is:
public function index() {
$this->Category->recursive = 1;
$this->Category->Behaviors->attach('Containable');
$this->paginate = array("contain"=>array("Image"));
$search="";
if(isset($this->params['named']['search'])){
$search = str_replace("%20"," ", $this->params['named']['search']);
$conditions = array(
'OR' => array(
array('Category.name LIKE' => '%'.$search.'%')
)
);
$this->set('categories', $this->paginate($conditions));
} else {
$this->set('categories', $this->paginate());
}
$success = $this-> Session->read('success');
$this->set(compact('search', 'success'));
}
And my Category model has :
public $hasMany = array(
'Panel' => array(...),
'Image' => array(
'className' => 'Image',
'foreignKey' => 'category_id',
'dependent' => false,
'conditions' => array('Image.default' => '1'),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
So, I need to filter the join table images.default set to '1'
In your categories controller change this line:
$this->paginate = array("contain"=>array("Image"));
to:
$this->paginate = array("contain"=>array("Image.default = 1"));
More info here: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations
Note: default recursive level is 1
Combine both condition into 1
$this->paginate = array("contain"=>array("Image.default = 1"),"recursive" => 1);
or make multiple conditions
$this->paginate = array('contain'=>array('Image'=>array('conditions'=>array('Image.default'=>1))))

Trouble including array output in another array

I have the following code:
$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;
Which echos out as...
10,121,20,36,23,66,24,21,105,76,82,17,22,122,43,47,102,5,6,106,8,75,54,38,57,86,56,101,123,95,25,62,16,39,40,69,37,9,42,7,15,41,87,73,85,104
This is great. However, I actually want to include the result (which I guess is $tagString) in another array as follows...
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
'tag__not_in' => array ($tagString)
);
I've tried removing brackets adding single / double / no quotes, removing the word 'array' in front of $tagString across all combinations but it just doesn't work. When I manually create...
'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)
The code works perfectly. How can I get the output from $tagString to be the content of 'tag__not_in' array within the brackets? Is this possible?
========
Update to reflect Amal's code...
$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
/*'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)*/
'tag__not_in' => explode(',', $tagString)
);
$tagString is a string - simply inserting it in array(...) won't create an array (unless you use eval(), which is generally a bad idea). Simply use explode() to create an array from the comma-separated string, like so:
'tag__not_in' => explode(',', $tagString)

CakePHP this->paginate not using order field

No matter what I do I can't get it to respect the order I specify.
$this->paginate = array(
'Car' => array(
'limit' => 6,
'order' => array(
'Car.year' => 'desc'
),
'table' => 'cars'
)
);
Generated SQL:
SELECT `Car`.`id`, ... `Car`.`year`,... FROM `cars` AS `Car` WHERE 1 = 1 LIMIT 6
Turns out I was using a :sort in my url parameters. Once I took that out all was well :)
If you only have one item, you don't need/shouldn't use an array:
//one thing
var $order = "Model.field DESC";
//multiple things
var $order = array("Model.field" => "asc", "Model.field2" => "DESC");
(per this page)
Note too that virtual fields are ignored by default when paginating.
See "Control which fields used for ordering" in Cake 2.0 Pagination documentation.
Example:
$this->MyModel->virtualFields['count'] = 0;
$this->Paginator->settings = array(
'fields' => 'COUNT(id) AS MyModel__count',
'group' => ('MyModel.group_id'),
'order' => array('MyModel__count' => 'DESC'),
);
// IMPORTANT: pass sortable fields including the virtual field as 3rd parameter:
$log = $this->Paginator->paginate('MyModel', null, array('MyModel__count', 'id'));

Categories