www.webdeveloper.com
Results 1 to 8 of 8

Thread: Having problem with nested if{}'s inside a while{}

  1. #1
    Join Date
    May 2006
    Posts
    245

    Having problem with nested if{}'s inside a while{}

    Hi,

    I think I am in a muddle

    I have several nested if{}'s and else{}'s within a while{}
    the logic seemed to be right ... but on doing the else{}
    the script doesn't seem to hit the end while "}" and continue
    with the while loop .

    Here is my code:

    PHP Code:
    $i=0;
    while(
    $i<=$input_words_cnt){
            if(!
    in_array($input_words[$i], $theos_words)) {
                
    $j $i+1;
                echo 
    "<br>Did NOT find \"$input_words[$i]\" in Theos";
                
    $phrase1 "$input_words[$i] $input_words[$j]";
                if(!
    in_array($phrase1$theos_words)) {  // checking 2 word phrase
                    
    $k $j+1;
                    echo 
    "<br>Did NOT find \"$phrase1\" in Theos";
                    
    $phrase2 "$input_words[$i] $input_words[$j] $input_words[$k]";
                    if(!
    in_array($phrase2$theos_words)) {     // checking 3 word phrase
                        
    $l $k+1;
                        echo 
    "<br>Did NOT find \"$phrase2\" in Theos";
                        
    $phrase3 "$input_words[$i] $input_words[$j] $input_words[$k] $input_words[$l]";
                        if(!
    in_array($phrase3$theos_words)) { // checking 4 word phrase
                            
    echo "<br>Did NOT find \"$phrase3\" in Theos";
                            
    $new_words[] = $phrase3;
                            require_once (
    "auto_theos_fm2.php");
                            exit;    
                            }
                        else { 
                            echo 
    "<br>Found \"$phrase3\" in Theos<br>"$i=$i+4;
                            }    
                        }
                    else { 
                        echo 
    "<br>Found \"$phrase2\" in Theos<br>"$i=$i+3;
                        }        
                    }
                else { 
                    echo 
    "<br>Found \"$phrase1\" in Theos<br>"$i=$i+2;
                    }
                }
            else { 
                echo 
    "<br>Found \"$input_words[$i]\" in Theos<br>"$i=$i+1;
                }
            }  
    // END while 
    When $phrase1 is found ( and it branches to the else{}
    the script stops and does not continue with the while loop

    ( probably have the same problem with $phrase2 and $phrase3
    but haven't got that far yet !! )

    Not sure how I should structure this

    Any help appreciated


    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    wow, lots of nested if-else's. I checked for missing braces etc and see no immediate problem. But without the data you're testing against it's hard to see where things are failing. Also, how you index increments work.

    If you supply the data and what kind of result you wish to achieve with it, maybe there is another way around the problem.

    I didn't fix anything at all in your code, but I don't feel like reformatting it again when there is more data to work with:

    PHP Code:
    $i 0;
    while (
    $i <= $input_words_cnt) {
        if (!
    in_array($input_words[$i], $theos_words)) {

            
    $j $i 1;
            echo 
    "<br>Did NOT find \"$input_words[$i]\" in Theos";
            
    $phrase1 "$input_words[$i] $input_words[$j]";

            if (!
    in_array($phrase1$theos_words)) {  // checking 2 word phrase

                
    $k $j 1;
                echo 
    "<br>Did NOT find \"$phrase1\" in Theos";
                
    $phrase2 "$input_words[$i] $input_words[$j] $input_words[$k]";

                if (!
    in_array($phrase2$theos_words)) {     // checking 3 word phrase

                    
    $l $k 1;
                    echo 
    "<br>Did NOT find \"$phrase2\" in Theos";
                    
    $phrase3 "$input_words[$i] $input_words[$j] $input_words[$k] $input_words[$l]";

                    if (!
    in_array($phrase3$theos_words)) { // checking 4 word phrase

                        
    echo "<br>Did NOT find \"$phrase3\" in Theos";
                        
    $new_words[] = $phrase3;
                        require_once (
    "auto_theos_fm2.php");
                        exit;

                    } else {echo 
    "<br>Found \"$phrase3\" in Theos<br>"$i $i 4;}
                } else {echo 
    "<br>Found \"$phrase2\" in Theos<br>"$i $i 3;}
            } else {echo 
    "<br>Found \"$phrase1\" in Theos<br>"$i $i 2;}
        } else {echo 
    "<br>Found \"$input_words[$i]\" in Theos<br>"$i $i 1;}

    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    May 2006
    Posts
    245
    OK fair enough !

    It is related to the data sets I have been working with.

    What I am doing is seeing if various chunks of data are already stored
    in the table:

    The already stored chunks are in an array: $theos_words


    The new data comes in like this:
    $Db_text= "data1 data2 data3 data4 data5 data6 data7 data8 data9 data10
    data11 data2 data3 data12 data5 data16 data17 data18 data19 data4 data1 data2 data23 data20 data25 data26 data12 data8 data13 data13";


    ( These are actually words of sentences -
    what I am doing if seeing if they appear in a glossary table
    - which I call theos )


    So I create an array:

    PHP Code:
    $Db_text strtolower($Db_text);
    $input_words explode(" "$Db_text);
    $input_words_cnt count($input_words);    

    // I check these so see if they are in the glossary:

    foreach($input_words as $input_word){
        if(!
    in_array($input_word$theos_words)) {
            
    $new_words[] = $input_word;
            }
        } 
    So I now have an array of single words that are not found
    in the glossary $new_words.


    Now I want to see if a single word - or that word and up to 3 neighboring words are already recorded in the glossary

    This is the reason for the if{}s and else{}s

    PHP Code:
    $i 0;
    while (
    $i <= $input_words_cnt) {
        if (!
    in_array($input_words[$i], $theos_words)) {

            
    $j $i 1;
            echo 
    "<br>Did NOT find \"$input_words[$i]\" in Theos";
            
    $phrase1 "$input_words[$i] $input_words[$j]";

            if (!
    in_array($phrase1$theos_words)) {  // checking 2 word phrase

                
    $k $j 1;
                echo 
    "<br>Did NOT find \"$phrase1\" in Theos";
                
    $phrase2 "$input_words[$i] $input_words[$j] $input_words[$k]";

                if (!
    in_array($phrase2$theos_words)) {     // checking 3 word phrase

                    
    $l $k 1;
                    echo 
    "<br>Did NOT find \"$phrase2\" in Theos";
                    
    $phrase3 "$input_words[$i] $input_words[$j] $input_words[$k] $input_words[$l]";

                    if (!
    in_array($phrase3$theos_words)) { // checking 4 word phrase

                        
    echo "<br>Did NOT find \"$phrase3\" in Theos";
                        
    $new_words[] = $phrase3;
                        require_once (
    "auto_theos_fm2.php");
                        exit;

                    } else {echo 
    "<br>Found \"$phrase3\" in Theos<br>"$i $i 4;}
                } else {echo 
    "<br>Found \"$phrase2\" in Theos<br>"$i $i 3;}
            } else {echo 
    "<br>Found \"$phrase1\" in Theos<br>"$i $i 2;}
        } else {echo 
    "<br>Found \"$input_words[$i]\" in Theos<br>"$i $i 1;}

    The "auto_theos_fm2.php" contains a form to allow me to input the missing data

    So, if the word and none of the 4 forward-adjacent words are not found
    then auto_theos_fm2.php should be loaded.

    If any of those 4 combination are found, the while loop should continue to
    the next word after the found combination.
    ( that explains the difference in $i increment )

    Hopefully that explains what I am trying do !!


    Thanks.


    .
    Last edited by jeddik; 11-13-2013 at 09:10 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  4. #4
    Join Date
    May 2006
    Posts
    245
    Maybe a case/switch would work better in this case ?

    Presently when the script finds a combination it just stops after displaying
    e.g. Found surgical gloves in Theos

    or Found carbonic acid crystals in Theos

    The while loop does not continue to next word.

    Any ideas ?



    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Trying things...
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Right, not that I'm 100% certain of everything... maybe 70-90% :P

    index.php
    PHP Code:
    <?php

        $Db_text 
    'data1 data2 data3 data4 data5 data6 data7 data8 data9 data10 data11 data2 data3 data12 data5 data16 data17 data18 data19 data4 data1 data2 data23 data20 data25 data26 data12 data8 data13 data13';

        
    $Db_text         strtolower($Db_text);
        
    $input_words     explode(' '$Db_text);
        
    $input_words_cnt count($input_words);
        
    $theos_words     = array(

            
    'data1',
            
    'data1 data2',
            
    'data1 data2 data3',
            
    'data4',
            
    'data5',
            
    'data5 data6',
            
    'data5 data6 data7',
            
    'data5 data6 data7 data8',
            
    'data10 data11 data2'

        
    );

        for (
    $i 0$i < (count($input_words) - 3); ++$i) {
            for (
    $j 3$j >= 0; --$j) {

                
    $phrase implode(' 'array_slice($input_words$i$j 1));

                if (!
    in_array($phrase$theos_words)) {
                    if (
    $j === 3) {
                        echo 
    $phrase ' -- NOT FOUND -- <b>ADD TO NEW WORDS AND REQUIRE FILE</b><br />';
                    } else {
                        echo 
    $phrase ' -- NOT FOUND<br />';
                    }
                } else {
                    echo 
    $phrase ' -- FOUND<br />';
                    break;
                }

            }
            if (
    $j 0) {$i += $j;}
            echo 
    '<br />';

        }

    ?>
    Output

    Code:
    data1 data2 data3 data4 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data1 data2 data3 -- FOUND
    
    data4 data5 data6 data7 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data4 data5 data6 -- NOT FOUND
    data4 data5 -- NOT FOUND
    data4 -- FOUND
    
    data5 data6 data7 data8 -- FOUND
    
    data9 data10 data11 data2 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data9 data10 data11 -- NOT FOUND
    data9 data10 -- NOT FOUND
    data9 -- NOT FOUND
    
    data10 data11 data2 data3 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data10 data11 data2 -- FOUND
    
    data3 data12 data5 data16 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data3 data12 data5 -- NOT FOUND
    data3 data12 -- NOT FOUND
    data3 -- NOT FOUND
    
    data12 data5 data16 data17 -- NOT FOUND -- ADD TO NEW WORDS AND REQUIRE FILE
    data12 data5 data16 -- NOT FOUND
    data12 data5 -- NOT FOUND
    data12 -- NOT FOUND
    
    ... and so on
    JavaScript: Learn | Validate | Compact | bionoid

  7. #7
    Join Date
    May 2006
    Posts
    245
    Thanks VERY much.

    I will run it through some tests today

    BTW. D
    o you know anything about .htaccess rewrite rules ?

    I wrote this thread but no-one visits that forum !!

    http://www.webdeveloper.com/forum/sh...or-my-rss-feed


    Thanks again.


    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Honestly I use .htaccess only for a simple redirection and caching purposes.

    For example:

    Code:
    <IfModule mod_expires.c>
      ExpiresActive On
      ExpiresByType image/gif A2592000
      ExpiresByType image/jpeg A2592000
      ExpiresByType image/png A2592000
      ExpiresDefault A86400
    </IfModule>
    
    php_flag magic_quotes_gpc Off
    php_flag register_globals Off
    php_flag display_errors On
    php_value error_reporting 8191
    php_value date.timezone UTC
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /index.php [L]
    This is my entire .htaccess ruleset.
    From there I determine what was being requested through my index.php file.

    Works like a charm.
    JavaScript: Learn | Validate | Compact | bionoid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles