foreach loop on multidimensional array - multidimensional-array

how could I make a nice search result like google
I cannot wrap my head around this many. thanks for any help you can give.
Array
(
[summary] => Array
(
[what] => pizza
[where] => city
)
[listings] => Array
(
[0] => Array
(
[parent] =>
[contents] => Array
(
[Video] => Array
(
[avail] =>
)
)
[id] => 1114638
[name] => Sexy house
[address] => Array
(
[street] => 3 King St E
[city] => loversLane
[prov] => AB
[pcode] => L8N1A1
)
[geoCoded] => Array
(
[latitude] => 43.256373
[longitude] => -79.868167
)
)
)
)
this works good at printing:
function recursivePrint($elem) {
foreach ($elem as $key => $value) {
if (is_array($value))
$this->recursivePrint($value);
else
print $value.'<br>';
}
}
But I want to be able to place links over the results etc. have the geocode as variables so I can use maps. Just as much control with as little lines as possible.

The HTML you choose to wrap round each one is up to you, but this should make it obvious how you use arrays in PHP:
echo 'Results for '.$elem['summary']['what'].' '.$elem['summary']['where'].'<br />';
foreach($elem['listings'] as $listing)
{
echo $listing['name'].'<br />';
echo $listing['address']['street'].'<br />';
echo $listing['address']['city'].'<br />';
echo ''.$listing['address']['pcode'].'';
echo '<br /><br />';
}

Related

WP - get_template_part based on matching taxonomy term(s)

