How to call functions in other PHP files? - php

I am trying to call a function from one file to another in PHP, however, I am getting the following error:
Fatal error: Call to undefined method SQLite3::print_model_query_form()
I have two files. The first - dbfunctions.php contains the method print_model_query_form().
The second file query_models contains the following code:
include_once("functions/dbfunctions.php");
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
print $db->print_model_query_form("query_models.php");
The function looks a little like this:
function print_model_query_form($action, $current_values = 0){
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
if($current_values){
// set to previous values.
}else{
// get POST values.
}
// Code to print query form.
}
Thanks in advance for any help.

Since you only hit fatal error on 3rd line, $db should be instantiated successfully. So, the issue should be the print_model_query_form method.
Referring to PHP:SQLite3 - Manual
, there is no such built in method called print_model_query_form.
[Edit 1]
Try to use require_once instead of include_once to make sure you have included dbfunctions.php successfully.
[Edit 2]
Check If you are using PHP's built in SQLite3 class (check your php.ini for extension=php_sqlite3.dll or extension=php_sqlite3.so).
If this is the case, check your dbfunctions.php for:-
class Something
new SQLite3
function print_model_query_form
If all the above exists then you should replace your 2nd line with,
$db = new Something(..);
Note: It would be better if you can show dbfunctions.php instead or letting us make assumptions based on guessing.

Related

[swagger-codegen][php-symfony] No entrypoint in generated code?

I am attempting to generate a php-symfony server stub using swagger-codgen but I am not getting an index.php (or app.php or anything like that).
When I generate a server stub using the php-silex preset I get an index.php. If I google "silex .htaccess" I can easily find the mod_rewrite directives to get it working. However, when I generate code using the php-symfony preset I cannot find the entry point.
I attempted to combine the php-silex and php-symfony generated codebases (since silex is a symfony project) like so:
(index.php):
use Swagger\Server\Controller\GetTypesListController;
$app->GET('/image/list/types', function (Application $app, Request $request) {
$tlc = new GetTypesListController();
$response = $tlc->getTypeListAction($request);
return $response;
//new Response('How about implementing getTypeList as a GET method ?');
});
However, then I get errors like:
"Fatal error: Uncaught Error: Call to a member function getApiHandler() on null"
I modified the getApiHandler function of my generated GetTypesListController to instantiate the apiServer variable:
public function getApiHandler()
{
if (!isset($this->apiServer)){
$this->apiServer = new \Swagger\Server\Api\ApiServer();
}
return $this->apiServer->getApiHandler('getTypesList');
}
but this just gave me another error:
Undefined property: Swagger\Server\Controller\GetTypesListController::$container
(referencing SymfonyBundle-php/Controller/Controller.php on line 149)
I am obviously doing something wrong. I would really appreciate being pointed in the right direction.
Edit: I do realize that I'm not supposed to call controllers inside controllers.. I assume that I'm not supposed to combine these two generated codebases at all.

php how to redeclare class in a for loop

Im using a library called PDFmerger , naturally, it merges pdfs together. I wish to run a cron every night that builds these to have them ready for the next day. The issue is when I run the code after the first one it fails because it is attempting to redeclare the class.
The for statement...
foreach ($pdfs_to_build as $pdf) {
// do stuff
$this->merge_pdfs($pdf['pdf_id']);
}
And my pdf merging code that gets ran in the loop...
$pdf = new PDFMerger;
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-1.pdf', '1');
$pdf->addPDF(DIR_FINAL_PDFS.$pdf_id.'/custom-page-1.pdf','1');
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-2.pdf', '1');
$pdf->merge('file', DIR_FINAL_PDFS.$pdf_id.'/final-build-'.$pdf_id.'.pdf');
Error after the first 1 is complete and we've onto the next one...
Fatal error: Cannot redeclare class PDFMerger in /var/www/example/pdfmerger/PDFMerger.php on line 24
Im wondering is there a workaround for this?
The problem isn't that you're calling new PDFMerger twice, that's perfectly acceptable. The problem is that the file:
/var/www/example/pdfmerger/PDFMerger.php
Is being include'd (or otherwise executed) twice, which has on line 24:
class PDFMerger {
That is what is triggering your error, that class has already been declared. Figure out why that file is being included twice and your error will clear up.
Your code is fine. That won't fail. However, not listed in what you posted, you are including PDFMerger.php.
Probably by having the include 'PDFMerger.php'; shown at https://pdfmerger.codeplex.com/ run inside the loop.
Just move that line out of the loop and/or change the include to include_once.

Include PHP File and execute it multiple times

Yeah I know this is probably not the best way to perform this. However I have a script which I would like to call multiple times with parameters and hand over two variables. The code I have so far looks like this:
$testfeld = array('User1'=>'400028',
'User2'=>'400027'
);
foreach($testfeld as $key=>$value) {
$_GET['cmd'] = 'modify';
include("testmodify.php");
}
If I run this code, I get an error message:
Fatal error: Cannot redeclare show() (previously declared in testmodify.php:14) in testmodify.php on line 39
If I perform this, it only modifys me the first entry of the array and then throws me the error message above.
Sadly, I have only read access to testmodify.php.... So is there a way to perform this include with all the content of the array?
Put your code in the function, include it once and call as much as you want.
You are receiving this error because the file testmodify.php contains the definition for "show". A second inclusion of the file caused by your loop attempts to define the object show again, which cannot be done.
Here is another SO question about someone trying to redefine a function.
Php and redifining
Edit: the other answer by MilanG is how you could go about fixing it.
You can do something like (in your testmodify.php script):
if (!function_exists("show")) {
function show($parameters) {
/* do your stuff here */
}
}
A better thing to do, in this case, would be to perform include_once() in your testmodify.php script, making it point to a script file where you define your show() function. My approach should also work for you.
Hope that helps :)

proper use of get_records in moodle

I wonder how can I find my moodle's database contents.
I want to elaborate users etc.
My first try is with PHP code using global $DB but there is an error message:
Fatal error: Call to a member function get_record() on a non-object in C:\xampp\htdocs\test.php on line 3
I use the getrecord() function but I didn't do it right.
Can anybody help me? The name of my database is "base".
Note: moodle version is 2.7.2
This is an example that should allow you to access the Moodle DB.
require_once(dirname(__FILE__).'/../../config.php'; // Insert as many '/../' as you need to get from the subdirectory your file is in, back up to Moodle's config.php
function my_function() {
global $DB;
$record = $DB->get_record('user', array('id' => 2));
var_dump($record);
}
my_function();

function to convert string to integer -- call to undefined function

I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit

Categories