Using field values in Page_Exporting (v2022)

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

Using field values in Page_Exporting (v2022)

Post by philmills »

In Page_Exporting under View Page I'd like to use field values for things like the title of the page, and filename etc.
something like this:

public function pageExporting()
    {
	global $ExportFileName;
	$ExportFileName = "StudentCard_".$this->StudentID->CurrentValue;
        if ($this->Export == "pdf") { 
		$this->ExportDoc->Text = "<style>td { font-size: 12px; font-family: Arial, Helvetica, sans-serif;}</style>";
           	$this->ExportDoc->Text .= "<h1 style='font-family: Arial, Helvetica, sans-serif'>Student Card | ".$this->StudentID->ViewValue."</h1>"; // Add title
		$this->ExportDoc->Text .= "<img src='".$school_logo()."' width='200' style='float: right;'></img>";
        }    
        return true; // Return true to use default export and skip Row_Export event
    }

It doesn't seem like field values can be used within this function, even though the exported pdf is loaded with field values...
What's the best method for this?


arbei
User
Posts: 9288

Post by arbei »

The Page_Exporting is fired before export, the data is not loaded yet, you may customize the ExportPdf class and override the exportHeaderAndFooter() method.


mobhar
User
Posts: 11660

Post by mobhar »

Alternatively, you may use ExecuteScalar global function in order to get your desired value from database.


philmills
User
Posts: 535

Post by philmills »

mobhar wrote:

Alternatively, you may use ExecuteScalar global function in order to get your desired value from database.

This would probably be the most flexible, but does Page_Exporting even know the id of the record being displayed? If so how do I pass that id into the ExecuteScalar query?
is there a session already created that contains the record id ?


philmills
User
Posts: 535

Post by philmills »

Wow, it seems like $this->MyField->CurrentValue actually does work. It's only $this->MyField->ViewValue that I can't use.
This is great, and it means I can use ExecuteScalar to get view values as needed.
Looks like issue is solved


philmills
User
Posts: 535

Post by philmills »

I'm updating this for v2023 +

It seems that $this->MyField->CurrentValue no longer works to get field value into an export filename.
I would use ExecuteScalar to get the values I need, but I need the current record ID for that.
Is there already a var defined for the current record ID (it's the URL) ?


philmills
User
Posts: 535

Post by philmills »

I couldn't find any record id variable which would work in page_Exporting
So I had to extract the id from the URL:

// Get the current URL
$currentUrl = $_SERVER['REQUEST_URI'];

// Split the URL by '/' to get an array of URL segments
$urlSegments = explode('/', trim($currentUrl, '/'));

// Get the last segment and trim any extra characters
$lastSegment = end($urlSegments);
$lastSegment = trim($lastSegment, '?'); // Remove any trailing '?'

// Check if the last segment contains a number
if (is_numeric($lastSegment)) {
    // The last segment is numeric, so it's the record ID
    $recordId = $lastSegment;

    // Output the extracted record ID
} else {
    // Handle the case where no number is found
    $recordId = "0";
}

After that I could use ExecuteScalar to grab the other values I needed in the pdf header section.
So it's working now but its messy

If there a global variable I can reference it would be better.


mobhar
User
Posts: 11660

Post by mobhar »

Assume your id field name is OrderID, then you may simply use the following code to check the output in PageRender server event:

$this->setMessage("OrderID = " . Route("OrderID"));

Post Reply