A solution to a big problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

A solution to a big problem.

Post by Jim »

I'm in the process of creating a program with about 16 different pages that need to connect to a MySQL DB. In each page, I include a config.php file that has all the information I need on it. Now here's the problem:

To include the page, I have to have the exact path to it. Since I'm going to distribute this program, that path is going to differ per each user. I don't want users to have to go through 4 pages (there would be 4 config files if I placed one in each folder that I'm using in the program) to edit the paths. Rather, I'd like to have them edit one file to use on all the pages.

Any ideas on how this might work? I've been thinking about this very hard and can't come up with anything.

Then again, I'm a n00b :)

Also, how do I keep certain actions from happening until I submit a form? (The form and code are on the same page).

Thanks!
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

From each file, simply do an

Code: Select all

include("../../path/to/config.php");
Avoid using the full path name, just use the relative path name.

And if you want to include files relative to the file you are currently in, as opposed to PHP's traditional way of including files from the file that is run, you can use this code:

Code: Select all

include_once dirname(__FILE__).'/path/to/file.php';
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

In your script you might have something like, lets call it myscript.php

Code: Select all

<?

  if($dostuff=='yes')&#123;
    ..your stuff here..
  &#125;
print "<form action="myscript.php" method...>
           <type="hidden" name="dostuff" value="yes">
           ...
         </form>";

?>
So first time your script is called $dostuff is not set to anything and any code here will not be executed.

When you submit the form a hidden field 'dostuff' has been assigned the value of 'yes', and the form calls its own script 'myscript.php'. This time the script is run but $dostuff now has a value and the code is executed in the if statement.

It doesn't have to be a hidden field, it can be any of the fields on your form you check or it can be passed in the URL i.e. <form action="myscript.php?dostuff=yes">

Hope this helps
Mike
Post Reply