How to Load Data from Database into Custom Files (v2019)

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11703

How to Load Data from Database into Custom Files (v2019)

Post by mobhar »

For those of you who want to need to know about how to load data from Database into the Custom Files using PHPMaker v2019, then here is the simple code to do that.

Note: "Include common files" not enabled for this case.

In this following example, we are using the demo project. That's why you see the "demo2019" as a part of namespace. Of course, you need to adjust it to your own project name. You may save then this Custom File as "load-data.php" (for example).

In addition, in this example, since the data that will be loaded depends on the "ID" parameter, then you may simply call it by using: load-data.php?ID=1 if you want to try it by yourself. It uses "ExecuteScalar" global function to get the value of the first column of the first record that found. Of course you may ehanced it using another global function such as "Execute", "ExecuteRow", and so forth.

I learned and know this, after going through several experiments for my AJAX, and this "load-data.php" file is the data source that I created for my AJAX Callback. Hopefully this helps you. Happy coding, everyone!

<?php
namespace PHPMaker2019\demo2019; // Don't forget to always use namespace this since v2019; adjust it to yours!
if (session_id() == "") session_start(); // Init session data
ob_start(); // Turn on output buffering
require_once "autoload.php"; // Everything is in here, so easy, right? :-)
if (!empty(Get("ID"))) { // Check the param of ID first
echo "The values is: " . ExecuteScalar("SELECT Model FROM models WHERE ID = ".$_GET['ID']); // Display it whatever the result is
} else {
echo 'Unknown ID or data not found'; // If not found or ID is not supplied
}
?>


sticcino
User
Posts: 1043

Post by sticcino »

I'm struggling as well with custom files that don't need the full framework. I have many custom files for simple ajax calls and routines that are now totally dead. i've tried to use the Api_Action() to replace some of the calls, but notice it requires the calls to be authenticated, even thought the user is logged in -- for a test even with the admistrator loggin i'm getting permission denied errors.

I have noticed that the following is not passed to the custom files as well:

global $Secuirty object and CurrentUserID() is NULL, and have also noticed that $Verify1 = &$Page is NULL

i've added this to one of my custom files:

<?php

namespace PHPMaker2021\xxxxxx;

// Page object
$Verify1 = &$Page;
?>
<?php
// Require files
$RELATIVE_PATH = "../"; <== will change depending on where you file is located

if (session_id() == "") session_start(); // Init session data
ob_start(); // Turn on output buffering

// Auto load
require_once $RELATIVE_PATH."vendor/autoload.php";

require_once $RELATIVE_PATH."src/constants.php";
require_once $RELATIVE_PATH."src/config.php";
require_once $RELATIVE_PATH."src/phpfn.php";
require_once $RELATIVE_PATH."src/userfn.php";
?>

i'm able to run functions from the userfn.php but with the security object and other CurrentUserID() functions,

as well your executescalar will not work... as well as any execute functions with this method in 2021....

mobhar
User
Posts: 11703

Post by mobhar »

Custom Files in v2021 is quite difference with Custom Files in the previous versions of PHPMaker. In other words, the trick in my post above cannot be used for v2021.

You need to enable "Include common files" option for v2021 if you want to use some common functions that provided by the generated script files.

If you do not enable that option, then even you write your code from the project side, then the generated Custom File will have the following content only:

<? // No class file ?>

So, again, you need to enable "Include common files" for your Custom Files in v2021. Of course you may still be able to enable "No header/footer" option from the "Generate" dialog window for your Custom Files, if you don't want to display the header/footer for your Custom Files, and this option is very useful for your own custom files such as simple Ajax calls, etc.

Post Reply