Trouble including array output in another array - php
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)
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' );
how to make json string by using php array?
This is the target jason string: {"dpid": 272, "priority": 10, "match": {"nw_src": "192.168.1.1", "nw_dst": "192.168.1.2", "nw_proto": 1, "eth_type": 0x0800}, "actions":[{"type": "DROP"}]} This is the php array that i made: $rule = array( "dpid" => 272, "priority" => 10, "match" => {"nw_src": "$_POST['src']", "nw_dst": "$_POST['dst']", "nw_proto": 1, "eth_type": 0x0800}, "actions"=> [{"type": "DROP"}], ); i am trying to turn this array into the json string above, using: $data_string=json_encode( $rule ); but it doesnt work :( i know the array is really non-sense, i am really new to php. Could somebody help me?
Your array should be: $rule = array( "dpid" => 272, "priority" => 10, "match" => array( "nw_src" => $_POST['src'], "nw_dst" => $_POST['dst'], "nw_proto" => 1, "eth_type" => 0x0800 ), "actions"=> array(array("type" => "DROP")), ); After that json_encode function will do all the work for you: $data_string = json_encode($rule);
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))))
Pagination without default order
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
php table of values
I have a table of values like this: http://www.conversiontable.org/clothingsizeconversiontable.html and I want to declare these values in a php class and then manipulate them easily. In your opinion, what is the best way to do such thing?
You can build an array indexed by country name and then by size number: $country_sizes = array( 'United States' => array(6, 8, 10, 12, 14, 16, 18), ... ) But in order to avoid mistakes and make the code more readable, I would assign a label to each size type. Then I would build an array indexed by size type first, and then by country: $sizes = array( 'S' => array( 'United States' => 6, 'United Kingdom' => 28, ... ), 'M' => array( 'United States' => 8, 'United Kingdom' => 30, ... ), ... ); This second way is more tedious to build, but seems more natural to me (what's the S size in U.K.?). Anyway, it's your choice according to your needs ;)
something like: $sizes = array( 'United States' => array(6, 8, 10, 12, 14, 16, 18), 'United Kingdom' => array (28, 30, 32, 34, 36, 38, 40) ); echo('United states first size: ' . $sizes['United States'][0]);