Filter date

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Filter date

Post by utony » 2022-11-29 20:37

Hello all, I am looking for some assistance on building a custom filter for my page. When the user hits the page I want all the records for the current day moving forward to show. Example, 87 records in the table, but from today move forward there is only 14 records. So I need some automatic so show today's date and forward.

thank you in advance

Utony

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Filter date

Post by pbottcher » 2022-11-29 22:15

Hi,

you can use the $options->QueryWhere in the _init function to restrict your search for the result.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-01 08:22

This is what I would like to do, but need your help.

Shows record in table from today to 14 days out on initial view TV.

Any code help, placement, ect would be greatly appreciated. I know this is probably so easy for a master coder like you. I just need assistance as I don't know the logical piece to make this happen.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Filter date

Post by jsetzer » 2022-12-01 08:34

As a starting point you can try with the following code:

file: hooks/TABLENAME.php

Code: Select all

function TABLENAME_init(&$options, $memberInfo, &$args)
{
  $options->QueryWhere = $options->QueryWhere ? $options->QueryWhere . " AND " : "WHERE ";
  $conditions = [
    "datediff(now(), `TABLENAME`.`modified_on`) < 7"
  ];
  $options->QueryWhere .= implode(" AND ", $conditions);  
  return TRUE;
}
  • replace TABLENAME by your table name, there are two occurrences in the code
  • replace modified_on by your column name
  • replace "< 7" by whatever condition you need
  • you can add more conditions to the $conditions array. They all will be joined with AND
It works for me.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-01 21:06

jsetzer, you are an absolute master of your craft. Thank you so much for being so good to all of us and myself. I bought your plugin and will continue to support your site. Keep building this forward. We need you!

Thank you Sir.

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-01 21:47

Work great, matter of fact it works to good. LOL... I got it to show the last 14 days by default, good there... But when I click the "show all" button it will not show all the records. The table holds true to the 14 days only. Anyway to keep the default 4 days from today's date, but all the show all button to work to see all records. Right now the 14 default shows 12 records, but I have 40 in the table.

Thanks in advance!!!

Tony

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Filter date

Post by jsetzer » 2022-12-02 03:03

So, the requirement has changed from "automatically filtering by date on load" to "optionally filtering by date".

In these cases I am usually using parameters in URL, for example
https://www.yourserver.com/yourApp/v1/TABLENAME_view.php?f=date

See the filter-parameter f? You can name it as you like.

chrome_Axy6i2ia9R.png
chrome_Axy6i2ia9R.png (52.66 KiB) Viewed 2029 times

In TABLENAME_init()-hook I can get the filter name (and perhaps optional additional parameters) and build my conditions:

Code: Select all

function TABLENAME_init(&$options, $memberInfo, &$args)
{
	$filter = (string)Request::val("f", "");
	$conditions = [];
	switch ($filter) {
		case 'date':
			$conditions = [
				"datediff(now(), `TABLENAME`.`modified_on`) < 7"
			];
			break;

		default:
			$conditions = ['1=1']; // always true
			break;
	}
	$options->QueryWhere = $options->QueryWhere ? $options->QueryWhere . " AND " : "WHERE ";
	$options->QueryWhere .= implode(" AND ", $conditions);
	
	return TRUE;
}
Your next question could be:
"How can users open that page with the given f-parameter?", but that is a different forum request, I think.

