bpross
bpross

Reputation: 353

Call to function error

Linux System

Looking for help with my function. I'm new learning them. It was mentioned to create a function to keep from writing the same code too many times.

I have found many pages about them and they have helped but I'm still having issues getting mine to work.

It is not giving any log errors and nothing is being created. I always get errors when starting with something so I know I'm missing something.

Here is what I have so far.

Thank you in advance.

Bob

<?php

$dirPath = "Path to File Location";
$buildPath = "Path to File Location";

function createFiles() {
    for ($i = 1; $i < 45; ++$i) {
        $filename = $buildPath . '/$area'. sprintf("%02s", $i) . '.php';
        if (!file_exists($buildPath$filename)) {
            $myfile = fopen($buildPath$filename, "w") or die("Unable to open item$i file!"); 
            fwrite($myfile, $txt1);
            fwrite($myfile, $addtitle);
            fwrite($myfile, $incltxt);
            fclose($myfile);
            chmod("$buildPath"."$area"."$i".".php", 0755);
        }
        // fopen, fwrite, fcloses...
        // filename for delete page setup
        if (!file_exists("$dirPath"."delete.page.php")) {
            $myfile = fopen("$dirPath"."delete.page.php", "w") or die("Unable to open file!");
            $txt = "<option value=\"\">Select Page to Delete......</option>\n
                    <option value=\"generalinfo.tx\">General Information Page.</option>\n
                    <option value=\"$filename\">$area Pg. $i</option>\n";
            fwrite($myfile, $txt);
            fclose($myfile);
            chmod("$dirPath"."delete.page.php", 0755);
        }
        else
            {
                $myfile = fopen("$dirPath"."delete.page.php", "a") or die("Unable to open file!");
                $txt = "<option value=\"$filename\">$area Pg. $i</option>\n";
                fwrite($myfile, $txt);
                fclose($myfile);
                chmod("$dirPath"."delete.page.php", 0755);
            }
            break;
    }
    return true;
}


    if ($i == 1) {
        createFiles();
    }
    // Once we get more than 4 items written in each file we need a new file.
    // This can happen maximum 11 times for each session
    if ($i == 5) {
        createFiles();
    }
    if ($i == 9) {
        createFiles();
    }
    if ($i == 13) {
        createFiles();
    }
    if ($i == 17) {
        createFiles();
    }
    etc......
    if($i == 49) {
        echo "<br />Maximum of 12 pages hit. Time to delete a few";
    }

?>

Upvotes: 0

Views: 75

Answers (2)

bpross
bpross

Reputation: 353

Here is my updated code. I tried one more thing before I was going to give up, and here is the one extra line I added and changes I made.

Stupid error. I don't know how you guys can do this all the time. This is the first time I messed with a function in php and spent all day on it. But it's working so I figured I'd share the changes for all.

function createFiles() {

extract($GLOBALS);

     for ($n = 1; $n < 45; ++$n) {
        $filename = $buildPath . $area . sprintf("%02s", $n) . '.php';
        $filenameb = $area . sprintf("%02s", $n) . '.php';
                                    //^ length of number you need, 2 in this case
        if (!file_exists($filename)) {
        $myfile = fopen($filename, "w") or die("Unable to open item$i file!"); 
        fwrite($myfile, $txt1);
        fwrite($myfile, $addtitle);
        fwrite($myfile, $incltxt);
        fclose($myfile);
        chmod("$filename", 0755);
     }
      break;
     }
if (!file_exists("$dirPath"."delete.page.php")) {
    $myfile = fopen("$dirPath"."delete.page.php", "w") or die("Unable to open file!");
    $txt = "<option value=\"\">Select Page to Delete......</option>\n
            <option value=\"generalinfo.php\">General Information Page.</option>\n
            <option value=\"$filenameb\">$area Pg. $n</option>\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    chmod("$dirPath"."delete.page.php", 0755);
}
else
    {
    $myfile = fopen("$dirPath"."delete.page.php", "a") or die("Unable to open file!");
    $txt = "<option value=\"$filenameb\">$area Pg. $n</option>\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    chmod("$dirPath"."delete.page.php", 0755);
    }
return true;

if ($i == 1) {
    createFiles();
}
// Once we get more than 4 items written in each file we need a new file.
// This can happen maximum 11 times for each session
if ($i == 5) {
    createFiles();
}
if ($i == 9) {
    createFiles();
}
if ($i == 13) {
    createFiles();
}
if ($i == 17) {
    createFiles();
}
etc......
if($i == 49) {
    echo "<br />Maximum of 12 pages hit. Time to delete a few";

Upvotes: 0

James
James

Reputation: 4803

$i is if it equals, and that part is working.

If that part is working, then you are not showing all your code!

Take this example, which is fundamentally the same as what you are doing:

function createFiles()
 {
  $i = 3;
 }


if ($i == 3)
 {
  createFiles();
  echo "is 3";
 }
else
 {
  echo "is not 3";
 }

This will ALWAYS echo "is not 3". The var is not set, as the one being set in the function is A) Not within the same global scope, and B) the function has never even been called.

How can a var being manipulated within a function determine in code outside the function if that same function should be called if the var within the function is set to something?
It's catch 22.

Your function call createFiles() will only execute if $i == 1, and the code you posted does not set that var, it will always be null or unset (etc).
That means your if statement which calls the function will never work, and always return FALSE, and so the function will never be called.

You also have to understand global scope, in that vars within the function stay within the function, and any outside the function are separate.
Essentially you currently have two vars, the one inside the function, and the one outside it, although still both named $i their global scope does not marry, so they never interact with one-another.

You can pass a var into the function to allow vars and their data outside the function to be used within the function, and return one out of the function for using the data outside the function.

Also, every time a user uses your code, any vars will be reset, meaning your bunch of ifs to determine if they have used X times will not work as desired.
You cannot do this client side.

You could set a session or cookie, but the client can easily bypass this if they want to (cleaning browser data).
If you truly want to limit them, you will need a persistent data storage method which the client has no access to - either a file, or more solid method would be to increment a database value.
Then if they can use the form again the next day, a cron could run through the table and clear up as required.

EDIT
To get your script working, try introducing a few stages to make it simple.

Stage1: Get the function working on it's own, adding files (or whatever its purpose is for).
Stage2: Manipulate the function being called how you want - i.e. only call function if user has used function < 10 (etc).

You could even break stage1 into a few sub stages, in Stage1a, get function accessing correct directory; Stage1b, get function accessing files; Stage1c, get function writing to files, etc.

Try to avoid writing a script with numerous areas of functionality all in one go, and break it into manageable logical chunks. Doing this, will also help you see separation between different concerns.

Upvotes: 2

Related Questions