Jump to content


Photo

help with code file/directory related


  • Please log in to reply
9 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 06 October 2006 - 08:59 PM

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
<!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>
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.
<?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.
?>

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?

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 October 2006 - 09:59 PM

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.

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


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...

fopen("$_SESSION[template]", "r")

should be....

fopen($_SESSION['template'], "r")


#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 06 October 2006 - 10:08 PM

the id is coming from the $id variable.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 October 2006 - 11:41 PM

You've done a wonderfull job of clarifying.

The $id variable appears once (undefined) in your first script, and not at all in your second.

Your on your own Im afraid.

#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 07 October 2006 - 12:10 AM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 07 October 2006 - 12:53 AM

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'.
Legend has it that reading the manual never killed anyone.
My site

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 07 October 2006 - 01:06 AM

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
<?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>
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)
<?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>
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??

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 October 2006 - 01:28 AM

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 mysql_insert_id instead of wasting resources by running a new query.

#9 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 07 October 2006 - 01:32 AM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 October 2006 - 01:39 AM

Im not going to get into application design logic, its something you understand with experience. All clients say they dont care how 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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users