randomly rotating products on a page with a reload

General ShopSite user discussion

randomly rotating products on a page with a reload

Postby jackie » Fri Jun 08, 2007 8:49 am

Hi

I am currently trying to build a page in Shopsite were products that have been assigned to it show up on the page and appear randomly, 9 at a time, and change with each page reload.

This process is relatively easy to create using only images and PHP, however I am still struggling to figure out how to get Shopsite to make this happen with entire products.

Is this even possible to accomplish with Shopsite products? Are there any examples that exist of this type of thing out there? I would appreciate any insight on this issue anyone has.

Thanks.
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Randomly Rotating Products

Postby BFChris » Fri Jun 08, 2007 9:26 am

I don't think this is possible in Shopsite because the pages are static HTML pages once they are generated in the Shopsite backoffice. They are not actively querying a database and constructing a page each time a viewer loads a page.

Sincerely,

Barefoot Chris
--------------------------------
Barefoot Chris Web Design
www.barefootchris.net
--------------------------------
BFChris
 
Posts: 322
Joined: Mon Oct 09, 2006 3:28 pm
Location: PA

Postby loren_d_c » Fri Jun 08, 2007 10:46 am

You are right that ShopSite itself won't do this, but what if your page was a .php page and your template output the products in PHP code that randomly orders them? Probably possible, but you would probably need someone who is very familiar with both ShopSite custom templates and PHP coding to do it.

-Loren
loren_d_c
 
Posts: 2572
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere

Postby jackie » Fri Jun 08, 2007 11:31 am

Good to know for sure that this is not some sort of function within Shopsite that I missed. Thanks for that.

I see where you are going with the php and I agree, I think that would be doable. Now the question is: How would I limit only 9 produts to show up at each time the page re-loads? For example lets say I had 20 products assigned to the page. Is there a way to tell Shopsite to only display 9 products on a page even if 20 are assigned to it, or would I be needing the php to do that also?
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby loren_d_c » Fri Jun 08, 2007 12:01 pm

Well, there would be a way using a VAR tag for a counter and incrementing it each time through the product loop to make only 9 product be placed on the generated page, but assuming that you want to rotate 9 out of 20 products on your page, you would really need to have ShopSite output all of the products into your PHP code and then let a PHP function determine which 9 to display.

-Loren
loren_d_c
 
Posts: 2572
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere

putting the product loop in php

Postby jackie » Tue Jun 12, 2007 1:21 pm

Ok, so I managed to figure out how to make a random disply script in php that will work for me, but I am still having trouble getting the product loop inside the php. Basically where '1' is I want the product loop to be. Any thoughts?

Here is the code:

[-- DEFINE PAGE --]

<?php


[-- LOOP PRODUCTS --]

[-- PRODUCT --]

[-- END_LOOP PRODUCTS --]


$file_array = array

(

'1','2','3','4','5',

);

$rand_keys = array_rand($file_array, 3);

echo $file_array[$rand_keys[0]] . "\n";
echo $file_array[$rand_keys[1]] . "\n";
echo $file_array[$rand_keys[2]] . "\n";


?>

[-- END_DEFINE PAGE --]
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby JeremeD » Tue Jun 12, 2007 3:53 pm

You have a good start there, but you have to get the products in the array before you work with them. Try this:

Code: Select all
deprecated code removed...


Give this a shot and see how it works.
Last edited by JeremeD on Tue Jun 12, 2007 8:19 pm, edited 1 time in total.
SD360.com
Certified ShopSite Designer
ShopSite Templates Available Now
JeremeD
 
Posts: 60
Joined: Sat Feb 10, 2007 4:20 pm
Location: Athens, GA

got it

Postby jackie » Tue Jun 12, 2007 4:54 pm

That worked perfectly,

Thanks
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby JeremeD » Tue Jun 12, 2007 5:03 pm

No problem. I'm glad it worked for you. ;-)
SD360.com
Certified ShopSite Designer
ShopSite Templates Available Now
JeremeD
 
Posts: 60
Joined: Sat Feb 10, 2007 4:20 pm
Location: Athens, GA

one more question

Postby jackie » Tue Jun 12, 2007 5:45 pm

I am trying to get this into a 3 column layout, but still seem to be having trouble. With this configuration the page seems to ignore the 3 column setting through the shopsite back office. I'm hoping you have some idea as to what I'm doing wrong. Here is what I have so far:


[-- DEFINE PAGE --]

[-- INCLUDE Page-Layout PROCESS --]

<table align="center" border="[-- VAR.border --]" cellpadding="3">

<?php


// This is just an empty array to hold the products
$productArray = array();

// The opening loop products tag
echo '[-- LOOP PRODUCTS PAGE.Columns --]';



// This takes each product and 'pushes' it onto the end of the array just as the function name implies
array_push($productArray, '[-- PRODUCT --]');



// The closing loop products tag
echo '[-- END_LOOP PRODUCTS --]';

// A counter used to limit the number of products displayed.
$counter = 0;

