Jump to content

help with code file/directory related


Ninjakreborn

Recommended Posts

Ok I could really, really use some help here, I have already constructed my database totally.  It is already complete and ready to go, my tables are all prepared, the directory creation works, duplicating and moving the file seems to work, but there is something I need to do now, that I absolutely can't figure out.
Ok.
Now, First of all I have a template page, this is page is called template1.php, there is some other pages, but there all the same, just different names, here is the code.
Template1.php
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template 1</title>
</head>
<body>
<div class="content">
<?php
$select = "SELECT * FROM usercms WHERE id = '$id';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {
echo "<h4>" . stripslashes($row['header']) . "</h4>";
echo "<p>" . stripslashes($row['text']) . "</p>";
}
?>
</div>
</body>
</html>[/code]
Now, I have an area of code, in the page that returns information from paypal, and does some database work.  In that script I create a directory, open the template page(that contains the above information), adn write it to antoher file and save it to that other directory that I created.  While leaving the original file untouched, everything is done, but I ahve to find a way to replace the id value int he select query on the template page, to be dynamically replaced with the user id on the other page, how do I do that.
Here is the code i have to that is doing the directory and file handling.
[code]
<?php
// VERY IMPORTANT, everything below is used for directory creation.
$var = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '"&#".ord($0).";"', $_SESSION['subdomain']);
$dir = "userpages/$var";
mkdir("$dir", 0700);
if (!mkdir) {
$errorhandler .= "unable to create the directory. Please contact support or try again.<br />";
}

$infile = fopen("$_SESSION[template]", "r") // as readonly
while (!feof($infile))
{
$buffer = fread($infile, filesize("$_SESSION[template]"));
}
fclose($infile);
// here I can do all my changes to the buffer variable(which contains the file information)
$outfile = fopen("index.php", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);

// VERY IMPORTANT, everything above here is used for directory creation.
?>
[/code]

So far everything seems to be in order.  I need to be able to do it in that section that says here I can do all my changes to the buffer variable, I just need to get that id to be replaced dynamically by the one coming from this script any advice?
Link to comment
Share on other sites

As Ive said before, I really think your going about this whole thing the wrong way. Anyway, I guess your client is stuck with it now.

[quote]I just need to get that id to be replaced dynamically by the one coming from this script[/quote]

Where is the id comming from in this script? Your going to need to be much clearer in your explination.

PS: You also have syntax errors. This...

[code=php:0]
fopen("$_SESSION[template]", "r")
[/code]

should be....

[code=php:0]
fopen($_SESSION['template'], "r")
[/code]
Link to comment
Share on other sites

I mean, I have the is populated.  I have it coming from the database, I am sure I have it, I put the information for the cms into the cms table, with the userid, of the person who is constructing the page.  Now I do a query to select that user by his username(they are unique), then retrieve that id, into the

$id variable
Now I need to get the contents of $id, to replace the contents of the $id variable in that dynamic page.
Link to comment
Share on other sites

It's still incoherent, so any help you get may or may not be applicable.

I *think* what you're trying to do is UPDATE some_database_table SET some_field to $something_or_other WHERE (probably) userid = '$userid_value'.
Link to comment
Share on other sites

Ok, let me explain like this.

I have sitting in the root directory a file named template1.php
it contains the following code.
template1.php
[code]<?php
session_start();
mysql_connect("localhost", "#####", "#####");
mysql_select_db("hasbadse_hbservice");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Template 1</title>
</head>
<body>
<div class="content">
<?php
$select = "SELECT * FROM usercms WHERE id = '$id';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {
echo "<h4>" . stripslashes($row['header']) . "</h4>";
echo "<p>" . stripslashes($row['text']) . "</p>";
}
?>
</div>
</body>
</html>[/code]
As you can see, this is it, there is a database connection, to keep it with a live connection, a session_start() to make sure sessions are there(for future versions of the website),
Now you can see there is a query there, it has 1 variable that you are not sure where the information is coming from.  It is coming you another page.  Ok, imagine this file template1.php is sitting on the server.

