SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Foreach loop displays extra empty row

    Would like to prevent the extra blank row from appearing in this loop.


    PHP Code:
    $swarray=explode(";"$swarray);
        foreach (
    $swarray as $rrow) {
        
    $row=explode(":"$rrow);
    echo 
    "
        var divId = 'imageColor_"
    .$row[0]."';
        if (divId == idToHighlight && MouseOver==1) {
            if(SelectedAsDefault){
                changeImgBorder(divId, \"1px solid #bdbdbd\","
    .$row[0].");
                            
            }else{
                if(divId != SelectedDefaultSwatchImgId)
                    changeImgBorder(divId, \"1px solid #bdbdbd\","
    .$row[0].");
                    changeDefaultColor('
    $row[1]');
                }    
                
        } else {
            if (SelectedDefaultSwatchImgId != undefined && SelectedDefaultSwatchImgId != '')
            {
                if (!MouseOver)
                {
                    changeDefaultColor(SelectedDefaultSwatchColorName);
                }
                if(divId != SelectedDefaultSwatchImgId)
                {
                    //original border
                    changeImgBorder(divId, \"0px solid\","
    .$row[0].");
                    
                }
            }
        } "
    ;



  2. #2
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the value of $swarray before you explode it?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1:White Vinyl:wv:1;2:Black Vinyl:bv:0;3:Silver Vinyl:sv:0;4:Brown Ultrasuede:bru:0;5:Orange Ultrasuedeu:0;6:Blue Ultrasuede:blu:0;7:Green Ultrasuede:gu:0;

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $swarray=explode(";"$swarray, -1); 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IF you are going to MAYBE have a semicolon at the beginning too, you may want to consider running it through trim($swarray, ';'); first
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  6. #6
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    PHP Code:
    $swarray=explode(";"$swarray, -1); 
    I never knew about the negative third parameter.. I always pre-processed the string

    Learn something new every day!
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that worked

  8. #8
    SitePoint Member
    Join Date
    Feb 2009
    Location
    San Diego, CA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    foreach($swarray as $rrow){
        if(empty($rrow)) continue;
        (rest of foreach loop)
    }
    Last edited by Sabia Design; Feb 5, 2009 at 22:33.
    Sabia Design - Modern Web Development
    PHP MySQL Cities - Need a database of cities for PHP / MySQL?
    San Diego, CA / San Francisco, CA / BS CS / Stanford Engineering

  9. #9
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sabia Design View Post
    Code:
    foreach($swarray as $rrow){
        if(empty($rrow)) continue;
        (rest of foreach loop)
    }
    Not the most efficient - you're running the check every single loop. When you know its just the last element you don't want, this is not the route to take
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  10. #10
    SitePoint Member
    Join Date
    Feb 2009
    Location
    San Diego, CA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    Not the most efficient - you're running the check every single loop. When you know its just the last element you don't want, this is not the route to take
    If you know that your last element is empty then you can use "-1" to remove that last element.

    However, that seems only useful when "'exploding" a string into an array. If you have an array that you want to skip all empty elements, then "continue" is more efficient.
    Sabia Design - Modern Web Development
    PHP MySQL Cities - Need a database of cities for PHP / MySQL?
    San Diego, CA / San Francisco, CA / BS CS / Stanford Engineering


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
  •