Solved

PHP

Posted on 2003-10-28
27
275 Views
Last Modified: 2010-04-06
Hi everyone, I hope this is an easy one – I would simply like to combine a bunch of PHP functions into one script.  Here is the architecture:

1) I send a PHP script a series of variables that have a value.  If these variables have a value, PHP fires a script:

varA=1
varB=2
varC=3
varD=4
…and so on (a total of 30 variables in sequential order)

2) If varA = ‘something’ then do the following:
<?
$file = fopen("./Folder/" . "NewFile1", "w");
fwrite($file,"$varA");
fclose($file);
?>

If varB = ‘somehting’ then do the following:
<?
$file = fopen("./Folder/" . "NewFile2", "w");
fwrite($file,"$varB");
fclose($file);
?>

This is repeated all the way down to the 30th variable.

Hope this is easy.
0
Comment
Question by:Donavan_usa
  • 14
  • 13
27 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
Comment Utility
i don't quite understand what you're trying to do here but here's my shot at it. if you're sending the script variables through a form using the GET method:

if($_GET["vars"]) {
  $sizeof = count($_GET["vars"]);
  for($i = 0; $sizeof > $i; $i++){
    $j = $i +1;
    $var[$i] = $j;
  }
  for($a = 0; $sizeof > $a; $a++) {
    $b = $a + 1;
    $file = fopen("./Folder/" . "NewFile$b", "w");
    fwrite($file,"$var["0"]");
    fclose($file);
  }
}
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
if youre using POST, swap $_GET for $_POST.. if this isn't what you were thinking of then could you give a bit more detail about how you want the system to work, then i can rewrite the script

cheers,

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz for your comment.  Here is some more information:

1) I send the PHP script the 30 variables from within an application (flash).  Each variable has a value (varA=1, varB=2, varC=3, varD=4).

2) In the PHP script, what I would like to happen is this: as each variable is tested and determined to have a value, the PHP script writes a 1 text file for each:

If varA = ‘something’ then do the following:
<?
$file = fopen("./Folder/" . "NewFile1", "w");
fwrite($file,"$varA");
fclose($file);
?>

The above script is repeated 30 times, so I imagine 30 'if' statements in PHP and 30 text files written.  The reason I would like to test each variable to determine if it has a value, is because I only would like to write a text file for each variable that indeed is true.  Probably something like this:

if (!isset($varA));

Cheers
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
ok well I don't know how flash delivers variables to forms, but I assume it'll be using the POST method(if you can see the variables in the address bar then it'll be using GET). it'd be preferable to use var[0] for your variables, or at least var1 var2 etc. this is because there aren't 30 letters. if you tell me how to get round this problem then i'll write you the script

cheers,

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz for your comment: what flash does is it loads the variables into the PHP script using the POST method - it looks like this:

loadVariablesNum("Cart.php", 0, "POST");

The above script sends the PHP script called Cart.php 30 variables, and yes, they are actually named using numbers: ex. varA1, varA2, varA3, varA4.  Where my questions rests then, is when the PHP script receives all of these 30 variables, if the variable has a value (at times maybe only the first 5 variables has a value) it executes the following PHP code:

<?
If varA1 = ‘something’ then do the following:
$file = fopen("./Folder/" . "NewFile1", "w");
fwrite($file,"$varA1");
fclose($file);

If varA2 = ‘something’ then do the following:
$file = fopen("./Folder/" . "NewFile2", "w");
fwrite($file,"$varA2");
fclose($file);

?>

This scripts needs to be written with 'if' statements all the way to 30, because at any given time, all 30 variables may be true.  The reason I use an if statement is that sometimes, like I mentioned, only a few of the 30 variables may have a value, thus I don't want 30 text files written everytime.

Thanks,
Cheers
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "varA" . $j;
  if($_POST[$index] != "") {
    $file = fopen("./Folder/" . "NewFile$j", "w");
    fwrite($file,$_POST["index"]);
    fclose($file);
  }
}
?>

at the moment this script checks on the line with 'if($_POST[$index] != "") {' that the variable is not an empty string  (i.e. ""). if you want it to check for another value, you can use == "your value" or if you want to check whether the variable has been set at all, you can just have if($_POST[$index]) {

tell me how you get on,

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz, we are getting closer.  If the above script receives lets say 20 variables that have a value (varA1=cat, varA2=dog, ...all the way to 20), will it go ahead and write 20 separate text files: NewFile1, NewFile2, etc.?

Thanks
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
yes should do, as long as the variable is not an empty string
0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz, I am working on the script, but it is not working yet.  Here is what I now have:

<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/" . "NewFile$j", "w");
    fwrite($file,$_POST["index"]);
    fclose($file);
  }
}
?>

