You are here:  » Drop down menu

Support Forum



Drop down menu

Submitted by JasonG on Wed, 2012-09-26 09:25 in

Hi David

Do you have any code already written to implenet a drop down menu for categories

As an example: {link saved} - hover oevr insurance or money, then display sub categories

or hover over Categories on this site: {link saved}

Thanks
Stuart

Submitted by support on Wed, 2012-09-26 12:12

Hi Stuart,

A drop-down box category navigation is straight forward - CSS can of course be used to style as required - the basic code as follows:

<?php
  $sql 
"SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE category <> '' ORDER BY category";
  if(
database_querySelect($sql,$rows))
  {
    print 
"<form method='GET' action='".$config_baseHREF."search.php'>";
    print 
"<select name='q' onchange='this.form.submit();'>";
    print 
"<option>Browse by category...</option>";
    foreach(
$rows as $row)
    {
      print 
"<option value='category:".htmlentities($row["category"]).":'>".$row["category"]."</option>";
    }
    print 
"</select>";
    print 
"<noscript><input type='submit' value='Go' /></noscript>";
    print 
"</form>";
  }
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by JasonG on Thu, 2012-09-27 11:11

Thanks David

Does this just go in the index.php file?

Stuart

Submitted by support on Thu, 2012-09-27 12:13

Hi Stuart,

It can go pretty much anywhere - if you only want the drop-down on the home page then yes, index.php is the place (note the PHP tags - if the position you want to include the code is already in PHP mode then leave them out of course). Otherwise it could go in html/header.php or html/footer.php as required if you want the drop-down shown on every page as part of your design...

Cheers,
David.
--
PriceTapestry.com

Submitted by enjoymarcus on Thu, 2013-01-24 18:54

Hi David,

Is there an easy way to create a menu system across separate PT installs and DBs?

Thanks,
Marc.

Submitted by support on Fri, 2013-01-25 09:14

Hi Marc,

Such things would normally be done with a loop; where having require()'d the main include files once, you include config.php for each site to generate whatever menu options required for that installation. To extend the above example to create a drop-down box for category selection inside 3 separate installations e.g. /electronics/, /homegarden/ and /healthbeauty/ you might do something like this:

<?php
  $sites 
= array
    (
      
"electronics"  => "Electronics",
      
"homegarden"   => "Home and Garden",
      
"healthbeauty" => "Health and Beauty"
    
);
  
$docroot "/home/example.com/public_html/"
  
// following just need to come from one install, doesn't matter which
  
require_once($docroot."electronics/includes/database.php"); 
  foreach(
$sites as $folder_name => $display_name)
  {
    require(
$docroot.$folder_name."config.php");
    
$sql "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE category <> '' ORDER BY category";
    if(
database_querySelect($sql,$rows))
    {
      print 
"<form method='GET' action='".$config_baseHREF."search.php'>";
      print 
"<select name='q' onchange='this.form.submit();'>";
      print 
"<option>".$display_name."...</option>";
      foreach(
$rows as $row)
      {
        print 
"<option value='category:".htmlentities($row["category"]).":'>".$row["category"]."</option>";
      }
      print 
"</select>";
      print 
"<noscript><input type='submit' value='Go' /></noscript>";
      print 
"</form>";
    }
  }
?>

Using the $docroot variable ensures that the require() statements will work wherever you insert the code within your site, e.g. it can be included within a common header in each installation for example. You can work it out by taking the Install Path as displayed on the Support Info page of one of your installations, and removing the actual installation folder from the end...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by enjoymarcus on Tue, 2013-01-29 16:54

Thanks David.

I'm getting the following error;

Parse error: syntax error, unexpected T_VARIABLE in /var/www/vhosts/...../menu.php on line 30.

With that said, how could I output just a single list, so that I can use it in the footer?

Thanks in advance.

Submitted by support on Wed, 2013-01-30 09:22

Hi Marcus,

There was a syntax error in the $sites array which should be:

  $sites = array
    (
      "electronics" => "Electronics",
      "homegarden" => "Home and Garden",
      "healthbeauty" => "Health and Beauty"
    );

(corrected above)

To create a single list of links to sites, use something like:

<?php
  $sites 
= array
    (
      
"electronics"  => "Electronics",
      
"homegarden"   => "Home and Garden",
      
"healthbeauty" => "Health and Beauty"
    
);
  foreach(
$sites as $folder_name => $display_name)
  {
    print 
"<a href='/".$folder_name."/'>".$display_name."</a>";
  }
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by jacques24681 on Tue, 2013-04-23 21:41

Hi

I'm new to pt, hope you can help me?

I want a drop down menu that uses <ul> and <li> elements instead of <select> and <option>?

Is this possible?

Thanks

Jacques

Submitted by support on Wed, 2013-04-24 07:48

Hello Jacques, and welcome to the forum!

Sure! The following is the minimal code to create a category menu using <ul> and <li>:

<?php
  $sql 
"SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE category <> '' ORDER BY category";
  if(
database_querySelect($sql,$rows))
  {
    print 
"<ul>";
    foreach(
$rows as $row)
    {
      if (
$config_useRewrite)
      {
        
$href $config_baseHREF."category/".tapestry_hyphenate($row["category"])."/";
      }
      else
      {
        
$href $config_baseHREF."search.php?q=category:".urlencode($row["category"]);
      }
      print 
"<li><a href='".$href."'>".$row["category"]."</a></li>";
    }
    print 
"</ul>";
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by jacques24681 on Wed, 2013-04-24 22:12

Hi David

Thank you, this helped me allot.

thanks

Jacques