Loop until it finds a new name for text files *Please help*

Hello fellow php experts, I have some question to ask you:

$category = $_POST['category'];
$counter_var = 0;
$filea = "$category/topic$counter_var.txt";

while (file_exists($filea)) {
$counter_var ++;//here is the loop****************************
} else {

$topic = $_POST[‘topic’];
$category = $_POST[‘category’];
$message = $_POST[‘message’];
$datetime = date(“m/d/y h:i a”);

$username = $_SESSION[‘username’];
$bData = @file(“log_files/$username.txt”); // collect all data into an array
foreach($bData as $v){list($name,$email,$gender,$birthm,$birthd,$birthy,$location,$modfiytime) = explode("#",$v);}

//word censors
$topic = stripslashes($topic);
$message = stripslashes($message);
$message = ereg_replace(“n”, “
n”, $message);

if ($message != “” && $category !="" && $topic !="") {$file = fopen($filea,“a”);
$write = fwrite($file,"$topic#$datetime#$name#$message");fclose($file);}
{echo"Required field(s) left blank!

if($write == TRUE)
{echo"Message successfully pasted!";}
{echo"Sorry but there was an error posting your message!";}

<? }} ?>
Post New Thread:
Category: HomeworkArts/MusicalSportsTechnologiesSocializingCurrent EventsOthersWeb Help
Post Content:
[/php] what I want to do is to loop the while until it finds a new name. So the outcome of this form will create topic0.txt, topic1.txt,topic2.txt,topic3.txt....and etc, however, it doesn't seem to work, can anyone help me and have a look at this? THanks for your time. Ted

Well after switching the phpbb CODE tags to the modified phpbb PHP tags ( see http://phphelp.com/forums/viewtopic.php?t=6960 ) you can see through the syntax highlighting that many of your lines enclosed in the <? tags are not being echoed or printed. Thus this would cause an error (probably a parse error on line xxxx ).

That brings me to my next point… What errors did you get? It’s tough when tell us “It’s broke? Can you help me fix it?” without knowing how you know it’s broke. (i.e. what’s it doing or not doing… What messages, etc).

To that end, there may be more than what I stated above, but that was the first thing I saw, and stopped looking.

Just off hand, the while loop doesn’t look correct. It looks like you are setting the filename outside of the loop, which means the code is always checking to see if the same file exists.

The current code shows:

$category = $_POST[‘category’];
$counter_var = 0;
$filea = “$category/topic$counter_var.txt”;
while (file_exists($filea)) {
$counter_var ++;//here is the loop****************************

I’m pretty sure you meant something like this:

$category = $_POST[‘category’];
$counter_var = 0;
do {
$filea = “$category/topic$counter_var.txt”;
} while (file_exists($filea));

Regarding the syntax highlighting, it looks like you forgot to add a closing php tag after:

[php]{echo"Sorry but there was an error posting your message!";}[/php]


Sponsor our Newsletter | Privacy Policy | Terms of Service