When I pass the above PHP script the variables: vcartG1=cat, vcartG2=dog, vcartG3=bird - it does not write the text files.  Any thoughts?

Thanks

0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz, I am working on the script, but it is not working yet.  Here is what I now have:

<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/" . "NewFile$j", "w");
    fwrite($file,$_POST["index"]);
    fclose($file);
  }
}
?>

When I pass the above PHP script the variables: vcartG1=cat, vcartG2=dog, vcartG3=bird - it does not write the text files.  Any thoughts?

Thanks

0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/" . "NewFile$j", "w");
    fwrite($file,$_POST[$index]);
    fclose($file);
  }
}
?>

sorry, i accidentally put $_POST["index"] instead of $_POST[$index]

am i right in thinking the files are being created with no text in them?

anyway try that out and get back to me,

cheers,

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Hi loz, funny thing - I actually tried that correction already and it also didn't write the files.

<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/" . "NewFile$j", "w");
    fwrite($file,$_POST[$index]);
    fclose($file);
  }
}
?>

In light of your question, each "vcartG1 through vcartG30" does have a bunch of text contained within their value.  For example:

I pass the above PHP Script the following values:
vcartG1=the world is a wonderful place
vcartG2=444
vcartG3=amazing
vcartG4=wow
etc.. etc.,

What I am anticipating is the PHP script will write a new text file named NewFile1.txt that equals vcartG1, and then write another text file named NewFile2.txt that contains vcartG2 and so on.

Your thoughts?

Thanks





0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
<?
for(i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/NewFile$j.txt", "w");
    print "writing..";
    if(!fwrite($file,$_POST[$index])) {
      print $_POST[$index] . " unsuccessful<br />";
    } else {
      print $_POST[$index] . " successful<br />";
    }
    fclose($file);
  }
}
?>

see what happens when that runs
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Donavan_usa
Comment Utility
Hi loz, I tried the above with no success.  Here is the script I wrote that does write the files, the exception is it only writes 1 at a time:

<?
$file = fopen("./Folder/Orders/" . "$file1", "w");
fwrite($file,"$vcartG1");
fclose($file);
?>

Then I add the above script again and it writes the second text file with the contents of vcartG2:

$file = fopen("./Folder/Orders/" . "$file2", "w");
fwrite($file,"$vcartG2");
fclose($file);

And so on down to 30.

I know we must be close,

Thanks
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
heh, i must be quite tired, the for clause is missing a $ at the start

<?
for($i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/NewFile$j.txt", "w");
    print "writing..";
    if(!fwrite($file,$_POST[$index])) {
      print $_POST[$index] . " unsuccessful<br />";
    } else {
      print $_POST[$index] . " successful<br />";
    }
    fclose($file);
  }
}
?>

try that, ill test it myself too
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
incidentally you need to perform chmod 0777 on the directory you're writing the text files into so that php has permission to write to it
0
 

Author Comment

by:Donavan_usa
Comment Utility
Thanks loz, I missed the '$' above also.

One last bump and we are done.

Here is the code so far:

<?
for($i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folder/Orders/" . "NewFile$j", "w");
    fwrite($file,$_POST[$index]);
    fclose($file);
  }
}
?>

One last item: what I would like to do is name the files after a variable coming into the PHP script.  Instead of using "NewFile$j" which names the new text files NewFile1, NewFile2, etc., I am sending the PHP script 30 distinct names, and each name is contained in the following variables:

name1 = 2003.7742.File1
name2 = 2003.7943.File1
name3 = 2003.8812.File1

As you can see, each file name is distinct - how would you incorporate this into the line:

$file = fopen("./Folder/Orders/" . "NewFile$j", "w");

Thanks for all your help,



0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
so the names of the files are coming in as seperate variables?

$findex = "name" . $j;
$file = fopen("./Folder/Orders/" . $_POST[$findex] . ".txt", "w");
0
 

Author Comment

by:Donavan_usa
Comment Utility
Hi loz, you have been a great help, a couple quick notes:

1) Instead of having your 'for' loop count to 30, how would you make it count to, let's say infinity?

2) One note: have you noticed with a PHP script that they need time to execute before you call another PHP script?  Like the above example, it seems to consistently trip itself up around the 11th time it has to write the text file.  Any thoughts?

