How to randomize images in marquee - php

Is there anyone who can help me out with this.
It’s about a marquee shortcode in wordpress.
The marquee code shows uploaded images on date order.
I was wondering whether I could make the images slide randomly.
So that no matter what date I upload a picture, with every page view , the order of sliding images will change every time.
Here's the code:
$out .= '" id="_'.$menu_id.'" style="background-color:'.$background_color.'">'."\n";
$out .= ' <div class="shadow"></div>'."\n";
$out .= ' <div class="content">'."\n";
$out .= ' <div class="text">'.balanceTags($content).'</div>'."\n";
$out .= ' <div class="marquee-outer">'."\n";
$out .= ' <ul class="marquee-inner">'."\n";
$images_array = explode(',', $images);
foreach($images_array as $image_id){
$image = wp_get_attachment_image_src($image_id, 'full');
$out .= ' <li class="marquee-item" style="background-image:url('.$image[0].');"></li>';
}
$out .= ' </ul>'."\n";
$out .= ' </div>'."\n";
//$out .= ' </div>'."\n";
//$out .= '</section>'."\n";
return $out;
}
Any ideas?

Use the PHP shuffle function to randomise the array:
$out .= '" id="_'.$menu_id.'" style="background-color:'.$background_color.'">'."\n";
$out .= ' <div class="shadow"></div>'."\n";
$out .= ' <div class="content">'."\n";
$out .= ' <div class="text">'.balanceTags($content).'</div>'."\n";
$out .= ' <div class="marquee-outer">'."\n";
$out .= ' <ul class="marquee-inner">'."\n";
$images_array = explode(',', $images);
shuffle($images_array);
foreach($images_array as $image_id){
$image = wp_get_attachment_image_src($image_id, 'full');
$out .= ' <li class="marquee-item" style="background-image:url('.$image[0].');"></li>';
}
$out .= ' </ul>'."\n";
$out .= ' </div>'."\n";
//$out .= ' </div>'."\n";
//$out .= '</section>'."\n";
return $out;
}

Related

How to add static li inside dynamic list using php?

I have 5 menu in the website which is coming dynamically :
menu1 menu2 menu3 menu4 menu5
Now I want to add menu6 before menu5 using php.
My code:
foreach ($collection as $category) {
$i++;
$menuCategory = $this->getCategoryAsArray($category, $currentCategory);
$class = '';
$class .= 'nav'. $i;
if($i == 1) {
$class .= ' first';
} elseif ($i == $count) {
$class .= ' last';
}
if($menuCategory['is_active']) {
$class .= ' active';
}
//if($this->hasChildProduct($category)) {
//$class .= ' parent';
//}
if($this->hasChildSubCategory($category)) {
$class .= ' parent';
}
$class .= ' level-top';
$html .= '<li class="level0 '. $class .'">';
$html .= '<a href="'. $menuCategory['url'] .'">';
$html .= '<span>'. $menuCategory['name'] .'</span>';
$html .= '</a>';
//if($this->hasChildProduct($category)) {
//$html .= $this->getChildProductMenuHtml($category, $i);
//}
if($this->hasChildSubCategory($category)) {
$html .= $this->getChildSubcategoryMenuHtml($category, $i);
}
$html .= '</li>';
}
Menu6 is the static link which code is:
<li class="vertical-submenu" id="static-menu"><a href="<?php echo $block->getUrl('menu6')?>"><?php echo __('menu6')?></li>
I am not getting the code because i am not aware of values coming in the array in foreach loop.
But I can explain the situation to you via Algorithm.
$i = 0;
foreach(expression){
if(value == "menu5"){
write("Menu 4")
}
Write("Menu ".$i)
$i++;
}
Hope your problem is resolved with this. If you need anything else or want to elaborate more about you problem text me at shahrukhusmaani#gmail.com

Reinitializing variable in php to NULL

