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
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Sun May 08, 2005 12:05 am
I want to display errors by using a function I made (located on function.php):
Code: Select all
function error($errors){
if($errors){
return "<table width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr bgcolor=\"#990000\">
<td><div align=\"center\"><font color=\"#CCCCCC\"><strong>Please Fix the Following
Errors</strong></font></div></td>
</tr>
<tr bgcolor=\"#990000\">
<td bgcolor=\"#333333\"><ul>"
foreach($errors[] as $fix => $value){
return implode("<li></li>",$fix);
}
return "</ul></td></tr></table>";
}else{
$error = 0;
return $error;
}
}
Here is the form error checking:
Code: Select all
include_once('function.php');
$errors = array();
if($atopcommander == ""){
$atopcommander = 'none';
}
$addcheck = mysql_query("SELECT * FROM `farms` WHERE username = '$ausername'") or die(mysql_error());
if(mysql_num_rows($addcheck) !== 0){
$errors[] = "Farm Already In Database";
}
if($ausername = "" && $daefenceaction == "" && $aarmysize == "" && $astatid == ""){
$errors[] = "Please Fill In All Fields";
}
if((!is_numeric($adefenceaction)) && (!is_numeric($aarmysize))){
$errors[] = "Please Recheck Defence Action and Armysize.";
}
if((strlen($astatid) <= 6) && (!is_numeric($astatid))){
$errors[] = "Please Check Stat ID.";
}
error($errors);
if($error == 0){
$timeadd = time();
mysql_query("INSERT INTO farms (id, username, topcommander, defenceaction, armysize, statid, comments, author, time) VALUES(NULL, '$ausername', '$atopcommander', '$adefenceaction', '$aarmysize', '$astatid', '$acomments', '$author', '$timeadd')") or die(mysql_error());
mysql_query("UPDATE users SET farmcount = farmcount + 1 WHERE user = '$username' LIMIT 1") or die(mysql_error());
print "Farm Was Added To Database";
}
I'm getting errors about the foreach. I need a push in the right direction, many thanks.
hongco
Forum Contributor
Posts: 186 Joined: Sun Feb 20, 2005 2:49 pm
Post
by hongco » Sun May 08, 2005 1:09 am
print_r ($errors);
see what you got.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun May 08, 2005 5:34 am
Code: Select all
foreach($errors[] as $fix => $value){
//change to
foreach($errors as $fix => $value){
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Sun May 08, 2005 10:16 am
Code: Select all
$ausername = $_POSTї'username'];
$atopcommander = $_POSTї'topcommander'];
$adefenceaction = trim(str_replace("e;,"e;,"e;"e;,$_POSTї'defenceaction']));
$aarmysize = trim(str_replace("e;,"e;,"e;"e;,$_POSTї'armysize']));
$astatid = trim($_POSTї'statid']);
$acomments = $_POSTї'comments'];
$author = $_POSTї'author'];
include_once('function.php');
$errors = array();
if($atopcommander == "e;"e;){
$atopcommander = 'none';
}
$addcheck = mysql_query("e;SELECT * FROM `farms` WHERE username = '$ausername'"e;) or die(mysql_error());
if(mysql_num_rows($addcheck) !== 0){
$errorsї] = "e;Farm Already In Database"e;;
}
if($ausername = "e;"e; && $daefenceaction == "e;"e; && $aarmysize == "e;"e; && $astatid == "e;"e;){
$errorsї] = "e;Please Fill In All Fields"e;;
}
if((!is_numeric($adefenceaction)) && (!is_numeric($aarmysize))){
$errorsї] = "e;Please Recheck Defence Action and Armysize."e;;
}
if((strlen($astatid) <= 6) && (!is_numeric($astatid))){
$errorsї] = "e;Please Check Stat ID."e;;
}
if($errors){
print "e;<table width=\"e;250\"e; border=\"e;0\"e; cellspacing=\"e;0\"e; cellpadding=\"e;0\"e;>
<tr bgcolor=\"e;#990000\"e;>
<td><div align=\"e;center\"e;><font color=\"e;#CCCCCC\"e;><strong>Please Fix the Following
Errors</strong></font></div></td>
</tr>
<tr bgcolor=\"e;#990000\"e;>
<td bgcolor=\"e;#333333\"e;><ul><li>"e;;
foreach($errors as $fix => $value){
$fixerrors = implode("e;</li><li>"e;,$value);
print $fixerrors;
}
print "e;</ul></td></tr></table>"e;;
print_r($errors);
}else{
$timeadd = time();
//mysql_query("e;INSERT INTO farms (id, username, topcommander, defenceaction, armysize, statid, comments, author, time) VALUES(NULL, '$ausername', '$atopcommander', '$adefenceaction', '$aarmysize', '$astatid', '$acomments', '$author', '$timeadd')"e;) or die(mysql_error());
//mysql_query("e;UPDATE users SET farmcount = farmcount + 1 WHERE user = '$username' LIMIT 1"e;) or die(mysql_error());
print "e;Farm Was Added To Database"e;;
}
Ok, I'm getting the following error:
Warning: implode(): Bad arguments. in /home/nokiddin/public_html/ffinder/add2.php on line 137
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun May 08, 2005 10:22 am
You need to pass an array to implode. What you are doing is passing an array element.
Change
Code: Select all
foreach($errors as $fix => $value){
$fixerrors = implode("</li><li>",$value);
print $fixerrors;
}
to just
Code: Select all
print implode('</li><li>',$errors);
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Sun May 08, 2005 10:54 am
Ahhh, that would explain it.
So now, When an error returns, it returns fine. But when the user enters/fixes the data, it doesnt check for error any more. I tried unset($errors). Any ideas?