Get filter in Page_Foot server event

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
twuc
User
Posts: 72

Get filter in Page_Foot server event

Post by twuc »

I want to update the counts if the user makes a selection in Advanced Search, but they are not updated when the search result is first displayed. They are displayed if I then refresh the page in the browser. It looks to me like $filter is set after the footer is displayed. Is this correct and how can I get round it?

In both Recordset_Selecting and Recordset_Searching I have the following:

	if(!session_id()) session_start(); 
	$_SESSION['selectFilter'] = $filter;

customtestheader.php

<?php
namespace PHPMaker2023\elsielge;
include_once __DIR__ . "/../vendor/autoload.php";

// Require files

// exclude alerts
//$userslevels = array(-2);
//if (!in_array(CurrentUserLevel(), $userslevels) && !empty($_SESSION['selectFilter'])) {
if (!empty($_SESSION['selectFilter'])) {

$count1 = 0;
$count2 = 0;
}
 // Header Alerts
$newfilter = ""; 

if(!session_id()) session_start();
if (!empty($_SESSION['selectFilter'])) {
	$newfilter = $_SESSION['selectFilter'];

	$string = "`league_id`";
	$replace = "A.`league_id`";
	$newfilter = str_replace($string,$replace,$newfilter);

	$string = "`club_id`";
	$replace = "A.`club_id`";
	$newfilter = str_replace($string,$replace,$newfilter);

	//   this is a work around to display league fixture alerts
	$string = "`away_club_id`";
	$replace = "A.`away_club_id`";
	$newfilter = str_replace($string,$replace,$newfilter);

	$string = "`competition_id`";
	$replace = "A.`competition_id`";
	$newfilter = str_replace($string,$replace,$newfilter);

	$string = "`division_id`";
	$replace = "A.`division_id`";
	$newfilter = str_replace($string,$replace,$newfilter);

	$string = "`opposition`";
	$replace = "A.`opposition`";
	$newfilter = str_replace($string,$replace,$newfilter);

	$string = "`team`";
	$replace = "A.`team`";
	$newfilter = " WHERE " . str_replace($string,$replace,$newfilter);

}

//var_dump($newfilter);


// Teams playing more than once on same day
 $sql = "SELECT Count(DISTINCT A.fixture_id) from `viewzz_team_clash` as A ". $newfilter;
 $count1 = ExecuteScalar($sql);
 
if ($count1 > 0) {
	$bg1 = 'text-red';
	$fa_icon = 'fa-warning';
} else {
	$count1 == 0;
	$bg1 = 'text-green';
	$fa_icon = 'fa-check-circle';
}
$title1 = 'Fixtures with Teams Playing Twice in a Day';


//  Grounds with matches at the same time
 $sql = "SELECT Count(DISTINCT A.fixture_id) from `viewzz_ground_clash` as A ". $newfilter;

 $count2 = ExecuteScalar($sql);
if ($count2 > 0) {
	$bg2 = 'text-yellow';
	$fa_icon2 = 'fa-warning';
} else {
	$count2 == 0;
 	$bg2 = 'text-green';
	$fa_icon2 = 'fa-check-circle';
}
$title2 = 'Matches on Grounds at the Same Time';



// 
$row1 = array('bg'=>$bg1,'number'=>$count1,'title'=>$title1,'icon'=>$fa_icon,'link'=>'ViewzzTeamClashList');
$row2 = array('bg'=>$bg2,'number'=>$count2,'title'=>$title2,'icon'=>$fa_icon2,'link'=>'ViewzzGroundClashList');


$merge = array_merge(array($row1),array($row2));
//$merge = array_merge($merge,array($row3));


Display_Alerts($merge);
?>
function Display_Alerts($row_data) {
// exclude alerts
$userslevels = array();
if (!in_array(CurrentUserLevel(), $userslevels)) {
	$sum = 0;
	foreach ($row_data as $item) {
		$sum += $item['number'];
	}
 	
	$footer ="		  <li class='nav-item dropdown'>
			<a class='nav-link' data-bs-toggle='dropdown' href='#' >
			  <i class='fas fa-bell'></i>";
	if ($sum > 0 ) {			  
		$footer .="<span class='badge bg-warning navbar-badge'>" . $sum ."</span>";
	} else {
		$footer .="<span class='badge bg-success navbar-badge'>" . $sum ."</span>";
	}
	$footer .="</a>
			<div class='dropdown-menu dropdown-menu-lg dropdown-menu-end'>
			  <span class='dropdown-item  dropdown-header'>" . $sum ." Notifications</span>
			  <div class='dropdown-divider'></div>
			";

//	var_dump($row_data);
	$count=0;
	foreach ($row_data as $row) {

//	var_dump($row);
	$footer .="			  <a href='". $row['link'] ."' class='dropdown-item' " . $row['bg']. ">
				<span class='" . $row['bg'] . "'>
				<i class='fas mr-2 ". $row['icon'] . "'></i></span><span> ".$row['number']. " " . $row['title'] ."</span>
			  </a>
			  <div class='dropdown-divider'></div>";

/*
	 if ($row['number'] <> 0) 	{	  
			  $footer .= "<a href='" . $row['link'] . "' class='small-box-footer'>
				 More info <i class='fa fa-arrow-circle-right'></i>
			  </a>";
			}	else {
				$footer .= "<p class='small-box-footer'>There are none to display </p>";
			}
			; 
			$footer .= "</div>
		  </div>
		  <!-- ./col -->";
 * 
 */
	 	  $count++;
	}
	  $footer .="</div>
		  </li>";
	  print_r($footer);
	}
}