I'm writing a simple gamePage. My Controller method looks like this:
$model = new Game143();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->verifyAnswers();
return $this->render('Game143-confirm', ['model' => $model]);
} else {
$model->initGame(1, 2);
return $this->render('Game143', ['model' => $model]);
}
The else is the important bit. The initGame method is passed the starting level and the max number of levels, so I'm iterating 2 times with random digits and everything works fine.
In my init method I set the value:
public function initGame($level, $numEx) {
$this->maxLevel = $numEx; // normally 10
$this->thisLevel = $level; //normally 1
I also did some output. This works fine and my variable is saved as 2.
In the if method of the controller my variable is suddenly set to NULL?!
Because of this even my results are NULL, because my iteration is <=maxLevel. Anyone know why this happens?
In verifyAnswers(); my given and initialized maxLevel is NULL.
Iterating not possible:
for(; $this->thisLevel <= $this->maxLevel; $this->thisLevel++) {
If i hardcode the maxLevel everything works fine also.
In the view (Working with yii-2):
for($model->thisLevel = 1; $model->thisLevel <= $model->maxLevel; $model->thisLevel++) {
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit1[$model->thisLevel]");
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit2[$model->thisLevel]");
$out .= "\n";
$out .= "\n";
$out .= "<h3>Level $model->thisLevel</h3>\n";
$out .= "<h3>max $model->maxLevel</h3>\n";
$out .= "\n";
$out .= "<div class=\"row aufgabenFeld\">";
$out .= "<div align=\"right\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">";
$out .= $model->digit1[$model->thisLevel];
$out .= "</div>";
$out.="<div id='myField__<?php echo $model->thisLevel; ?>' class=\"col-lg-2 col-md-2 col-xs-2 wall\">\n";
$out .= $form->field($model, "proposal[$model->thisLevel]")->label(false);
$out .= "</div>";
$out .= "<div align=\"left\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">"; //id test xD
$out .= $model->digit2[$model->thisLevel];
$out .= "</div>";
$out .= "</div>";
$out .= "<div><br></div>";
In the 'submitView':
foreach ($model->proposal as $key=>$value){
$out .= "<li><h4>Aufgabe $key</h4>";
$out .= $model->digit1[$key];
$out .= " ";
$out .= $model->proposal[$key];
$out .= " ";
$out .= $model->digit2[$key];
$out .= $model->maxLevel[$key];
$out .= "</li>";
}

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

PHP echo custom fields with $out

i am using the Favorites plugin in Wordpress to save posts. I'm trying to adjust the way the information is displayed though. All the styling etc works below and my divs are being included, however i am struggling to pull information through to fill the divs. Fore example, i am trying to pull the excerpt in to the p.details but it is just throwing out an empty result, with no errors. Similarly, i am trying to pull through an acf custom field of 'bath' into p.bath but that is also empty. Any suggestions? Thanks in advance.
if ( is_multisite() ) switch_to_blog($this->site_id);
$out = '<ul class="property-list" data-userid="' . $this->user_id . '" data-links="true" data-siteid="' . $this->site_id . '" ';
$out .= ( $include_button ) ? 'data-includebuttons="true"' : 'data-includebuttons="false"';
$out .= ( $this->links ) ? ' data-includelinks="true"' : ' data-includelinks="false"';
$out .= ' data-nofavoritestext="' . $no_favorites . '"';
$out .= ' data-posttype="' . $post_types . '"';
$out .= '>';
foreach ( $favorites as $key => $favorite ){
$out .= '<li data-postid="' . $favorite . '">';
$out .= '<div class="third-1">';
$out .= '<a class="property-thumb" href="' . get_permalink($favorite) . '">';
$out .= '</a>';
$out .= '</div>';
$out .= '<div class="third-2">';
if ( $this->links ) $out .= '<h3 class="name"><a href="' . get_permalink($favorite) . '">';
$out .= get_the_title($favorite);
if ( $this->links ) $out .= '</a></h3>';
if ( $this->links ) $out .= '<h4 class="price">';
$out .= '£' . '300';
if ( $this->links ) $out .= '</h4>';
if ( $this->links ) $out .= '<p class="details">';
$out .= the_excerpt();
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<p class="bed">';
$out .= '1';
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<p class="bath">';
$out .= '1';
if ( $this->links ) $out .= '</p>';
if ( $this->links ) $out .= '<a class="full-details" href="' . get_permalink($favorite) . '">';
$out .= 'Full details';
if ( $this->links ) $out .= '</a>';
if ( $this->links ) $out .= '<a class="book-viewing" href="' . get_permalink($favorite) . '">';
$out .= 'Book Viewing';
if ( $this->links ) $out .= '</a>';
$out .= '</div>';
$out .= '</li>';
}
Change the_excerpt(); to get_the_excerpt(); and it should work.

Writing out the category of a product from Duka Press Plugin

I'm trying to modify a wordpress plugin called Duka Press, and I'm a complete noobie to PHP. It uses shortcodes which produces the template for the product grid, and I basically just need to write out all the categories the product has to create jQuery filter function.
To better understand what I need read the HTML comment in the code underneath
CODE:
<ul>
I need this for each category inside the active category set i the shortcode:
<li>[CategoryNameHere]</li>
</ul>
$products = get_posts($order_string . 'numberposts=' . $per_page . '&post_type=' . $type . '&meta_key=price&category=' . $category . $offset);
if (is_array($products) && count($products) > 0) {
$content .= '<div class="dpsc_grid_display">';
$count = 1;
$all_count = 0;
foreach ($products as $product) {
$output = dpsc_get_product_details($product->ID, $p_b_n, $direct_checkout);
if ($output) {
$attachment_images = &get_children('post_type=attachment&post_status=inherit&post_mime_type=image&post_parent=' . $product->ID);
$main_image = '';
foreach ($attachment_images as $image) {
$main_image = $image->guid;
break;
}
$prod_permalink = get_permalink($product->ID);
$content .= '<div class="dpsc_grid_product">';
$content .= '<div class="dpsc_grid_product_image">';
if ($main_image != '') {
$content .= '<img src="' . DP_PLUGIN_URL . '/lib/timthumb.php?src=' . $main_image . '&w=' . $dp_shopping_cart_settings['g_w'] . '&h=' . $dp_shopping_cart_settings['g_h'] . '&zc=1" >';
}
$content .= '</div>';
<!-- In the following line i need to write out all the categories for the current product -->
$content .= '<div class="dpsc_grid_product_detail [Categories here]">';
$content .= '<p class="title">' . __($product->post_title) . '</p>';
$content .= '<p class="detail">' . $product->post_excerpt . '</p>';
$content .= '<p class="price">' . $output['price'] . '</p>';
$content .= $output['start'];
$content .= $output['add_to_cart'];
$content .= $output['end'];
$content .= '</div>';
$content .= '</div>';
if ($count === intval($column)) {
$content .= '<div class="clear"></div>';
$count = 0;
}
$count++;
$all_count++;
}
}
$content .= '<div class="clear"></div>' . $page_links . '<div class="clear"></div>';
$content .= '</div>';
$content .= '<div class="clear"></div>';
}
return $content;
Shortcode that uses the template above:
[dpsc_grid_display category="22" total="100" column="3" per_page="100" type="duka"]

Categories