SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Location
    VA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help using "empty"

    I'm trying to determine if 20 text boxes have entries.
    I can check a single text box with !empty.
    But I want to check for multiple empty text boxes with
    the same statement.

    I'm stumped.

    Cruse

  2. #2
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use something like...

    if ( !empty($..) AND !empty($..) AND ...) ...

    or you can use something like this...

    if ( !empty($.. OR $..) ) ...


    Hope my pesudocode makes sense.

  3. #3
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Constructing an if statment with 20 conditionals can get quite unmanagable.

    If your textboxes are named in series, you could try something like this:

    PHP Code:
    $textboxBaseName 'box';
    for (
    $i 1$i <= 3$i++) {
       
    $textboxName $textboxBaseName.$i
       
    if (empty($$textboxName)) {
          echo 
    "$textboxName is empty.<br>\n";
       }

    That would check for values in $box1, $box2, and $box3.

    If your textboxes are not names in sequence, then you can still shorten the code by putting a list of all the textboxes in an array and then using a foreach loop.

    That would work something like this:

    PHP Code:
    $textboxList = array();
    $textboxList[] = 'first_name';
    $textboxList[] = 'middle_initial';
    $textboxList[] = 'last_name';

    foreach (
    $textboxList as $textboxName) {
       if (empty($
    $textboxName)) {
          echo 
    "$textboxName is empty.<br>\n";
       }

    Let me know if this helps.

  4. #4
    SitePoint Member
    Join Date
    Mar 2002
    Location
    VA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I'll try both suggestions.

    Cruse

  5. #5
    SitePoint Member
    Join Date
    Mar 2002
    Location
    VA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify a little.

    I tried the foreach, but it printed the result
    3 times, as I was checking 3 variable.
    ( I was using 3 for testing)

    Actually I have 5 possible selections in 5 categories.
    In any particular category the 5 text boxes are duplicated
    so the customer can order up to 5 different items. The
    other two vars are size and qty.
    Customer manually enters item num. and then selects size
    and qty from select boxes.
    Item num. vars are design1, design2,..., design5.

    I'm trying to print out an order summary for each
    entry. For now, I'm using a whole lot of "Ifs".

    Cruse

  6. #6
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wasn't able to understand your explanation of why the code I posted isn't working for you.

    You said it printed three results for three testing variables, what exactly did it output? and should have it output?

    Also, if you post your code with all the if statments, I can write a drop in replacement for it so I won't have to guess on how your going to use it.

  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Location
    VA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cyngon,

    Here is my quick temp solution. Temporary because I'm
    trying to get the output working (even though the code
    is not efficient) so the order can be submitted and
    emailed to sales. I can continue to refine it in the next
    few days.
    So this is what I have:

    if (!empty($ssdesnum1)) {
    echo "<b>Design Number: </b>$ssdesnum1<br>\n";
    echo "<b>Size: </b>Adult/ $sssize1<br>\n";
    echo "<b>Quantity: </b>$ssqty1<br>\n";
    }
    if (!empty($ssdesnum2)) {
    echo "<b>Design Number: </b>$ssdesnum2<br>\n";
    echo "<b>Size: </b>Adult/ $sssize2<br>\n";
    echo "<b>Quantity: </b>$ssqty2<br>\n";
    }

    if (!empty($ssdesnum3)) {
    echo "<b>Design Number: </b>$ssdesnum3<br>\n";
    echo "<b>Size: </b>Adult/ $sssize3<br>\n";
    echo "<b>Quantity: </b>$ssqty3<br>\n";
    }

    I tried to tweak your code, and that's probably what
    messed it up. The result I wanted was to output
    each desnum if it was entered in the text box. If I
    made one entry, it printed 3 times.
    In your code , I changed the empty to !empty and
    replaced the echo with the code above.
    In other words, If text box not empty, print the
    design, size and qty. If it is empty, check the next box.

    It was going thru the 3 "ifs" 3 times. That was the prob.
    Sorry to sound confusing.
    Any help will be appreciated.

    Cruse

  8. #8
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a replacement for what you posted, try this:

    PHP Code:
    $textboxBaseName 'ssdesnum';
    for (
    $i 1$i <= 3$i++) {
       
    $textboxName $textboxBaseName.$i
       
    if (!empty($$textboxName)) {
          echo 
    "<b>Design Number: </b>".${"ssdesnum$i"}."<br>\n"
          echo 
    "<b>Size: </b>Adult/ ".${"sssize$i"}."<br>\n"
          echo 
    "<b>Quantity: </b>".${"ssqty$i"}."<br>\n"
       }

    It should do the exact same thing.


Bookmarks

Posting Permissions

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