arbei
User
Posts: 9284

Post by arbei »

Your code looks OK, you may try set the filter to $GLOBALS before setting to session variable. If global variable exists, use it first, otherwise retrieve from your session variable.


twuc
User
Posts: 72

Post by twuc »

Thanks for that it's much appreciated, but I'm still having problems getting to the bottom of this. I've contacted support and got some helpful advice on debugging (see below) so I've simplified a version of my code, set $Globals, applied it to the 2023 demo project cars table, and its the same behaviour: namely my html isn't updated after Advanced Search, but the count is!

Cars Table:

        public function recordsetSearching(&$filter)
        {
            // Enter your code here
        	if(!session_id()) session_start(); 
        	$GLOBALS['selectFilter'] = $filter;
        	$_SESSION['selectFilter'] = $filter;
        }

PageFoot:

        <script type="text/html" class="ew-js-template" data-name="myDropdown" data-method="prependTo" data-target="#ew-navbar-end" data-seq="10">
        <?php
        $newfilter = ""; 
        if(!session_id()) session_start();
        if (!empty($_SESSION['selectFilter'])) {
         	$newfilter = $_SESSION['selectFilter'];
            $sql = "SELECT COUNT(*) from `cars` WHERE " . $newfilter;
            } else {
                if (!empty($GLOBALS['selectFilter'])) {
                    $newfilter = $GLOBALS['selectFilter'];
                    $sql = "SELECT COUNT(*) from `cars` WHERE " . $newfilter;
                } else {
                $sql = "SELECT COUNT(*) from `cars`";
                }
        }
        $count = ExecuteScalar($sql);
        **Log("my sql in footer", ["filter" => $newfilter, "sql" => $sql, "count" => $count]);**
        ?>
        <li class="nav-item dropdown">
        <a class="nav-link" data-bs-toggle="dropdown" href="#">
        <i class="fa-solid fa-bell"></i>
            <span class="badge bg-warning navbar-badge"><?php echo($count); ?></span>
        </a>
        </li>
        </script>

I've contacted Support and they advised me to add this line to help diagnose the problem

Log("my sql in footer", ["filter" => $newfilter, "sql" => $sql, "count" => $count]);

which logs my sql in the logfile and produces the following:

[2023-03-16T14:56:13.118360+00:00] log.DEBUG: my sql in footer {"filter":"`Cylinders` = 4","sql":"SELECT COUNT(*) from `cars` WHERE `Cylinders` = 4","count":"5"} []

I hope this helps others.

This seems to show that the footer code is being executed after the Advanced Search, the count is successfully updated with the value from the filter, but the html is not displaying the updated count! There are no sql errors reported or errors in the console.

If I reload the page manually (in Chrome) the new count appears by magic.

Note that if anyone replicates this code in their own (or the demo) project, the sql will only work with Advanced Search on an integer field (e.g. 'cylinders' in the demo project).

It's completely bamboozling me, any suggestions appreciated.


mobhar
User
Posts: 11660

Post by mobhar »

I tried your code in demo2023 project, and it works as expected.

twuc wrote:

but the html is not displaying the updated count!

Which html did you mean? Because the badge correctly displays the count after doing searching.


twuc
User
Posts: 72

Post by twuc »

Which html did you mean?

    <li class="nav-item dropdown">
    <a class="nav-link" data-bs-toggle="dropdown" href="#">
    <i class="fa-solid fa-bell"></i>
        <span class="badge bg-warning navbar-badge"><?php echo($count); ?></span>
    </a>
    </li>

But if it worked for you in the demo that's good news. I'll just have to go back to basics and have a good look at my setup.

Many thanks for doing that, I'll keep you updated.


twuc
User
Posts: 72

Post by twuc »

ok, I'm well out of my depth here. I'm still in the demo using the simple code shown above, and the same filter 'cylinders=4'.

View Page Source (Chrome) correctly shows:


    <span class="badge bg-warning navbar-badge">5</span>

Inspect (F12) shows (and page is displayed in the browser as):


    <span class="badge bg-warning navbar-badge">14</span>

From Stack Overflow, this might be caused by:

View source shows you the original HTML source of the page.
The inspector shows you the DOM as it was interpreted by the browser. This includes for example changes made by javascript which cannot be seen in the HTML source.

Bearing in mind this is the demo, I've made no other changes to the demo code, there are no errors in the console, I'm using a WAMP environment, and Mobhar is getting the correct result: does anyone know why or how the javascript may not be displayed correctly? Or how I can debug this? Many thanks.


Post Reply