Sorry for being thick, but I'm just not getting website favourites!

By JeffC - March 11, 2016

Ok, so I have installed the plug in and followed the first 4 instructions. I have my Add Favourites/Remove Favourites toggle working as it should.

But how do I view the favourites? …

Where does this code go? I presumed in a new file (myfavs.php or whatever) but if I do that I get the 'headers already output' error. I'm sure I am missing something really simple, could it just be Friday afternoon fatigue, or am I being a complete dummy? Any pointers gratefully received.

LISTING FAVORITE RECORDS
-------------------------------------------------------------------------------
Use the following viewer code to load only the users favorite records from a
section:

  <?php
    require_once "cmsAdmin/lib/viewer_functions.php";

    // get favorite record nums
    $tableOrTag        = mysql_escape('news');  // update this with your table or tag name
    $currentUserNum    = mysql_escape( @$CURRENT_USER['num'] );
    $favoriteNumsQuery = "SELECT recordNum FROM `{$TABLE_PREFIX}_website_favorites` WHERE tableOrTag = '$tableOrTag' AND createdByUserNum = '$currentUserNum'";

    // load matching records
    list($favoriteRecords, $favoriteMetaData) = getRecords(array(
      'tableName'     => 'news', // update this with your table or tag name
      'perPage'       => '10',
      'where'         => " num IN ($favoriteNumsQuery) ",
      'loadCreatedBy' => false,
      'allowSearch'   => false,
    ));
  ?>

Just update 'news' with your tablename.

Jeff

By JeffC - March 14, 2016

Hi Ross

Having looked at this again with fresh eyes this morning I have got it sorted. Not sure what I was missing on Friday, but I started again, following the supplied instructions, and everything works as expected.

I do have a follow-up question however.

I pull in the content of my detail page using a generic file <?php include('common/inc/detail.php') ?>.

This does present an issue with the website favourites plugin in the following line because it requires a link to a specific table, in this case 'news'. 

<?php

$tableOrTag = 'news'; // Update with your section's table name


$recordNum = $record['num']; // Update this with current record number


?>

How would I alter the above so that it can be used on all of my detail pages. Could I change 'news' to a generic Tag and then apply that Tag to all of my records? Or would I list all of my table names: ie 

$tableOrTag = 'news'; 'reviews'; 'interviews'; // Update with your section's table name

Jeff

By ross - March 16, 2016

Hi Jeff

How about this. Instead of having $tableOrTag set inside your genric include file "common/inc/detail.php", have it set just before the include call like this:

<?php $tableOrTag = "news"; ?>
<?php include('common/inc/detail.php') ?>

This way, you can have a different $tableOrTag each time you include your detail.php page.

Let me know if this will work for you.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By JeffC - March 17, 2016

Thank you Ross, that worked perfectly.

Could I trouble you with one more question, then I'm done :)

I have a list viewer page to display my saved favourites. This page works perfectly with favourites from one table (news). now that I have added my 'add page to favourites' code to other sections, how would I list all of the favourites together on this page, for example favourite 'news' and favourite 'recipes'

Here is my viewer code:

// get favorite record nums
$tableOrTag        = mysql_escape('news');  // update this with your table or tag name
$currentUserNum    = mysql_escape( @$CURRENT_USER['num'] );
$favoriteNumsQuery = "SELECT recordNum FROM `{$TABLE_PREFIX}_website_favorites` WHERE tableOrTag = '$tableOrTag' AND createdByUserNum = '$currentUserNum'";

// load matching records
list($favoriteRecords, $favoriteMetaData) = getRecords(array(
'tableName'     => 'news', // update this with your table or tag name
'perPage'       => '10',
'where'         => " num IN ($favoriteNumsQuery) ",
'loadCreatedBy' => false,
'allowSearch'   => false,
));

and to display the results:

<?php$tableOrTag = 'news';          // Update with your section's table name$recordNum  = $record['num'];  // Update this with current record number?>

<?php foreach ($favoriteRecords as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php echo $record['short_description'] ?>
<?php endforeach ?>

Would an array merge be the solution here? If so, could you talk me through it.

Thanks

Jeff

By ross - March 17, 2016

Hi Jeff

I think the easiest way // get favorite record nums

$tableOrTag        = mysql_escape('news');  // update this with your table or tag name
$currentUserNum    = mysql_escape( @$CURRENT_USER['num'] );
$favoriteNumsQuery = "SELECT recordNum FROM `{$TABLE_PREFIX}_website_favorites` WHERE tableOrTag = '$tableOrTag' AND createdByUserNum = '$currentUserNum'";

// load matching records
list($favoriteRecords, $favoriteMetaData) = getRecords(array(
'tableName'     => 'news', // update this with your table or tag name
'perPage'       => '10',
'where'         => " num IN ($favoriteNumsQuery) ",
'loadCreatedBy' => false,
'allowSearch'   => false,
));

You'll need one copy for each $tableTag you have. Make sure to rename the $favoriteRecords variable in each copy of the code.

Once you do that, you can use array_combine() to stick all the arrays together.

A more advanced option would be to stick with the one block of code from above and modify $favoriteNumsQuery so the "WHERE" part is like this:

WHERE (tableOrTag = 'news' OR tableOrTag = "blogs" OR tableOrTag = "sports") AND  createdByUserNum = '$currentUserNum'"

Let me know what you think.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By JeffC - March 18, 2016

Thanks Ross

I've got that working with the first approach, favourites are added. However, now I can not remove them. Would I also need to adjust something here?:

<div class="<?php wsf_cssClassFor($tableOrTag, $recordNum, 'remove'); ?>"style="<?php wsf_displayStyleFor($tableOrTag, $recordNum, 'remove'); ?>"></div>
<p><a href="#" onclick="<?php wsf_onClickFor($tableOrTag, $recordNum, 'removeAndReload'); ?>"><span class="glyphicon glyphicon-remove"></span> Remove from favourites</a></p>
</div>

If I am taking up too much of your time in the forum, I am happy to take this over to consulting and pay you for your time.

Thanks

Jeff

Jeff

By Damon - March 24, 2016

Hi Jeff,

Just checking in. Please email Ross at consulting@interactivetools.com to continue discussing what is needed and to go ahead and get the work done.  

He is out of the office Friday but in all of next week. 

Thanks! 

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/