www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Reading file into a foreach loop.

  1. #1
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695

    Reading file into a foreach loop.

    Having some trouble, and not sure where my prob is. This function SHOULD read the contents of a file (five sections named $cipher) include them one at a time, decrypt them, then echo them out in a div (class+1). If the file does not exist, it should die quietly. This was just a quick type-out so I'm not sure what errors are in it.

    PHP Code:
    <?php 
     
    function tpout() { 
     
     GLOBAL 
    $noko$sct$path;    
     
    $spec = array ( 
            
    => 'Specifications',         => 'Information'
            
    => 'Maintenance',         => 'Safety'
            
    => 'Troubleshooting', );    # Declare TAPS

    # Javascript to Run Show/Hide DIV's    
    echo    '<script type="text/javascript">'
    echo    'function tapper() {
                el=document.getElementsByTagName("div")
                for(var i=0;i<el.length;i++){
                    if(el[i].className=="tap") {

                    if(el[i].style.display=="none"||el[i].style.display=="") {
                    el[i].style.display="block"
                } else {
                el[i].style.display="none"
                    } }
                }
            }'
    ;
    echo    
    '</script>';

        
    # Style Configurator
        
    echo '<style> .tap{ display:none; } </style>';
        echo 
    '<div id="tb">';

        
    $i 0$name $noko[1]; # Zeroing Out Increments
        
        # Check if we really have a file to work with
        
    $filename $_SERVER['DOCUMENT_ROOT'] . "/$sct/$path/$name.php";
        if (
    file_exists($filename)) { include ($filename);     
            foreach ( 
    $cipher AS $string ) {

                
    # Name out Array Contents
                
    echo "<div class=\"tab\"><a href=\"#\">$spec[$i]</a>"
                echo 
    "<div class='tap$i' style=\"display:none\">"

                    
    $hash decrypt($s); stripslashes($hash); $i++;

                echo 
    $hash."</div>"# Push Decrypted Data
                
    }
                
            echo 
    '</div>';
            
    $i NULL# Destroy Increments
        
        
    } else { /* Die without Error */ }
        
    ?>

  2. #2
    Join Date
    Jul 2003
    Location
    Lansdale, PA
    Posts
    88
    Did you run this? What is it doing? That would help us narrow down what might be wrong.

  3. #3
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Edit Unexpect $end
    PHP Code:
    <?php 
     
    function tpout() { 
     
     GLOBAL 
    $noko$sct$path;    
     
    $spec = array ( 
            
    => 'Specifications',         => 'Information'
            
    => 'Maintenance',         => 'Safety'
            
    => 'Troubleshooting', );    # Declare TAPS

    # Javascript to Run Show/Hide DIV's    
    echo    '<script type="text/javascript">';
    echo    
    'function tapper() {
                el=document.getElementsByTagName("div")
                for(var i=0;i<el.length;i++){
                    if(el[i].className=="tap") {

                    if(el[i].style.display=="none"||el[i].style.display=="") {
                    el[i].style.display="block"
                } else {
                el[i].style.display="none"
                    } }
                }
            }'
    ;
    echo    
    '</script>';

        
    # Style Configurator
        
    echo '<style> .tap{ display:none; } </style>';
        echo 
    '<div id="tb">';

        
    $i 0$name $noko[1]; # Zeroing Out Increments
        
        # Check if we really have a file to work with
        
    $filename $_SERVER['DOCUMENT_ROOT'] . "/$sct/$path/$name.php";
        if (
    file_exists($filename)) { include ($filename);     
            foreach ( 
    $cipher AS $s ) {

                
    # Name out Array Contents
                
    echo "<div class=\"tab\"><a href=\"#\">$spec[$i]</a>"
                echo 
    "<div class='tap$i' style=\"display:none\">"

                        
    $hash decrypt($s); 
                        echo 
    stripslashes($hash); 

                echo 
    "</div>"$i++; # Push Decrypted Data
                
    }
                
            echo 
    '</div>';
            
    $i NULL# Destroy Increments
        
        
    } else { /* Die without Error */ }

      
    ?>
    Last edited by ehime; 09-01-2009 at 01:10 PM.

  4. #4
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    I fixed the gross errors and it looks like its locking up at my foreach loop.

  5. #5
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    if I include crypto here (and no where else) it tells me that I am trying to redeclare encrypt(), if I don't include it, it tell me that I have an invalid argument in my foreach loop?

    the ciphers look like this:
    PHP Code:
    $cipher # Cipher Block / Specifications
    'M0zOgi0aHMSIrHNXJd7S1HsBWZM6uPtyXeASqLxXiC+g+w8qrcZtmvZNdabWl5/rC8dxw8Cj2zx83oFHi/a11'.
    'DYjDToECEuT8fU16bek9s69vJng9DhXHdMyYcMJH90JIVUgxpnTy1VY54oZ2xi0x2JCgefONN5ZpdCqLOfRP/'.
    'fJfK8QbKq0CcuZuy6DBh6g1/vrHd59XLaPX1mXpNkjgtsy5JXCbKkfPIvmYlTveEVDHeyaeLDDwHUl/OHdQ5E'.
    'EOFq/1I15PdqarMdd2/l2bZN5DUkWo78TxAhkb2/dcDW4NFNgbYJgrqMKFieGddBsy/5Fv3VPfSaexVdZDWig'.
    '8kiduG1B6be07WD/BicxFvISQnbdlJ1cWO5Aj8OvSzI3sIM3/aJ9VAYPFugDov13ifCdH+rkY6Ayq45OUXeq7'.
    'Hwp2AOWJxxZINbAc33tROn5hhZOiBsmne9SpV2AzGjiUbA2Mb1//ZMPL7ki5zFZjjlbu0PhgzDFg6zAsNIPQz';

    $cipher # Cipher Block / Information
    'fa8VIai59HXBnO1/x0ofsIC5hxpH0/0n63vonGhOyaHz+SprZHc29+qR7jDlA/0qGaDZgpJCzVD4xiQ0VcpiS'.
    'vbPjQp/rjYIFiUTWlBvVzXZqq8a5fxD4mRj6+hihpKff+gI2OKlCippjuB96J8GZkC8p69kQTm9l69dc3HW3q'.
    'CYCn50+w2O/ocAPWLa+JOblWGiYhYu0fe7wCRFGUO3ynByxDhWV4XjpappW83PPyE9J9Y3JEwjhr9Zc+lut1v';

    $cipher # Cipher Block / Maintenance
    'fa8VIai59HXBnO1/x0ofsIC5hxpH0/0n63vonGhOyaFIFdZvWqX05FqzvGgQgpWIaAesTan2M6nSTt8PZjAGV'.
    'eO2ErcEWjPlAW48GauWrZpiSQllabGOMvM3vkbqIiIYP6OSFT4iWzHiwez/TusSPmqsF8pBxgk3xx5MUZvROu'.
    't9PcQQtC+nufxHpo3++06pmhsxeVVV3XL87oltVAAV0ryMgBkc1aADA1sIZUAbppQ0La038Jr1yqGRjay1MLN';

    $cipher # Cipher Block / Safety
    'fa8VIai59HXBnO1/x0ofsIC5hxpH0/0n63vonGhOyaFlP4D1qReZINHU4z+HDOblh9kVqSfPL6Xle8MdJrUck'.
    'Bnz9Kb4sWrmn5gLwL5nrE+mov7fUO3DfzENTmhasfC7Lx9TtuS41nddY8hcWL3Tm8gh2gFgcWvCXOQfajPOIG'.
    '4w1nyAkkzudvojR637KkEyjQOyhHSS13chDBSdzGMoDaG+uzYTTL8gSa9ZbWTTyF84ayr7h7+gBOCjGwN61F3';

    .
    etc 

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Do you have a closing brace for the function? It doesn't seem to be included in the first post.

  7. #7
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    I saw that, and inserted it, yes.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    $cipher is not an array the way it displayed above. You are reassigning the value with each addition. If you want it to be an array use one of the array assignments such as:
    PHP Code:
    $cipher[] = "blah blah"

  9. #9
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Does cipher need to be an array though? I thought I could use it as a string?

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    foreach is used to loop through each instance in an array. Additionally, if you have a file that resets the variable repeatedly you will only be able to access the value you set it to last.

    Example:

    PHP Code:
    $var1 "a";
    $var1 "b";
    $var1 "c";

    echo 
    $var1
    Will output:
    Code:
    c
    Check out the manual on Arrays.

  11. #11
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    So it seems like it's close to working. You can see here what I mean.
    Click Here Then the Century Color, it's still doing weird stuff...

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    Um....ok. I didn't see any php errors or anything. What did that link have to do with the problem above?

  13. #13
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    That's the link to the page I'm using it on.

  14. #14
    Join Date
    Jan 2009
    Posts
    3,346
    What do you mean by "its still doing weird stuff"?

  15. #15
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    I think it (might be) the Javascript? On clicking other links it blanks out half of my background bar.

Thread Information

Users Browsing this Thread

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

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