I'm trying to display some snippets (loaded via get_template_part()) based on a custom taxonomy terms assigned to a post.
So far I can get taxonomy terms assigned to the post with
$term_list = wp_get_post_terms($post->ID, 'sidebar_snippets', array("fields" => "all"));
print_r($term_list);
Which produces an array of objects like that:
(
[0] => WP_Term Object
(
[term_id] => 11
[name] => Future Events
[slug] => future_events
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => sidebar_snippets
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)
I was thinking of iterating over an array of assigned terms and load the appropriate snippets. The snippets' names are identical with the 'slug' of taxonomy term.
$term_list = wp_get_post_terms($post->ID, 'sidebar_modules', array("fields" => "all"));
print_r($term_list); // works fine - outputs three terms (like above)
foreach($term_list as $term) {
echo $term['slug']; // does not out put anything.
get_template_part( 'modules/' . $term['slug] . '.php' );
}
I have two problems. One that it doesn't not even output the $term[slug]. Secondly, how would I add some validation, eg. check if the file exists first before trying to get_template_part?
Thank you
You are trying to access object value as array so it not echo the value. Use the below code for echo correctly.
foreach($term_list as $key => $term) {
$term_slug = $term->slug; // does not out put anything.
get_template_part( 'modules/'.$term_slug.'.php' );
}
For more help see this link : Click Here
Thanks

Hide specific category in listings

I'm working on a Wordpress theme. The theme is Classifieds theme from premiumpress. The theme has a shortcode to list all the listings. The corresponding shortcode is [LISTINGS].
The function for the shortcode is as follows
/* =============================================================================
[LISTINGS] - SHORTCODE
========================================================================== */
function wlt_page_listings( $atts, $content = null ) {
global $userdata, $wpdb, $CORE; $STRING = ""; $extra=""; $i=1; $stopcount = 4;
extract( shortcode_atts( array( 'query' => '', 'show' => '', 'type' => '', 'cat' => '', 'orderby' => '', 'order' => '', 'grid' => "no", 'featuredonly' => "no"), $atts ) );
// SETUP DEFAULTS
if(!isset($atts['show']) || (isset($atts['show']) && $atts['show'] == "") ){ $atts['show'] = 5; }
if($atts['type'] == ""){ $atts['type'] = THEME_TAXONOMY.'_type'; }
if($atts['orderby'] == ""){ $atts['orderby'] = "post_title"; }
if($atts['order'] == ""){ $atts['order'] = "desc"; }
// DEFAULT FOR LIST STYLE
if($grid == "yes"){
$sstyle = "grid_style";
$STRING .= '<script language="javascript">jQuery(window).load(function() { equalheight(\'.grid_style .item .thumbnail\');});</script>';
}else{
$sstyle = "list_style";
}
$query= str_replace("#038;","&",$query);
if(strlen($query) > 1){
// ADD ON POST TYPE FOR THOSE WHO FORGET
if(strpos($query,'post_type') == false){
$args = $query ."&post_type=".THEME_TAXONOMY."_type";
}else{
$args = $query;
}
}elseif($featuredonly == "yes"){
$args = array('posts_per_page' => $atts['show'],
'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'],
'meta_query' => array (
array (
'key' => 'featured',
'value' => 'yes',
)
)
);
}else{
/*** default string ***/
$args = array('posts_per_page' => $atts['show'], 'post_type' => $atts['type'], 'orderby' => $atts['orderby'], 'order' => $atts['order'] );
}
/*** custom category ***/
if(strlen($atts['cat']) > 1){
$args = array('tax_query' => array( array( 'taxonomy' => str_replace("_type","",$atts['type']) ,'field' => 'term_id','terms' => array( $atts['cat'] ))), 'posts_per_page' => $atts['show'] );
}
// BUILD QUERY
$the_query = new WP_Query( hook_custom_queries($args) );
if ( $the_query->have_posts() ) {
$STRING .= '<div class="_searchresultsdata"><div class="wlt_search_results row '.$sstyle.'">';
while ( $the_query->have_posts() ) { $the_query->the_post(); $post = get_post();
$STRING .= '<div class="item '.hook_gallerypage_item_class('col-md-4').$CORE->FEATURED($post->ID).'">'.hook_item_cleanup(hook_gallerypage_item($CORE->ITEM_CONTENT($post))).'</div>';
}
$STRING .= '</div></div><div class="clearfix"></div>';
}
// END QUERY
wp_reset_postdata();
return $STRING;
}
add_shortcode( 'LISTINGS', array($this,'wlt_page_listings') );
The shortcode does not have an attribute to hide certain categories. I need to display all listings, except the ones in wedding category, which is a custom taxonomy. Is there any way to do that with the above code?
Will something like this work?
if ( is_tax( 'listing', 'wedding' ) ) {
do not display the wedding listings and display the rest}
Any suggestions?
EDITS:
This my online site url : http://webzer.comxa.com/
The main page shows the all the products.I like to have all but not one that is from wedding category coz i have separate page to list wedding category.
i have tried this where 51 is the page id of my home store page
if ( is_page( 51 ) && is_tax( 'listing', 'wedding' ) ) {
?><style>.caption {display:none!important;}</style>
<?php } ?>
this also didn't work
Consider this :
change
function wlt_page_listings( $atts, $content = null ) {
to
function wlt_page_listings( $atts, $content = null, $exclude=array(99) ) { // 99 is wedding cat id
where $exclude is an optional array of excluded cat names (99 in there for ease of testing/use)
Then in the
while ( $the_query->have_posts() ) {
add something like this:
$post = get_post(); // get post obj, will use the ID attr
$cats = wp_get_post_categories( $post->ID )); // returns array of IDs for all cats
foreach($exclude as $x){ // loop on the excluded cat ids
if(in_array($x, $cats))continue; // if excluded skip
}
http://codex.wordpress.org/Function_Reference/wp_get_post_categories
I think this will work or at least got you close.
display:none is bad mojo as the content will still be in your source code for others to see.
I hope I addressed the problem correctly for you, cheers.

How to get service tags based on service route?

I'm working on a permission system and I'd like to get information about all the admin classes loaded in Sonata.
I have a list of all these classes via
$this->getConfigurationPool()->getAdminClassas();
I would like to get the defined tags (Group / Label) for these services.
Thanks!
You can get Groups / Labels by calling getDashboardGroups() on getConfigurationPool()
$groups=$this->getConfigurationPool()->getDashboardGroups();
$adminGroups=array();
$i = 0;
foreach ($groups as $key => $val) {
$adminGroups['groups'][$i]['label']=$val['label'];
foreach ($val['items'] as $items) {
$adminGroups['groups'][$i]['items'][]=$items->getLabel();
}
$i++;
}
echo '<pre>';print_r($adminGroups);echo '</pre>';
So the resultant $adminGroups array will have all groups and its admin titles in below form
Array
(
[groups] => Array
(
[0] => Array
(
[label] => Group 1
[items] => Array
(
[0] => Menu item 1
)
)
[1] => Array
(
[label] => Group 2
[items] => Array
(
[0] => Menu item 1
[1] => Menu item 2
[2] => Menu item 3
)
)
[2] => Array
(
[label] => Group 3
[items] => Array
(
[0] => Menu item 1
[1] => Menu item 2
)
)
/*And so on ....*/
)
)
Above will return admin labels which are set to show_in_dashboard = true to all admin labels you can get them by their service ids
$services = $this->getConfigurationPool()->getAdminServiceIds();
$container = $this->getConfigurationPool()->getContainer();
$adminLabels = array();
foreach ($services as $key => $val) {
$admin = $container->get($val);
$adminLabels['label'][] = $admin->getLabel();
}
echo '<pre>';print_r($adminLabels);echo '</pre>';

Gettin atrribute thumbnail from node from a wordpress rss feed

I've been trying to get this seemingly easy peace of code to work.
I'm loading rss from a wordpress site and it all works fine except for the thumbnails. Since in the XML they are set as an attribute instead of a nodeValue i can't seem to get import them. (i've really tried a lot)
$rss = new DOMDocument();
$rss->load('http://goalprogramme.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
// in XML it looks like <media:thumbnail url="http://goalprogramme.files.wordpress.com/2014/01/dsc_0227.jpg?w=150"/>
//echo $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url');
//push items
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'thumbnail' => $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url') // this line doesn't work !!!
);
array_push($feed, $item);
}
Any help would be greatly appreciated.
Thanks so much in advance!
Hours later i've created another piece of code that does work. If anyone needs it it, here it is:
$feed_array = array();
$feed = simplexml_load_file('http://goalprogramme.wordpress.com/feed/');
foreach ($feed->channel->item as $item) {
$title = (string) $item->title;
$description = (string) $item->description;
$link = (string) $item->link;
$date = (string) $item->date;
if ($media = $item->children('media', TRUE)) {
if ($media->thumbnail) {
$attributes = $media->thumbnail->attributes();
$thumbnail = (string)$attributes['url'];
}
}
$item = array (
'title' => $title ,
'desc' => $description,
'link' => $link,
'date' => $date,
'thumbnail' => $thumbnail
);
array_push($feed_array, $item);
}

Create Drupal Form in Table

I try to create a table where a row is looking like this:
|Text1|Text2|Button1|
As soon as the user clicks on the button I want to exchange the Button1 with tow textFields and another button... so a normal AJAX-request.
I've tried to implement this, not sure if this is the correct way, let me know if I could to it in a completely other way :S
So what I've already done:
In my hook_menu i link to a function that returns the data:
return theme ( 'my_theme_function', array (
'running' => true,
'form' => $database->getData ()
) );
What happens in the getData()-Function:
I create the form using drupal_get_form(..).. returned from the given form is this:
$data = array (
'items' => $finder->getGamesToPlayForm ( $form_state ),
'#title' => 'Title'
);
the $finder returns the list of items I want to show in the table. So nothing special.
my_theme_function is set to use a template-file where I would like to show the whole stuff.
This looks like this:
$header = array (
'Col1',
'Col2',
''
);
$rows = array ();
foreach ( element_children ( $formData ['items'] ) as $gameId ) {
$row = array ();
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col1'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col2'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#form'] );
$rows [] = $row;
}
$output = theme ( 'table', array (
'header' => $header,
'rows' => $rows
) );
unset($formData['items']);
$output .= drupal_render_children ( $formData );
print $output
this way, everything is printed correct. But there are no "form"-tags, which stops my form from working..
So, any idea/hint how to solve this kind of problem? Probably I'm on the completely wrong way..
try
'render element' => 'form'
in your theme

Resources