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

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

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

Post by mobhar »

Now we can use Custom Files with Include common files disabled.

**1. Make sure you have already updated template to the latest version from Tools -> Update Template.

**2. Create a Custom File and name it as test.php, then copy and paste this code into Content section of the Custom File of demo2021 project of PHPMaker 2021:

<?php

namespace PHPMaker2021\demo2021; 

use PHPMaker2021\demo2021\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

global $RELATIVE_PATH;

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

require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
require_once "src/userfn.php";

echo "<h3>Custom File without Include common files option</h3>";

$sql = "SELECT `Model` FROM `models`"; // define your SQL
$stmt = ExecuteQuery($sql); // execute the query
$value = ""; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
while ($row = $stmt->fetch()) { // loop
$value .= $row["Model"] . "<br>"; // in case the result returns more than one record, display it and separated by line break
} // end loop
echo "Here is the Models list: <br>" . $value; // display the result
} else { // if there are no result
echo "No record found."; // display the message
} // end of check condition

?>

**3. Re-generate ALL the script files, as usual.

**4. Visit the Custom File from browser: http://localhost/demo2021/test.php

The Limitation: The styles, header, footer, sidebar won't be implemented in this Custom File.

This trick is useful if you want to optimize the built-in functions regardless of the appearance / layout of your web application.


mansour
User
Posts: 315

Post by mansour »

Hi,
I used your suggested tip and codes. The problem is that the LOCALE settings do NOT work as usual!
e.g.
echo FormatCurrency($publicfee, 0, -2, -2, -2); >>>> returns $ sign even with FA locale (Persian project)?
Any fix?

Thanks


mobhar
User
Posts: 11841

Post by mobhar »

Which part did you use? Did you enable or disable Include common files?


mansour
User
Posts: 315

Post by mansour »

Hi,
I disabled Include common files, because of I used ajax to retrieve data from that custom file.

Thanks


mobhar
User
Posts: 11841

Post by mobhar »

If you disable Include common files option, then you cannot use FormatCurrency function properly.


mansour
User
Posts: 315

Post by mansour »

Hi,
It seems I can NOT use language/session variables when the "Include common files" is disabled.
Is there any way to use session and also language variables in custom files without Including common files?
e.g.:
ExecuteHtml function does NOT return field captions. (custom file without Including common files)
Thanks
Mansour


mobhar
User
Posts: 11841

Post by mobhar »

I don't think so. You should enable Include common files option for your Custom File for such case.


mansour
User
Posts: 315

Post by mansour »

Hi,
It seems this does NOT work with v2024. may I ask you suggest the above code for 2024?
thanks



Post Reply