You are here:  » Featured products modification


Featured products modification

Submitted by shoppersbase on Mon, 2011-11-28 13:22 in

Hi

The current featured products setup i have on my home page {link saved} enables me to Randomly display products including their images.

May I ask if you can help me with a small modification to this feature.

I would like to display Category names randomly in a GRIND form instead of products listing on my home page.

So instead of my featured products feature displaying random products mine will be displaying categories names randomly.

so it would look like this

Shop by Category

Baby and toddler clothing | Cameras | Games
Baby Clothes | Tables | Phones
Bags | Women Shoes | Handsets
Beers | Bikes | Fashion
Blank Media | Sports | Travel
Blankmedia |

I wouldn't mine paying for this but i think it will offer another way of listing key information on the index page.

Please can you provide me with the necessary codes i need to place that will a

- list randomly categories names in place of featured products
- List them in the way i have specify above (Grind form)
- also ability to restrict the number of characters per name to say 40 etc..

I ask for the last feature because it would prevent a category name that is too long.

Thanks

Joseph

Submitted by support on Mon, 2011-11-28 15:39

Hi Joseph,

That's pretty straight forward actually; the code to display a grid of random categories is as follows - simply paste this in wherever required, such as before or after the existing Featured Products code in index.php.

  $numRandomCategories = 18;
  $categoriesPerRow = 3;
  $sql = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT ".$numRandomCategories;
  database_querySelect($sql,$rows);
  $count = 0;
  print "<table>";
  print "<tr>";
  foreach($rows as $row)
  {
    print "<td><a href='".$config_baseHREF."categories.php?q=category:".urlencode($row["category"])."'>".$row["category"]."</a></td>";
    $count++;
    if ($count == $categoriesPerRow)
    {
      print "</tr><tr>";
      $count = 0;
    }
  }
  print "</tr>";
  print "</table>";

If the position in the code that you want to include the table isn't in PHP mode don't forget to include PHP tags. The HTML layout may need a adjustment to suit your design - let me know if you're not sure...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by shoppersbase on Mon, 2011-11-28 16:15

Hi

Thanks for your reply above however am struggling to implement the code as it BREAKS the index.php page. On my index.php page I want to completely remove the Featured products code. I don't want to use that.

Then I want to replace it with the above category listing code you have given me..

My question is how or where do I place the above code on my index.php page

existing index.php code
{code saved}

Also how do I remove the featured products codes as well

Thanks

joseph

Submitted by shoppersbase on Mon, 2011-11-28 16:21

Hi

Am not sure if you receive my last reply. please can you advice me

- how to completely remove the featured products code
- how to implement the above code (Random category listings)
- where to implement to code.

It currently breaks the index page

Thanks

Joseph

Submitted by support on Mon, 2011-11-28 16:27

Hi Jospeh,

There was a small typo in the original code, corrected above; please have a go with the following as your complete index.php

<?php
  
require("includes/common.php");
  print 
"<div align='center'>";
  print 
"<div style='height:80px;'>&nbsp;</div>";
  require(
"html/header.php");
  print 
"<div style='height:15px;'>&nbsp;</div>";
  require(
"html/searchform.php");
  print 
"<p>";
  print 
"<medium>";
  print 
translate("Search or browse by")." ";
  print 
"<a href='".($config_useRewrite?"merchant/":"merchants.php")."'>".translate("<b>merchant</b>")."</a>, ";
  print 
"<a href='".($config_useRewrite?"category/":"categories.php")."'>".translate("<b>category</b>")."</a> ".translate("or")." ";
  print 
"<a href='".($config_useRewrite?"brand/":"brands.php")."'>".translate("<b>brand</b>")."</a>";
  print 
"</medium>";
  print 
"</p>";
  
$numRandomCategories 18;
  
$categoriesPerRow 3;
  
$sql "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT ".$numRandomCategories;
  
database_querySelect($sql,$rows);
  
$count 0;
  print 
"<table>";
  print 
"<tr>";
  foreach(
$rows as $row)
  {
    print 
"<td><a href='".$config_baseHREF."categories.php?q=category:".urlencode($row["category"])."'>".$row["category"]."</a></td>";
    
$count++;
    if (
$count == $categoriesPerRow)
    {
      print 
"</tr><tr>";
      
$count 0;
    }
  }
  print 
"</tr>";
  print 
"</table>";
  print 
"</div>";
  print 
javascript_focus("search.q");
  require(
"html/footer.php");
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by shoppersbase on Mon, 2011-11-28 16:38

Hi

Great it works but we need further tweaks..

Each URL is coming out this way.....

http://www.example.com/categories.php?q=category:

IT SHOULD BE LIKE THIS

http://www.example.com/search.php?q=category:Cases:

Your code strips out the Category name at the end and its using the category.php instead of search.php. So for { Musical }. I have this

http://www.example.com/categories.php?q=category:

but it should be http://www.example.com/search.php?q=category:Musical:

Please help

Thanks