// This continues pulling random products from the productArray until it reaches the threshold which in this case is 3.
while ($counter < 9) {

echo '<td>';

print_r($productArray[array_rand($productArray)]);

echo '</td>';

$counter++;
}

?>

</table>

[-- END_DEFINE PAGE --]
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby JeremeD » Tue Jun 12, 2007 6:37 pm

Jackie,

Use this code as is. If you use the Page.Columns field in ShopSite, extra tr's will be inserted. This code will add tr's as necessary to generate columns. I added two variables right at the top so you can easily adjust the number of products displayed and number of columns per row. This code assumes that your product template contains td tags to make the cells within each row.

Code: Select all
deprecated code removed...
Last edited by JeremeD on Tue Jun 12, 2007 8:19 pm, edited 1 time in total.
SD360.com
Certified ShopSite Designer
ShopSite Templates Available Now
JeremeD
 
Posts: 60
Joined: Sat Feb 10, 2007 4:20 pm
Location: Athens, GA

Still one more issue

Postby jackie » Tue Jun 12, 2007 7:20 pm

JeremeD,

I needed to add <td>s to get the code you gave me lay-out properly. I also changed a few other things to make it fit. It is almost perfect except for one detail. It is displaying doubles of products that are assigned to it.

I currently have 12 products assigned to the page, when I reload the page I am seeing more than one of a few of the products. I thought array_rand would deal with this issue, but I guess not under this configuration. Any thoughts?

Code: Select all
[-- DEFINE PAGE --]


<table align="center" cellpadding="3" border="1" width="894">
<tr>


<?php

$totalProducts_jb = 9; // The total number of products to display
$numCols_jb = 3; // The number of columns per row

// This is just an empty array to hold the products
$productArray = array();

// The opening loop products tag
echo '[-- LOOP PRODUCTS --]';

// This takes each product and 'pushes' it onto the end of the array just as the function name implies
array_push($productArray,'[-- PRODUCT --]');

//  The closing loop products tag
echo '[-- END_LOOP PRODUCTS --]';

// A counter used to limit the number of products displayed.
$counter = 0;

// Another counter that we'll use to create the columns.
$counter2 = 0;

// This continues pulling random products from the productArray until it reaches the threshold which in this case is 9
while ($counter < $totalProducts_jb) {
echo "<td>";
print_r($productArray[array_rand($productArray)]);
echo "</td>";
$counter++;
$counter2++;

// If counter2 has reached 3, we'll end the current column and start a new one then reset the counter so it can generate the next column
if ($counter2 == $numCols_jb) {
echo "</tr><tr>";
$counter2 = 0;
}
}

?>

</tr>
<table>


[-- END_DEFINE PAGE --]

jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby JeremeD » Tue Jun 12, 2007 8:19 pm

Yes, array_rand can pick unique keys, but I was choosing one random key at a time using a loop so there were some duplicates. I updated the code to pull all 9 keys at once so there won't be any duplicates now.

I added one or two more variables you will need to change if you change their names.

Code: Select all
<?PHP

$totalProducts = 9; // The total number of products to display
$numCols = 3; // The number of columns per row

// First we start a table
echo '<table cellpadding="5" cellspacing="5" border="0">
<tr>';

// This is just an empty array to hold the products
$productArray = array();

// The opening loop products tag
echo '[-- LOOP PRODUCTS --]';

// This takes each product and 'pushes' it onto the end of the array just as the function name implies
array_push($productArray,'[-- PRODUCT --]');

//  The closing loop products tag
echo '[-- END_LOOP PRODUCTS --]';

// A counter used to limit the number of products displayed
$counter = 0;

// Another counter that we'll use to create the columns
$counter2 = 0;

// Pick out the number of random keys needed
$randomKeys = array_rand($productArray, $totalProducts);

// This continues pulling random products from the productArray until it reaches the threshold which in this case is 9
while ($counter < $totalProducts) {
echo $productArray[$randomKeys[$counter]];
$counter++;
$counter2++;

// If counter2 has reached your column threshold, we'll end the current column and start a new one then reset the counter so it can generate the next column
if ($counter2 == $numCols) {
echo "</tr><tr>";
$counter2 = 0;
}
}

// Now end the table
echo '</tr>
<table>';

?>
SD360.com
Certified ShopSite Designer
ShopSite Templates Available Now
JeremeD
 
Posts: 60
Joined: Sat Feb 10, 2007 4:20 pm
Location: Athens, GA

done

Postby jackie » Tue Jun 12, 2007 8:32 pm

That did the trick,

Thanks again JeremeD
jb
jackie
 
Posts: 31
Joined: Mon Mar 12, 2007 12:41 pm

Postby fastdrive » Thu Jul 26, 2007 11:15 pm

Hello Everyone,


This sounds like a great feature. Is this something I have to pay for or can I figure it out? Im hosting my website with Lexiconn.

Thanks for all your help!!
fastdrive
 
Posts: 1
Joined: Thu Jul 26, 2007 11:07 pm

Next

Return to User Forum

Who is online

Users browsing this forum: No registered users and 94 guests