Jump to content


Photo

handling multiple error/successes


  • Please log in to reply
4 replies to this topic

#1 senatorslab

senatorslab
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 March 2006 - 12:47 AM

so i searched everywhere to try and get a solution to this, but searching for anything involving the word "errors" brings back all sorts of stuff (as expected... :() not relevant to my situation, and i apologize if this type of issue has already been covered. ok, i'm writing a page for a "content management system" that is used to update a "news scroller". the news scroller is done in flash as dynamic text that pulls from a flat text file. so with the CMS i am just writing to 5 text files. really simple. it's just 5 text input fields that are populated by the text files, and you can edit the input and then hit the submit to update all or some of the txt files. it all works fine, but i am trying to get some "validation" after the submission of updating the text files. simply put, i just want to echo certain variables based upon whether the text file was written correctly or not. i hope that is explained well. also, i'm sure there is a simpler way that i am trying to go about it, (i.e. using arrays to hold errors/successes perhaps??, maybe a case situation??, maybe something i don't even know??? lol) but if you would rather just help with the current method of multiple variables, that's fine. so with out further delay i'll get to the code.

<?php
function updatenews(){
$error1 = "";
$error2 = "";
$error3 = "";
$error4 = "";
$error5 = "";
$success1 = "";
$success2 = "";
$success3 = "";
$success4 = "";
$success5 = "";
$errors = "";
$successes = "";

$fn = "../news1.txt";
$fp = fopen($fn, "w");
if (!$fp){
$error1 = "Unable to open file: <b>news1.txt</b> for writing.<br>";
return $error1;
}else {
$news1 = stripslashes($_POST['news1']);
fwrite($fp, "$news1");
fclose($fp);
$success1 = "File: <b>news1.txt</b> written successfully.<br>";
return $success1;
}

$fn = "../news2.txt";
$fp = fopen($fn, "w");
if (!$fp){
$error2 = "Unable to open file: <b>news2.txt</b> for writing.<br>";
return $error2;
}else {
$news2 = stripslashes($_POST['news2']);
fwrite($fp, "$news2");
fclose($fp);
$success2 = "File: <b>news2.txt</b> written successfully.<br>";
return $success2;
}

//.....for simplicity (and a shorter post) i won't list the rest of the txt
//files...just assume that i continue the above methodology for news 3, 4, and 5.

}

$errors = "$error1 $error2 $error3 $error4 $error5";

if($_POST["submit"]=="Update News"){
$errors = updatenews($_POST);
if ($errors == "")
$successes = "$success1 $success2 $success3 $success4 $success5";
}
?>

and then in the html i am simply echoing the variables in a div like this:

<div id="edit_news_error">
<?php echo $errors; ?>
<?php echo $successes; ?>
</div>

what it currently does upon success is show the first success only ($success1) in my div and current layout is fine. and if i remove all of the news*.txt files to get the errors, it only displays the first error ($error1) and it doesn't display in my div at all correctly. the div layout issue is proly nothing, i'll most likely get it figured out when i get the error handling working right. hope this all makes sense to anyone that is willing to help. i will greatly appreciate it. i hope i have posted i the correct location and have abided by forum rules. if this needs to be moved to another location, i understand and apologize. thanks in advance.

-senatorslab

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 March 2006 - 03:02 AM

A much easier way to do this is to use arrays for the messages. Also you need to go through all your files before returning from the function. That's why you're only getting the first succes message
<?php
function updatenews(){
    $smsgs = array();
    $emsgs = array();
    for ($i=1;$<6;$i++) {
         $fn = '../news' . $i . '.txt';
          $fp = fopen($fn, "w");
          if (!$fp) $emsgs[] = 'Unable to open file: <b>news' . $i . '.txt</b> for writing.';
          else {
             $news = stripslashes($_POST['news' . $i]);
             fwrite($fp, $news);
             fclose($fp);
             $smsgs[] = 'File: <b>news' . $i . '.txt</b> written successfully.';
          }
    }
    return(array(implode("<br>\n",$smsgs),implode("<br>\n",$emsgs));
}

f($_POST["submit"]=="Update News"){
      list($successes,$errors) = updatenews($_POST);
}
?>
This should do what you want. Your HTML code is fine.

Notice that I put the code to read the files into a loop and only return when all the file are done.

Please note that I haven't checked this for syntax errors.

Ken

#3 senatorslab

senatorslab
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 March 2006 - 03:36 AM

wow! that is awesome. i totally understand what is going on, but you are correct in saying there may be syntax errors. i keep getting Parse error: syntax error, unexpected ';' on the return(array) line. :( i can't see what is wrong immediately and i understand if you don't want to bother helping find a silly syntax error for me, so i'll get it eventually...i hope..lol. thanks SO much! you have enlighten me greatly to arrays and the implode function. thanks a lot for helping me out and for being so prompt about it as well. :)

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 March 2006 - 03:54 AM

I just forgot one closing parenthisis on the line.
<?php     return(array(implode("<br>\n",$smsgs),implode("<br>\n",$emsgs))); ?>

I don't mind helping. Everyone was new and stuggling once. I've been scripting PHP since 1999 and I'm still learning new things.

Ken

#5 senatorslab

senatorslab
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 March 2006 - 04:00 AM

holy crap. i've been staring at this forever trying to step through and see what was missing. i can't believe i didn't see that. well, at least i just have shitty eyes...lol. and not a complete fool. thanks again for all your help. i'll most definitely have to keep coming to this site to learn more. many regards.

-senatorslab




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users