joseph

Submitted by shoppersbase on Mon, 2011-11-28 16:58

Hi David

While you are helping me fix the URL issue. i just want to complete the formatting side..sorry am very lousy with php code. html Ii can manage.

Ok for consistent I need to tidy up the listings

- Spacing between each column (cell spacing etc)
- Column with the SAME. Right now its using the longest category name
- Text size medium

please can i have a sample necessary code placement. What i mean is if you send me a sample code with the above formatting code embedded as well then i can play around with the parameters but right now I have no clue how to integrate column width, alignment, cell spacing etc with a php code. On a raw HTML I can do that.

Thank

joseph

Submitted by support on Mon, 2011-11-28 17:17

Hi Joseph,

There was one instance of $category that should have been $row["category"] - corrected in both posts above, but to cover your last reply regarding formatting, a replacement for the section of code responsible for the random categories would be as follows;

  $numRandomCategories = 18;
  $categoriesPerRow = 3;
  $sql = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT ".$numRandomCategories;
  database_querySelect($sql,$rows);
  $count = 0;
  print "<table cellspacing='20'>";
  print "<tr>";
  foreach($rows as $row)
  {
    print "<td width='200' style='font-size:medium'><a href='".$config_baseHREF."search.php?q=category:".urlencode($row["category"])."'>".$row["category"]."</a></td>";
    $count++;
    if ($count == $categoriesPerRow)
    {
      print "</tr><tr>";
      $count = 0;
    }
  }
  print "</tr>";
  print "</table>";

The above sets a cell spacing of 20 pixels (cellspacing='20'), and cell width of 200 pixels (width='200') and font size medium (style='font-size:medium')

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by shoppersbase on Mon, 2011-11-28 17:39

Hi David

Thanks a lot but the URL is still wrong. It results to this

{link saved}

This just takes you to the category page. I think we are missing this code or something

$item["href"] = "search.php?q=category:".urlencode($product["category"]).":";

Thanks

joseph

Submitted by support on Mon, 2011-11-28 18:11

Hi Jospeh,

Corrected above; this line:

print "<td width='200' style='font-size:medium'><a href='".$config_baseHREF."categories.php?q=category:".urlencode($row["category"])."'>".$row["category"]."</a></td>";

...should have referred to search.php as follows:

print "<td width='200' style='font-size:medium'><a href='".$config_baseHREF."search.php?q=category:".urlencode($row["category"])."'>".$row["category"]."</a></td>";

Cheers,
David.
--
PriceTapestry.com

Submitted by shoppersbase on Mon, 2011-11-28 21:27

Hi

It works..Thanks a bunch

Joseph

Submitted by smartprice24 on Mon, 2017-10-09 20:52

Hi David
Is possible using category hierarchy?

Thanks

Submitted by support on Tue, 2017-10-10 08:59

Sure - for Category Hierarchy, and using Foundation's block-grid-* style rather than a table, have a go with:

<?php
  $numRandomCategories 
12;
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."categories_hierarchy`";
  
database_querySelect($sql,$rows);
  if (
count($rows)>$numRandomCategories$numRandomCategories count($rows);
  
$categories_hierarchy = array();
  foreach(
$rows as $row)
  {
    
$categories_hierarchy[$row["id"]] = $row;
  }
  function 
getCategoryPath($id)
  {
    global 
$categories_hierarchy;
    
$categories = array();
    do {
      
array_unshift($categories,$categories_hierarchy[$id]["name"]);
    } while(
$id $categories_hierarchy[$id]["parent"]);
    return 
implode("/",$categories);
  }
  print 
"<div class='row'>";
  print 
"<div class='small-12 columns'>";
  print 
"<ul class='small-block-grid-2 medium-block-grid-4'>";
  
$category_ids array_rand($categories_hierarchy,$numRandomCategories);
  foreach(
$category_ids as $category_id)
  {
    
$path getCategoryPath($category_id);
    print 
"<li><a href='".tapestry_indexHREF("category",$path)."'>".$categories_hierarchy[$category_id]["name"]."</a></li>";
  }
  print 
"</ul>";
  print 
"</div>";
  print 
"</div>";
?>

Change $numRandomCategories in the first line as required...

Cheers,
David.
--
PriceTapestry.com

Submitted by smartprice24 on Tue, 2017-10-10 14:39

Hi David.
Thanks.
Scripts error:

Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in {code saved}

Warning: Invalid argument supplied for foreach() in {code saved}

  $category_ids = array_rand($categories_hierarchy,$numRandomCategories);
  foreach($category_ids as $category_id)

Submitted by support on Tue, 2017-10-10 15:28

Hi,

That would occur if $numRandomCategories was greater than the number of configured nodes in the category hierarchy - i've modified the above to include a check to automatically set the $numRandomCategories to the number of configured categories if lower...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by smartprice24 on Tue, 2017-10-10 20:05

Thanks David.

Hi have decreased down 12 and fine work.

Many thanks.
Giuseppe