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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    simplifying a whole stack of codes in for loop

    Is it possible in simplifying this stack of codes? They all have things in common but have differences as well:
    PHP Code:
    $i=0;
    if (!empty(
    $keyword1))
        {
            echo 
    "<span class='' id='' style='font-weight: bold; color: red;'> ".$keyword1."</span>\n";
            
    $i++;
        }
    if (!empty(
    $keyword2))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword2."</span>\n";
        }
    if (!empty(
    $keyword3))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword3."</span>\n";
                
    $i++;
        }
    if (!empty(
    $keyword4))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: green;'> ".$keyword4."</span>\n";
            
    $i++;
        }
    if (!empty(
    $keyword5))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: orange;'> ".$keyword5."</span>\n";
            
    $i++;
        }
    if (!empty(
    $keyword6))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: purple;'> ".$keyword6."</span>\n";
            
    $i++;
        }
    if (!empty(
    $keyword7))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword7."</span>\n";
            
    $i++;
        }                        
    if (!empty(
    $keyword8))
        {
          if (
    $i 0)
            {
                echo  
    " + ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword8."</span>\n";
            
    $i++;
        }
    if (
    $_GET['book_spoke'] == 'Book_Spoke'){
        {
          if (
    $i 0)
            {
                echo  
    " AND ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in BOOK SPOKE ".$spoke."</span>\n";
            
    $i++;
        }    
    }
    if (
    $_GET['chapter_spoke'] == 'Chapter_Spoke'){
        {
          if (
    $i 0)
            {
                echo  
    " AND ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in CHAPTER SPOKE ".$spoke."</span>\n";
            
    $i++;
        }    
    }
    if (
    $_GET['verse_spoke'] == 'Verse_Spoke'){
        {
          if (
    $i 0)
            {
                echo  
    " AND ";
            }
            echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in VERSE SPOKE ".$spoke."</span>\n";
            
    $i++;
        }    
    }
    echo 
    '<br />'

  • #2
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    This bit can be simplified into a for loop, But it means you have to set all the keywords into an array instead of individual arrays, otherwise its defeating the purpose.

    PHP Code:
    if (!empty($keyword1)) 
        { 
            echo 
    "<span class='' id='' style='font-weight: bold; color: red;'> ".$keyword1."</span>\n"
            
    $i++; 
        } 
    if (!empty(
    $keyword2)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword2."</span>\n"
        } 
    if (!empty(
    $keyword3)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: blue;'> ".$keyword3."</span>\n"
                
    $i++; 
        } 
    if (!empty(
    $keyword4)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: green;'> ".$keyword4."</span>\n"
            
    $i++; 
        } 
    if (!empty(
    $keyword5)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: orange;'> ".$keyword5."</span>\n"
            
    $i++; 
        } 
    if (!empty(
    $keyword6)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: purple;'> ".$keyword6."</span>\n"
            
    $i++; 
        } 
    if (!empty(
    $keyword7)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword7."</span>\n"
            
    $i++; 
        }                         
    if (!empty(
    $keyword8)) 
        { 
          if (
    $i 0
            { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: aqua;'> ".$keyword8."</span>\n"
            
    $i++; 
        } 
    The looped Version:

    PHP Code:
    //Instead of Doing some major changes, weve just make $keywords into an array
    //by putting square brackets AROUND the number. Hence the reason that in
    //the for loop, it starts at one rather than zero.

    //Now we have to set the color array here.
    $COLORS = array('red','blue','blue','green','orange','purple','aqua','aqua');
    for(
    $i 1$i count($keyword); $i++){                
        if (!empty(
    $keyword[$i])){ 
            if (
    $i 1) { 
                echo  
    " + "
            } 
            echo 
    "<span class='' id='' style='font-weight: bold; color: ".$COLORS[$i].";'> ".$keyword[$i]."</span>\n";
        }

    Current Project: Nothing at the minute

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    function show_keyword($keyword$color$i){
       if (!empty(
    $keyword)){
            if(
    $i 0){
                echo 
    ' + ';
            }
        echo 
    '<span class="" id="" style="font-weight: bold; color: '.$color.';"> '.$keyword.'</span>\n';
            
    $i++;
       }
       return 
    $i;
    }

    $i=0;
    $i show_keyword($keyword1,'red',$i);
    $i show_keyword($keyword2,'blue',$i);
    $i show_keyword($keyword3,'blue',$i);
    $i show_keyword($keyword4,'green',$i);
    $i show_keyword($keyword5,'orange',$i);
    $i show_keyword($keyword6,'purple',$i);
    $i show_keyword($keyword7,'aqua',$i);
    $i show_keyword($keyword8,'aqua',$i);

    if (
    $_GET['book_spoke'] == 'Book_Spoke'){
          if (
    $i 0){
            echo  
    " AND ";
        }
        echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in BOOK SPOKE ".$spoke."</span>\n";
        
    $i++;
    }
    if (
    $_GET['chapter_spoke'] == 'Chapter_Spoke'){
          if (
    $i 0){
            echo  
    " AND ";
        }
        echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in CHAPTER SPOKE ".$spoke."</span>\n";
        
    $i++;
    }
    if (
    $_GET['verse_spoke'] == 'Verse_Spoke'){
          if (
    $i 0){
            echo  
    " AND ";
        }
        echo 
    "<span class='' id='' style='font-weight: bold; color: black;'> in VERSE SPOKE ".$spoke."</span>\n";
        
    $i++;
    }
    echo 
    '<br />'
    Edit: scoop_987: sorry, I miss the part you already posted
    gilgalbiblewhee: I take a break before continue with GET part, ( this way I discover scoop_987 post)

    regards
    Last edited by oesxyl; 07-30-2008 at 01:21 AM.


  •  

    Posting Permissions

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