Web Design Forum: Help: Randomising results - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help: Randomising results Help with a snippet of code please! Rate Topic: -----

#1 User is offline   HecticBen 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 22-December 11
  • Reputation: 0

Posted 22 December 2011 - 02:49 PM

Hey guys,

I'm currently using a plugin in Wordpress to show shopping cart products from my BigCartel store into my Wordpress. Now the script is only showing 1 result and echos an image for the product and a text link.. These are grabbed through BigCartel's API.

However, it shows the latest product, which is not what I want. I'd like it to change everytime you refresh to a different product. I believe this is the code I'd need to edit (correct me if I'm wrong!):

function list_products($limit = 10){
    $url = $this->productsapiurl . '?limit='. $limit;
    $result = $this->pull_data($url);
    $list = '<div style="width:100%; text-align:center;">';
    foreach ($result as $item) {                      
          $list .= '<a href="' . $this->producturl . $item->permalink . '" title="' . $item->name . '"><img src="' . $this->image_url($item->images->image[0]->url) . '" height="114" /><br />' . $item->name . '</a>';              
    }        
    $list .= '</div>';
    return $list;    
  }


Can anyone help??

Many thanks!

Ben
0

#2 User is offline   PHP Monkey 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 83
  • Joined: 25-July 11
  • Reputation: 6
  • Gender:Male
  • Location:Doncaster, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 28 December 2011 - 04:57 PM

View PostHecticBen, on 22 December 2011 - 02:49 PM, said:

Hey guys,

I'm currently using a plugin in Wordpress to show shopping cart products from my BigCartel store into my Wordpress. Now the script is only showing 1 result and echos an image for the product and a text link.. These are grabbed through BigCartel's API.

However, it shows the latest product, which is not what I want. I'd like it to change everytime you refresh to a different product. I believe this is the code I'd need to edit (correct me if I'm wrong!):

function list_products($limit = 10){
    $url = $this->productsapiurl . '?limit='. $limit;
    $result = $this->pull_data($url);
    $list = '<div style="width:100%; text-align:center;">';
    foreach ($result as $item) {                      
          $list .= '<a href="' . $this->producturl . $item->permalink . '" title="' . $item->name . '"><img src="' . $this->image_url($item->images->image[0]->url) . '" height="114" /><br />' . $item->name . '</a>';              
    }        
    $list .= '</div>';
    return $list;    
  }


Can anyone help??

Many thanks!

Ben



Use the shuffle() function to randomize the array items.

<?php

function list_products($limit = 10) 
{
	$url = $this->productsapiurl . '?limit='. $limit;
	$result = $this->pull_data($url);
	$list = '<div style="width:100%; text-align:center;">';
	
	// Randomize the order of the array items.
	shuffle($result);
	
	foreach ($result as $item) {
		$list .= '<a href="' . $this->producturl . $item->permalink . '" title="' . $item->name . '"><img src="' . $this->image_url($item->images->image[0]->url) . '" height="114" /><br />' . $item->name . '</a>';              
	}
	
	$list .= '</div>';
	
	return $list;
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users