SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: foreach issue

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach issue

    Hey,

    With the folowing code, I get the last if statement back as the result no matter what $types matches.. I want it to match the jpg file to each word in the array..

    PHP Code:
        $type explode(",",$type);
        foreach(
    $type as $types) {  
        
        if(
    $types 'Wordmark') {    
            
    $l 'wordmark.jpg';
        }
        if(
    $types 'Letterform') {
            
    $l 'letterform.jpg';
        }
        if(
    $types 'Pictorial') {
            
    $l 'pictoral.jpg';
        }
        if(
    $types 'Emblem') {
            
    $l 'emblem.jpg';
        }
        if(
    $types 'Abstract') {
            
    $l 'abstract.jpg';
        }

    echo 
    "$l<br />$types"

    Thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    =
    ==


  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh well that was embarrassing.. I should have taken a break..

    Thanks

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You seem to have your variables backwards as well... $type for multiple types, and $types for a single type hardly makes sense.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,824
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Hi @_matrix_,

    It just so happens that my latest project heavily involves matrixes/matrices


    Maybe try this:

    PHP Code:

        $type 
    'Wordmark, Letterform, Pictorial, Emblem, Abstract';
        
    $type explode(','$type); 
       
        foreach(
    $types as $id => $type):
          echo 
    '<br />'$id': '$type' ===> 'strtolower($type) . '.jpg';
        endforeach; 
    Output:
    0: Wordmark ===> wordmark.jpg
    1: Letterform ===> letterform.jpg
    2: Pictorial ===> pictorial.jpg
    3: Emblem ===> emblem.jpg
    4: Abstract ===> abstract.jpg
    Last edited by John_Betong; Jun 20, 2012 at 02:46. Reason: spelling - not my fortay

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this instead:
    PHP Code:
    $incoming "Letterform, Wordmark,Someoldcrap";
    $allowed = array("Letterform","Wordmark""Pictorial""Emblem""Abstract");

    $types explode(",",$incoming); 
    $results ""// declare the empty string

    // as was pointed out get used to writing "foreach plural as singular" and you wont get mixed up
     
    foreach($types as $type) {   
      if( 
    in_array(trim($type), $allowed)){
        
    $results .= strtolower($type) . '.jpg for ' $type PHP_EOL;  // concat the string
      
    }
     }
     echo 
    $results
    Your original code was flawed because you were forever overwriting any previous match.

    This code concatenates a string, but could just as easily be returning an array.

    PHP Code:
        if($types 'Pictorial') { 
            
    $l 'pictoral.jpg'
        } 
    When you see yourself writing things over and again Pictorial, pictorial alarm bells should go off in your head and you should ask "is there a better way?" Perhaps invest some real time in grokking how arrays work.

    EDIT Gah, too slow, beaten by JB!


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
  •