Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-17-2004, 05:13 AM file write loop.
Novice Talker

Posts: 11
Trades: 0
Hi all
I have a need, a need for... ease?
ok ok, so i have this lot of php slapped together...
PHP Code:
<?php
for($i=0;$i<71;$i++) { 
$warning[$i] = $_POST[''.$i.''];
}

$rContents "$filename"
$dir "Users/VIC";

if (!
$handle fopen("$dir/$warning[$i]"'aw+')) { 
         echo 
"<br>Cannot open file ($dir/$warning[$i])";              
         exit; 
   } 

   
// Write content to file. 
   
if (fwrite($handle,"\r\n $rContents") === FALSE) { 
       echo 
"<br>Cannot write to file ($dir/$warning[$i])"
       exit; 
   } 
   echo 
"<br>Success, wrote ($rContents) to file ($dir/$warning[$i])"

       
chmod("$dir/$warning[$i]"0777);

       
fclose($handle);?>
($filename is a value set elsewhere in the script...)

now... in this particular instance there is 71 differing values of $_POST[' '] set in the html form page..- which get set nicely in the first 2 lines...

the rest of the code is to write a file, named on the value set in the <input value=" "> part, now there is 71 differing values in this particular form, hence the getting the $_POST[' '] variable to loop 71 times. so instead of copy/modifying/pasting the file write code 71 times to write each file (if the value is selected in the form...) WHAT can i do to make the one lot of file write code and loop it 71 times?

Simon
|-SiM-| is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-17-2004, 08:20 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
Didn't understand that in the slightest Try this though;

PHP Code:
$filename "my_stuff.txt";

$fall "";

foreach(
$_POST as $key=>$value) {
  
$fall .= "$key=$value\n";
}

$fp fopen($filename,"w") || die("Couldn't open the file");
fwrite($fp,$fall);
fclose($fp); 
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE

Last edited by webwoRRks; 08-17-2004 at 08:23 AM..
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-17-2004, 08:23 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
Ohhhhhh, I see! (I think.) This is more what you meant;

PHP Code:
$contents "Stuff n text n stuff";

foreach(
$_POST as $value) {
   
$fp fopen($value.".txt","w") || echo "Error writing file [$value.txt]\n";
   if(
$fp) {
       
fwrite($fp$contents);
       
fclose($fp);
   }

__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-17-2004, 09:31 AM
Novice Talker

Posts: 11
Trades: 0
Sorry for not explaining myself correctly, the 2nd lot of code you gave me works, except for a few things...
Quote:
Success, Wrote iuyu_ioui.txt to iuyu.txt
Success, Wrote iuyu_ioui.txt to ioui.txt
Success, Wrote iuyu_ioui.txt to uiyi.txt
Success, Wrote iuyu_ioui.txt to yutu.txt
Success, Wrote iuyu_ioui.txt to trry.txt
Success, Wrote iuyu_ioui.txt to Coastal_Wind_Warning_West_of_Cape_Otway.txt
Success, Wrote iuyu_ioui.txt to Coastal_Wind_Warning_Cape_Otway_to_Wilsons_Prom.tx t
Success, Wrote iuyu_ioui.txt to Land_Wind_Warning.txt
Success, Wrote iuyu_ioui.txt to Flood_Advice_-_West_Gippsland.txt
Success, Wrote iuyu_ioui.txt to Flood_Warning_-_Fifteen_Mile_Creek.txt
Success, Wrote iuyu_ioui.txt to ADD USER .txt
Now, it writes the files selected by the user (in the html form), but also writes every other $_POST that is used on the form, such as username and the "submit" button...

Now to further my above post, the file write code works fine, i just don't want to have to have 71 slightly different versions for the 71 <input>/$_POST values that could possibly be selected in the HTML form, so how can get it to loop 71 times - and without halting when it gets to a $_POST variable that hasn't been selected... (i sorta got it to loop, but would stop on the first un selected value...)
|-SiM-| is offline
Reply With Quote
View Public Profile
 
Old 08-17-2004, 02:29 PM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
umm... still not sure what you meant. If you want it to skip unassigned POST variables, just do

if(isset($_POST['item_id'])) { /* stuff */ }
else { /* Dont do stuff */ }

To get it to loop only through certain field with a number and word ID, suppose your fields were;

input name="item_1"
input name="item_2"
input name="item_3"

Your loop code would be

for($i=1;$i<=3;$i++) {
if(isset($_POST['item_'.$i])) { /*Do stuff.... */ }
}
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-17-2004, 10:18 PM
Novice Talker

Posts: 11
Trades: 0
ok, ill try to explain again...

HTML FORM.....
(71 different checkbox's, users select whichever ones they want)
each checkbox has a different <input name=""> in this case it starts at 0 and goes to 71 and different Values - which becomes the filename of the txt file we are trying to write on the php side.
.
.
.
PHP SCRIPT.
Gets all $_POST[values] set in the html form - returns an error when a value is unslected, but that gets surpressed by error_reporting(0).

the file writing code i supplied works fine only for the first checkbox, if selected.
PHP Code:
 if (!$handle0 fopen("$dir/$warning0"'aw+')) { 
         echo 
"<br>Cannot open file ($dir/$warning0)";              
         exit; 
   } 

 
// Write content to file. 
   
if (fwrite($handle0,"\r\n $rContents") === FALSE) { 
       echo 
"<br>Cannot write to file ($dir/$warning0)"
       exit; 
   } 
        
   echo 
"<br>Success, wrote ($rContents) to file ($dir/$warning0)"
       
chmod("$dir/$warning0"0777);
       
fclose($handle0); 
now, it works fine beacuse i have got $warning0 ($warning0 is set by..
PHP Code:
for($i=0;$i<71;$i++) { 
$warning[$i] = $_POST[''.$i.''];

)
so $warning0 = $_POST['0'] through to $warning71 = $_POST['71']

For me to use my file write code properly, i need to change 2 things --> $handle0 and $warning0 - this needs to be changed thorugh to $handle71 and $warning71, and having 71 slightly different versions of the same code makes for a very bulky script.

Hence why i need it to loop 71 times, adding 1 to the value of $handle and $warning (which i can't get it to do) and not halt on an unselected value, but how?


The code supplied in the previous post kinda works, for some reason it writes the same file 4 times over, and also writes unselected values - which is not what i want.

Hopefully u understand this time

Last edited by |-SiM-|; 08-17-2004 at 10:28 PM.. Reason: speeling misstaks hehe
|-SiM-| is offline
Reply With Quote
View Public Profile
 
Old 08-18-2004, 11:26 AM
Novice Talker

Posts: 11
Trades: 0
anyone???
|-SiM-| is offline
Reply With Quote
View Public Profile
 
Old 08-18-2004, 11:51 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
ARGH! maybe I'm just slow.... but I think I get what you're saying... If they check a box, you want to write to the file who's name is the value of that checkbox. If thats so, you want something like this;

PHP Code:
<?php
for($i=0;<=71;$i++) {
  if(isset(
$_POST[$i])) {
     
$fp fopen($_POST[$i].".txt","w");
     
fwrite($fp,$rContents);
     
fclose($fp);
  }
}
?>
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-23-2004, 09:17 AM
Novice Talker

Posts: 11
Trades: 0
thanks, worked a treat
|-SiM-| is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to file write loop.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61332 seconds with 12 queries