3) In the above script, can you exchange the '$_POST' with something else?  Why do you include this?  Could it read as follows:


<?
for($i = 0; 30 > $i; $i++){
  $j = $i + 1;
  $index = "vcartG" . $j;
$file = "name" . $j;

if($_POST[$index] != "") {
    $file = fopen("./Folders/Orders/" . ***REMOVE $_POST HERE*** $_POST [$file] . ".tym", "w");
    fwrite($file, ***REMOVE $_POST HERE*** $_POST[$index]);
    fclose($file);
  }
}
?>
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
hi,

1) erm you wouldn't want it to count to infinity because it'd never end :p.. if you really wanted an infinte loop, you'd make it so that the term could never be reached like -1 > $j. what are you trying to achieve with a different loop count?

2) do you mean by "PHP script" some PHP code, like a clause afterwards? a php script is just one file. if you find that the files aren't all written when the php script has finished running, you might want to check that it's actually completed and not been cut off by the server. a way to check is to do a print "finished"; at the end of the page and then check it prints this out. if it doesn't, you'll need to use set_time_limit(); (http://uk2.php.net/set_time_limit) to increase the maximum execution time

3) $_POST is used for accessing variables from POST data. if register_globals is off in the configuration, you can just use the variable name but this is insecure. for example, if you have a session variable called $logged_in which stores whether the user is logged in or not, with register_globals off, a user could add ?logged_in=1 to the URL and they've accessed your secure area. so it's basically a safety measure

cheers,

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Hi loz, you have been great - by the way, what part of the country are you from?

You have been a great help, I hope to work with you again - the 500 points are yours with an 'A' - thanks.

I will add another 200 points if you could close off on the bottom few questions:

The reason I asked about the infinite loop is the above script powers a shopping cart, and I would like to allow as many orders as possible (not just 30 for example).  By adding -1 > $j from above, would this 'hang' the script and not allow it to finally end?  Would you just set the number to lets say a large number like 1000 > $j, so it is very unlikely to be reached?

In refernece to my question #2 above, I was triggering too many PHP scripts too close to each other, I find that when there is some overlap when they are executing, at times they do not fully execute.  What do you do in this situation?

And lastly, how would you combine the following 4 PHP scripts.  The reason I'm asking, is that they fire just before the above text script, and it would run alot smoother if the first 4 scripts were combined.  They are simple PHP email scripts that send out emails.  Here they are:

PHP Script #1 that fires:

<?
$ToEmail = "$varEmail1";
##$ToName = "SOME ADDRESS";
$ToSubject = "SOME SUBJECT";
$EmailBody = "WELCOME TO YOUR ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: ".COMPANY." <".$Email.">");
?>

Then, the next script fires and so on (all it is doing is sending a bunch of different variables to a new email address each time):

PHP Script #2 that fires:

<?
$ToEmail = "$varEmail2";
##$ToName = "SOME ADDRESS2";
$ToSubject = "SOME SUBJECT2";
$EmailBody = "WELCOME TO YOUR SECOND ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR SECOND ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: ".COMPANY." <".$Email.">");
?>

PHP Script #3 that fires:

<?
$ToEmail = "$varEmail3";
##$ToName = "SOME ADDRESS3";
$ToSubject = "SOME SUBJECT3";
$EmailBody = "WELCOME TO YOUR THIRD ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR THIRD ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: ".COMPANY." <".$Email.">");
?>

PHP Script #4 that fires:

<?
$ToEmail = "$varEmail4";
##$ToName = "SOME ADDRESS4";
$ToSubject = "SOME SUBJECT4";
$EmailBody = "WELCOME TO YOUR FOURTH ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR FOURTH ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: ".COMPANY." <".$Email.">");
?>

Thanks,
0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
hi,

i'm from the uk, it's 1am here

in answer to your first point, to be honest the 30 > $j is inefficient but it was a simple workaround for this case. this is where having one array contain all your vcartG variables would become very useful - then you could simply do count($_POST["vcartG"]), this gaining the number of vcartG variables that are sent and use this variable as the number of times to loop. with your named variables it's still possible but the function's a bit more drawn out and it will only work as long as the variables are sent in order (i.e. vcartG1, vcartG2 and vcartG3 being sent instead of a possible vcartG1, vcartG2 and vcartG4, if you get me..)

i don't see why you would be triggering too many php scripts at once - this sounds very inefficient, php is very powerful and shouldn't really be overloaded. if you tell me how lots of scripts are fired at once then i can try and point out how to make it more efficient

and for number 3: you only need one script to send off lots of mails. if these variables are coming from form data again, you can use loops like in the code we were using earlier to automate the process, otherwise you can just stick the items once after another. there's a small problem with the mail function - From: ".COMPANY.". either you want company to be a variable and so you need a $ in front or you want it as part of the string and so you need "From: Company <".$email.">"

so:

<?
$ToEmail = "$varEmail1";
##$ToName = "SOME ADDRESS";
$ToSubject = "SOME SUBJECT";
$EmailBody = "WELCOME TO YOUR ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: COMPANY <".$Email.">");
$ToEmail = "$varEmail2";
##$ToName = "SOME ADDRESS2";
$ToSubject = "SOME SUBJECT2";
$EmailBody = "WELCOME TO YOUR SECOND ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR SECOND ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: COMPANY <".$Email.">");
$ToEmail = "$varEmail3";
##$ToName = "SOME ADDRESS3";
$ToSubject = "SOME SUBJECT3";
$EmailBody = "WELCOME TO YOUR THIRD ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR THIRD ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: COMPANY <".$Email.">");
$ToEmail = "$varEmail4";
##$ToName = "SOME ADDRESS4";
$ToSubject = "SOME SUBJECT4";
$EmailBody = "WELCOME TO YOUR FOURTH ORDER - etc. etc";
$EmailFooter="THANK YOU FOR YOUR FOURTH ORDER";
$Message = $EmailBody.$EmailFooter;
mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: COMPANY <".$Email.">");
?>
0
 

Author Comment

by:Donavan_usa
Comment Utility
Wow - you are dedicated!  I'm currently in the U.S.  Do you own your own company in the UK?

In reference to your first comment, how would an array look to get around the 30 > $j above?

In reference to your second comment, the above 4 emails scripts are fired before I reach the text script we have been working on.  I was calling each email script individually, for example:

loadVariablesNum("email1.php", 0, "POST");

Then delay the second one being called:

loadVariablesNum("email2.php", 0, "POST");

And so on down the line to the fourth script.  How would you combine the above 4 emails scripts with a loop that is similiar to our PHP text code?

I was going to guess you were from England, I could tell in your accent

:)


0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
hah i'd like to have my own company but an unrealistic dream at the age of 18! i'm not that dedicated, i just find doing lots of things at once a lot more interesting than marching through the same thing for hours on end without a break

to organise your variables into an array, change their names so they go from vcartG1 to vcartG[0] and then vcartG2 becomes vcartG[1]. the number goes back because this is the standard with arrays. now when you have all your vcartG's under one array, it's a lot easier to manipulate the data. you can do the same with your filenames so they have the same number index as their data counterpart. so we do $countvcart = count($_POST["vcartG"]); and use $countvcart as the number instead of 30. it's more efficient than using the number 30 in fact. if you want to do this, the rest of your code where the vcart variables are used will need to change a bit, but its not too much effort

<?
$countvcart = count($vcartG);
for($i = 0; $countvcart > $i; $i++){
  $j = $i + 1;
  if($_POST["vcartG"][$i] != "") {
    $file = fopen("./Folder/Orders/" . $_POST["name"][$i] . ".txt", "w");
    fwrite($file,$_POST["vcartG"][$i]);
    fclose($file);
  }
}
?>

where's this data for the emails coming from?

basically you put all the code into one script, so these four emails go above the text script.

here's an example but you'll need to modify it a bit as i don't know the data source:

<?
for($x = 0; $x > 4; $x++) {
  $ToEmail = $to[$x];
  $ToName = $name[$x];
  $ToSubject = $subject[$x];
  $EmailBody = $body[$x];
  $EmailFooter=$footer[$x];
  $Message = $EmailBody.$EmailFooter;
  mail($ToName." <".$ToEmail.">",$ToSubject, $Message, "From: COMPANY <".$Email.">");
}
?>

i had a feeling you were from the us, donavan_usa, but i can't imagine where from

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
Hi loz, sorry for the delay, I was running some tests.  In the future, is there anyway to contact you directly with PHP related questions?  In light of a company, You have great momentum for 18 - keep it going strong!

0
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
if you need to, you can email me at loz at keepshouting dot com but to be honest you're likely to get a quicker and probably better answer asking on here, anyway tell me how you get on with the code, time to catch some sleep

loz
0
 

Author Comment

by:Donavan_usa
Comment Utility
thanks loz - you have been a great help and thank you for hanging in there, I know its early in the morning in the UK (I went to school there).

Here's the 700 and the 'A'

Thanks again,
Donavan
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now