[SOLVED]What's wrong?

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
shadowx360
Forum Newbie
Posts: 6
Joined: Sun Oct 10, 2010 3:53 pm

[SOLVED]What's wrong?

Post by shadowx360 »

Can anyone tell me what's wrong with my code?

Code: Select all

<?php
$IDs = preg_split("/[\s]*[,][\s]*/", $_REQUEST['IDs']);
$myFile = "IDs.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
for ( $counter = 0; $counter < count($IDs); ++$counter) 
{
    fwrite($fh, $IDs[counter]."\n");
    echo $IDs[counter]."\n";
}
fclose($fh);
?>
What I'm trying to do is make a PHP file that will take a parameter like : http://www.mysite.com/log.php?IDs=1,2,3 and then add to the ID.txt "1\n2\n3\n" (the \n meaning new line).

Solved
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: [SOLVED]What's wrong?

Post by John Cartwright »

What a useless preg_split pattern :D Although it's impossible to tell whats wrong with your script if you don't tell us the expected and actual behavior. I.e., debugging information.

A simpler version:

Code: Select all

$myFile = "IDs.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, str_replace(',', PHP_EOL, $_REQUEST['IDs']) . PHP_EOL);
fclose($fh);
Post Reply