Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Is there a shorter way to do this coding?

    Just wondering if there is a shorter way of checking code than this way??

    PHP Code:
    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 

  • #2
    New Coder
    Join Date
    Jan 2008
    Posts
    63
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.
    Web hosting by coders, for coders: php hosting - Magento eCommerce hosting - perl hosting - check us out!

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Smile

    not shorter, but better.: this way will give you all errors at once and it's easy to add more error types.
    PHP Code:
    <?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

  • Users who have thanked Leeoniya for this post:

    wkheathjr (03-08-2008)

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    if (isset($_POST['submit'])) 
    to check for submit and also
    PHP Code:
    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...

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •