Combo Page title

15 posts by 4 authors in: Forums > CMS Builder
Last Post: September 25, 2014   (RSS)

By Toledoh - September 11, 2014

Hi Guys,

I'm using the "combo page" generator to display products in categories - seen here: http://freestylepools.com.au/products.php

The pertinent code is:

// load records from 'products_listing'
list($products_listingRecords, $products_listingMetaData) = getRecords(array(
'tableName' => 'products_listing',
'loadUploads' => true,
'allowSearch' => false,
));

// load variations
$variationRecords = mysql_select('variations');
$variationProductNumToRecords = array_groupBy($variationRecords, 'products_listingNum', true);


// get the list of categories as array wherein the array key is the category value, and array value is the category label
// $productCategoriesValuesToLabels will be used for the left category list
$productCategoriesValuesToLabels = array_combine(array_pluck($products_listingRecords, 'category'), array_pluck($products_listingRecords, 'category:label'));

// $productsListsRecords will be used for the right sub-list
$productsListsRecords = array();
if (@$_REQUEST['category']){
$productsListsRecords = array_groupBy($products_listingRecords, 'category', true);
$productsListsRecords = @$productsListsRecords[$_REQUEST['category']];
}
else{
$productsListsRecords = $products_listingRecords;
}

However, google sees http://freestylepools.com.au/products.php and http://freestylepools.com.au/products.php?category=5 as different pages, and I therefore have to change the page title.

How can I capture the link selected (ie. the ?category=5) and create a variable such as $title to use in the head code?  And I guess I will have to have it default to $title = '' initially as nothing is selected?

Cheers,

Tim (toledoh.com.au)

By Toledoh - September 17, 2014

Any thoughts on this guys?

Cheers,

Tim (toledoh.com.au)

By Toledoh - September 18, 2014

haha - Thanks Zicky!  There's many ways to skin a cat huh!

Cheers,

Tim (toledoh.com.au)

By Codee - September 19, 2014

If I'm understanding the goal properly, what about something like this in the header:

<title><?php if ($productsRecord['title']): ?> - <?php echo $productsRecord['title'] ?><?php endif ?> -  <?php if ($productsRecord['category']): ?><?php echo $productsRecord['category'] ?> pool products<?php endif ?>- Freestyle Pool Products for Sale</title> 

By Toledoh - September 19, 2014

Thanks Equinox.  However no cigar.

I've attached a sample file (it's a bit messy - sorry).  I guess I need to call the query from the URL somehow... and if there is no query, then default to something else.

ie.

$cat = "Products Page",

Get query from URL (ie.  products.php?category=2) if available.  If so, then $cat="URL-QUERY:label Page"

<title>?php echo $cat</title>

Cheers,

Tim (toledoh.com.au)
Attachments:

sample.php 11K

By Toledoh - September 22, 2014

Hi itools - any ideas on how to achieve this?

Cheers,

Tim (toledoh.com.au)

By gregThomas - September 23, 2014 - edited: September 23, 2014

Hi Tim,

What about doing something like this:

<?php
  
  require_once("_app_init.php");
  
  $categoryTitle = "";

  // error checking - check required plugins installed and activated
  if (!@$GLOBALS['SIMPLECART_PLUGIN']) {
    die("You must activate the SimpleCart plugin before you can access this page.");
  }

  // load record from 'pages'
  list($pagesRecords, $pagesMetaData) = getRecords(array(
    'tableName'   => 'pages',
    'where'       => "`num` = '9'", //PAGE CONTENT http://freestylepools.com.au/cmsAdmin/admin.php?menu=pages
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $pagesRecord = @$pagesRecords[0]; // get first record
  if (!$pagesRecord) { dieWith404("Record not found!"); } // show error message if no record found


// load records from 'products_listing'
list($products_listingRecords, $products_listingMetaData) = getRecords(array(
  'tableName'   => 'products_listing',
  'loadUploads' => true,
  'allowSearch' => false,
));

// load variations
$variationRecords             = mysql_select('variations');
$variationProductNumToRecords = array_groupBy($variationRecords, 'products_listingNum', true);


// get the list of categories as array wherein the array key is the category value, and array value is the category label
// $productCategoriesValuesToLabels will be used for the left category list
$productCategoriesValuesToLabels = array_combine(array_pluck($products_listingRecords, 'category'), array_pluck($products_listingRecords, 'category:label'));

// $productsListsRecords will be used for the right sub-list
$productsListsRecords = array();
if (@$_REQUEST['category']){
  $productsListsRecords = array_groupBy($products_listingRecords, 'category', true);
  $productsListsRecords = @$productsListsRecords[$_REQUEST['category']];
  if($productsListsRecords){
    $categoryTitle = "Category - ".$productsListsRecords[0]['category:label']; 
  }
}else{
  $productsListsRecords = $products_listingRecords;
}

?><!DOCTYPE html>
<html>
<head>
<title>A title <?php if(@$categoryTitle){ $categoryTitle; } ?></title>

So if a category is detected, it looks as if you're filtering the current records on it. So at that point we can grab the category title from the first record from that list and store it in the variable $categoryTitle.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - September 23, 2014

Thanks Greg.

I'm getting the error: Notice: Undefined offset: 0 in /home/freestyl/public_html/products.php on line 43

Cheers,

Tim (toledoh.com.au)

By gregThomas - September 24, 2014

Hi Tim,

It could be that as you're grouping the items by their category number that the key doesn't exist in the array. Try using this method instead:

  // $productsListsRecords will be used for the right sub-list
  $productsListsRecords = array();
  if (@$_REQUEST['category']){
    $productsListsRecords = array_groupBy($products_listingRecords, 'category', true);
    $productsListsRecords = @$productsListsRecords[$_REQUEST['category']];
    if($productsListsRecords){
      foreach($productsListsRecords as $record){
        $categoryTitle = "Category - ".$record['category:label']; 
        break;
      }
    }
  }else{
    $productsListsRecords = $products_listingRecords;
  }

?><!DOCTYPE html>
<html>
<head> 
<title>A title <?php if(@$categoryTitle){ $categoryTitle; } ?></title>

Thanks!

Greg

Greg

Greg Thomas







PHP Programmer - interactivetools.com