You can add custom links to your homepage or navbar (https://bigprof.com/appgini/help/advanc ... r-contents) or even custom links by modifying hooks/TABLENAME-tv.js (https://bigprof.com/appgini/help/advanc ... agic-files) or _header-hook (https://bigprof.com/appgini/help/advanc ... ame_header) or _footer()-hook (https://bigprof.com/appgini/help/advanc ... ame_footer) or even have a browser-bookmark or desktop-link. There are many places and options for placing additional links.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Filter date

Post by jsetzer » 2022-12-02 03:15

Ok, as a starting point, here we have a very short snippet for _footer-hook in hooks/TABLENAME.php which will render a filter- and a reset-filter-button:

chrome_DKpzDcFQFR.png
chrome_DKpzDcFQFR.png (11.06 KiB) Viewed 2026 times

Code: Select all

function TABLENAME_footer($contentType, $memberInfo, &$args)
{
	$footer = '';
	switch ($contentType) {
		case 'tableview':
			$footer="<script>
			jQuery(\"#top_buttons > .all_records\")
				.append(jQuery('<a class=\"btn btn-default btn-lg\" href=\"?f=date\"><i class=\"glyphicon glyphicon-calendar\"></i></a>'))
				.append(jQuery('<a class=\"btn btn-default btn-lg\" href=\"?f=\"><i class=\"glyphicon glyphicon-remove\"></i></a>'));
			</script>";
			break;
// ...
}
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-02 05:12

JS,

function Move_Log_footer($contentType, $memberInfo, &$args)
{
$footer = '';
switch ($contentType) {
case 'tableview':
$footer="<script>
jQuery(\"#top_buttons > .all_records\")
.append(jQuery('<a class=\"btn btn-default btn-lg\" href=\"?f=date\"><i class=\"glyphicon glyphicon-calendar\"></i></a>'))
.append(jQuery('<a class=\"btn btn-default btn-lg\" href=\"?f=\"><i class=\"glyphicon glyphicon-remove\"></i></a>'));
</script>";
break;
}

case 'tableview':
$footer='';
break;

case 'detailview':
$footer='';
break;

case 'tableview+detailview':
$footer='';
break;

case 'print-tableview':
$footer='';
break;

case 'print-detailview':
$footer='';
break;

case 'filters':
$footer='';
break;
}

return $footer;
}

gives me - Parse error: syntax error, unexpected token "case" in C:\xamppnew\htdocs\XXX\hooks\Move_Log.php on line 56

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Filter date

Post by jsetzer » 2022-12-02 05:55

Personal wishes:
  1. Please, when posting here in the forum, always paste code fragments inside [code] ... [/code] blocks for better readability.
    see here app.php/help/bbcode?sid=63dcb1fb3510ea6 ... 552f4#f2r1
    This makes it easier for all of us to read and understand your code and to help you. You can even use the code-tool of the toolbar.
    chrome_UPVL0PC7vL.png
    chrome_UPVL0PC7vL.png (10.22 KiB) Viewed 2021 times
  2. I strongly recommend using a modern Code Editor like VSCODE (Visual Studio Code, free) offering Intellisense and Syntax Highlighting
---

Now back to the topic: You have added an invalid extra bracket which has to be removed:

chrome_JpCwljWNkF.png
chrome_JpCwljWNkF.png (10.34 KiB) Viewed 2021 times

My Code Editor (VSCODE) immediately shows me the syntax problems

Code_x5uUeb5gst.png
Code_x5uUeb5gst.png (32.27 KiB) Viewed 2021 times

As soon as I remove that extra bracket, everything looks good:

LiXJDM1mIg.png
LiXJDM1mIg.png (32.63 KiB) Viewed 2021 times

I hope this was the only mistake.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-03 16:26

JS- you bet thanks for the info. You don't know what you don't know about the code pasting. That worked!

I like the idea of just building a button for 7 days out from todays date and 14 days out from today's date. Can you help me? here is my url. But, I would like URL to always be from today's date out 7 days and then 14 days. Is there a way to allow user to not have to click and manually add dates to filter?

Is there like [today's date] for something?

http://localhost/XXX/Move_Log_view.php? ... =510679940

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Filter date

Post by jsetzer » 2022-12-03 17:01

According to MySQL docs, you can replace now() by CURRENT_DATE()
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

utony
Veteran Member
Posts: 93
Joined: 2020-04-03 18:37

Re: Filter date

Post by utony » 2022-12-04 19:00

Looking for some assistance.

I want to be able from the DV page, to click on the "Print Preview" button and a pop up message appears to display a message. The user will have to acknowledge before clicking the print button. Any assistance would much appreciated.

Looking for code help and location to put it .

Thank you!!!!!

-JS-?????

Post Reply