...

View Full Version : Is there a shorter way to do this coding?



wkheathjr
03-07-2008, 05:07 PM
Just wondering if there is a shorter way of checking code than this way??



if(strlen($eventtitle)<1)

{

print "<span class=errortext><b>ERROR:</b>You did not type in Event Title.</span>";

}

else if(strlen($eventdesc)<1)

{

print "<span class=errortext><b>ERROR:</b>You did not type in Event Description.</span>";

}

else if(strlen($eventstartmonth)<1)

{

print "<span class=errortext><b>ERROR:</b>You did not select Starting Month.</span>";

}

else if(strlen($eventstartday)<1)

{

else

awatson
03-07-2008, 06:22 PM
Create an array, indexed by the field name you want to check, with the values being the error messages. Loop through the array check if each field is empty, then if so printing out the error message for that field.

I'm sure there's other ways as well.

Leeoniya
03-07-2008, 09:13 PM
not shorter, but better.: this way will give you all errors at once and it's easy to add more error types.

<?php
$errs = array(
"evt_title"=>"You did not type in Event Title.",
"evt_desc"=>"You did not type in Event Description.",
"start_month"=>"You did not select Starting Month.",
"start_day"=>"You did not select Starting Day."
);

$outerrs = array();

if(strlen($eventtitle)<1)
array_push($outerrs, $errs["evt_title"]);
if(strlen($eventdesc)<1)
array_push($outerrs, $errs["evt_desc"]);
if(strlen($eventstartmonth)<1)
array_push($outerrs, $errs["start_month"]);
if(strlen($eventstartday)<1)
array_push($outerrs, $errs["start_day"]);

$output = '<span class="errortext">';
foreach($outerrs as $v) {
$output .= "<b>ERROR:</b>$v<br />";
}
$output .= '</span>';
echo $output;
?>
Leon

wkheathjr
03-08-2008, 03:33 PM
Ahh so basically we wouldn't have to worry about else statement if we use this array function on top of the page before getting right to the purpose of adding event info? The way I wrote this code was to check if all field was filled before inserting the field into the database after 'else'.

And the main reason I asked is because I realized I made it too complex to check if the post has pic or not, you know? Like this:


if (isset($_POST['submit'])) to check for submit and also
if(isset($_FILES["fupload"]) if submit has pic uploaded.

Structure was like this:

If submitted with pics then check for error else INSERT result into database, else If submitted without pics then check for error else INSERT result into database.

Now I realized this is poorly structured...

Leeoniya
03-08-2008, 11:55 PM
if you want to prevent database insertion if ANY errors are encountered, just do if(count($outerrs) > 0). this will tell you if there were any errors at all.

rather than reinventing the wheel (or spending many hours writing regular expressions), i would recommend using a backend RegExp validation library such as this: http://www.benjaminkeen.com/software/php_validation/

using regular expressions has the benefit of automatically preventing SQL injection attacks in many cases. and also does a lot more than check the length of a string, allowing you to give errors on the formatting of each field.

since i mostly write webapps, i prefer to do this live in javascript using an onBlur or onChange event handlers for each field to reduce server interaction until it's time to actually talk to the DB. a good library for doing this is YAV: http://yav.sourceforge.net/

Leon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum