Favorites in List Page

Hi,
I want to do a favorites button in each row in list page and in view page. No problem (ListOptions and Page_Render). But I’ll also want a filter that change the mysql statment like:

SELECT * FROM activity WHERE id IN
(SELECT activityid FROM favorites WHERE userid = CurrentUserInfo(“id”))In Recordset_Selecting I can add AddFilter() but how to only activate the filter when user press the button?Can I use the filter API like: <a href=“activitylist.php?field1=‘.CurrentUserInfo(“id”).’”> in someway?Thanks!

riverman wrote:
In Recordset_Selecting I can add AddFilter() but how to only activate the filter when user press the button?Your button should post back the page, then in the server event you should check if that button is pressed and then add your filter.

Thank you very much!If anyone else is interested; this is what I did. I think it would be nicer if used AJAX, but I’m not good enough at java script…I used isset() function to determine if button was pressed.

I have a table called activity where users can choose to like records.CREATE TABLE IF NOT EXISTS activity (
id int(11) NOT NULL AUTO_INCREMENT,
namn varchar(50) NOT NULL,
PRIMARY KEY (id));Of course I also have a users table with users that can logged in.CREATE TABLE IF NOT EXISTS users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(25) NOT NULL,
password varchar(50) NOT NULL,
activated int(11) NOT NULL,
name varchar(25) NOT NULL,
userlevelid int(11) NOT NULL,
profile text DEFAULT NULL,
PRIMARY KEY (id));Then I created a table called favorites where all likes are stored in.CREATE TABLE IF NOT EXISTS favorites (
id int(11) NOT NULL AUTO_INCREMENT,
userid int(11) NOT NULL,
activityid int(11) NOT NULL,
PRIMARY KEY (id));We need a button where user can press when they want to see only favoritesTable-Specific->List Page → Page_DataRenderingfunction Page_DataRendering(&$header) {
echo ‘


echo ’ Favoriter’;
echo ‘

’;}We need a filter to Page List that is only active when a user has pressed a button to show only favorites.Table-Specific->Common->Recordset_Selectingfunction Recordset_Selecting(&$filter) {
// Filter it
if(isset($_GET[“favo”])){
$filters = “id IN (SELECT activityid FROM favorites WHERE userid = “.CurrentUserInfo(‘id’).”)”;
}else{
// Default filter
}

AddFilter($filter, $filters); // Set filter

}I created in list view, a hart that is filled (fa-heart) when liked and not filled (fa-heart-o) when not liked. I use the variable $chgfavo to trigger an update of the record.Table-Specific->->List Page → ListOptions_Loadfunction ListOptions_Load() {
$item = &$this->ListOptions->Add(“mantest”);
$item->Header = “Favorit”;
$item->MoveTo(0);
}Table-Specific->List Page → ListOptions_Renderedfunction ListOptions_Rendered() {
if(isset($GET[‘chgfavo’]) && $GET[‘chgfavo’] == ‘TRUE’){
update_favo(CurrentUserInfo(“id”), $GET[‘activityid’]);
$GET[‘chgfavo’] = ‘FALSE’;
}

if (get_favo(CurrentUserInfo("id"), $GLOBALS["activity"]->id->CurrentValue)){
	$this->ListOptions->Items["mantest"]->Body = '
		<span id="el1_activity_view" class="activity_view">
			<a class="ew-row-link ew-view" title="" data-caption="Favorit" href="activitylist.php?chgfavo=TRUE&activityid='.strval($GLOBALS["activity"]->id->CurrentValue).'" data-original-title="Favorit">
				<i data-phrase="ViewLink" class="fa fa-heart" data-caption="Favorit"> </i>
			</a>
		</span>';
}else{
	$this->ListOptions->Items["mantest"]->Body = '
		<span id="el1_activity_view" class="activity_view">
			<a class="ew-row-link ew-view" title="" data-caption="Favorit" href="activitylist.php?chgfavo=TRUE&activityid='.strval($GLOBALS["activity"]->id->CurrentValue).'" data-original-title="Favorit">
				<i data-phrase="ViewLink" class="fa fa-heart-o" data-caption="Favorit"> </i>
			</a>
		</span>';
}
$this->ListOptions->GetItem("mantest")->CssStyle = "text-align:center;";

}—I made two global functions. One the get status of record (liked or not) and one to update favorites table.Global->All Pages->Global Codefunction get_favo($user, $activity){
$fave = ExecuteScalar(“SELECT userid FROM favorites WHERE userid=”.$user." AND activityid=".$activity);

if ($fave==null){
	$fave = FALSE;
}else{	
	$fave = TRUE;
}

return $fave;

}function update_favo($user, $activity){
if (get_favo($user, $activity)==TRUE) {
Execute(“DELETE FROM favorites WHERE favorites.activityid = “.$activity.” AND userid=”.$user);
}else{
Execute(“INSERT INTO favorites (id, userid, activityid) VALUES (NULL, '”.$user.“', '”.$activity.“')”);
}}It works for me but could perhaps be nicer coded… and again thank you Arbei for helping me in the right direction!

You should change this code:
if ($fave==null){to:
if (empty($fave)) {