Now I have my signup script, this is where they come back to, after paying through paypl, where everything happens, that is below.
thankyou.php (everything that comes back from paypal, comes to here)
[code]<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Thank You</title>
</head>
<body>
<?php
if ($_SESSION['controller'] == true) {
mysql_connect("localhost", "#####", "####");
mysql_select_db("hasbadse_hbservice");
// create user account.
$errorhandler = "";
$insert = "INSERT INTO userinfo SET username = '$_SESSION[username]', password = '$_SESSION[password]', email = '$_SESSION[email]', subdomainname = '$_SESSION[subdomain]';";
mysql_query($insert);
$select = "SELECT * FROM userinfo WHERE username = '$_SESSION[username]';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) { // retrieve userid with user account.
$id = $row['id'];
} // end getting userid.

$insertdn = "INSERT INTO subdomain (path, subdomainname, userid) VALUES ('test', '$_SESSION[subdomain]', '$id');";
if (!mysql_query($insertdn)) {
$errorhandler .= "There was a problem, please contact support immediately.<br />";
}
// VERY IMPORTANT, everything below is used for directory creation.
$var = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '"&#".ord($0).";"', $_SESSION['subdomain']);
$dir = "userpages/$var";
mkdir("$dir", 0700);
if (!mkdir) {
$errorhandler .= "unable to create the directory. Please contact support or try again.<br />";
}

$infile = fopen("$_SESSION[template]", "r") // as readonly
while (!feof($infile))
{
$buffer = fread($infile, filesize("$_SESSION[template]"));
}
fclose($infile);
// here I can do all my changes to the buffer variable(which contains the file information)
$outfile = fopen("index.php", "w") // open writeable
fwrite($outfile, $buffer)
fclose ($outfile);

// VERY IMPORTANT, everything above here is used for directory creation.

if ($errorhandler != "") {
echo "<span style=\"color:red;\">";
echo $errorhandler;
echo "</span>";
}
if ($errorhandler == "") {
?>
<h4>Thank You</h4>
<p>Thank you for your purchase, you can go over to the administration page, and login there.</p>
<a href="/useradmin.php" title="User Admin">User Admin</a>
<?php
}
}else {
echo "You have to go through the entire signup process, before coming to this page, in order <br />";
echo "for your account to be able to be created.<br />";
}
?>
</body>
</html>[/code]
Ok, this will explain where the id is suppose to come from.
I create the person a user account, then immediately go back to the database to retrieve the id the db associated with that user.
THen I database the information on the subdomain that is to be created soon, along with the userid of the person that is creating it.

Then I database the information I had brought to another form that contained a header/text for a cms.  I database the header, text, and userid of the person putting int he cms info.  Each entry is one section of text.  Now I need to create a directory named the name they choose for the subdomain, I need to open template1.php with the data it currently holds, trap the contents in a variable, and close the file.  I did all of that, NOW I need to modify the value in that file $id, to hold the value of the id of the user.  This will allow the system to retrieve that user id when the page goes live, so it shows the right information.
THat part I need to do, I have already set up to save the new file to the right directory, adn close it when finished.  The only thing is getting that id, to be replaced by the other id??
Link to comment
Share on other sites

Sorry but your logic here is just rediculous. Why does each user need there own hard coded template1.php? This thing is going to be a nightmare to maintain. Anyway... Im not the client (and should bite my tongue).

You'll need to run $buffer through str_replace and replace the string $id with the value of your $id variable. Which by the way you could retrieve using [url=http://php.net/mysql_insert_id]mysql_insert_id[/url] instead of wasting resources by running a new query.
Link to comment
Share on other sites

Ok, if you get the general flow of the site, then what should I do.  The client doesn't care "how" it's done he just want's it done. If there is a better logic I can put behind it, or a better idea of how I can program it tell me, even if I have to rewrite the whole thing.

I am tired of sitting here like I know everything.
I want advice, if you give me something, that I understnad, and makes sense, even if I have to rewrite the whole thing, then so-be-it.

Let me know what you think, what can I do differently to be more effective, easier to mantain, if you understand the idea.
THe template pages are pages tthat the person can choose from, ad design will be added behind each tempalte, and more templates added, so they can choose what design they want.  Let me know what you think, and I will do whatever you suggest.
Link to comment
Share on other sites

Im not going to get into application design logic, its something you understand with experience. All clients say they dont care [i]how[/i] because they dont understand the logic, nor should or do they need to. What clients dont like is when they go to add a feature to something they've had built only to be told that the old code is ilogical and will need reworking for features to be implimented.

Most clients expect (or hope for) there developer to get it right the first time and that the code they are paying for to be maintainable, secure and efficient.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.